Skip to content

Commit 19e36ff

Browse files
committed
make VersionUtil more robust
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent d32d053 commit 19e36ff

File tree

3 files changed

+111
-49
lines changed

3 files changed

+111
-49
lines changed
Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,63 @@
1+
/**
2+
* Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*
24+
*/
125
package org.slf4j.impl;
226

3-
import java.lang.reflect.Method;
4-
527
import org.slf4j.helpers.Util;
628

729
public class VersionUtil {
8-
static final int MINIMAL_VERSION = 5;
30+
static final int DEFAULT_GUESS = 8;
31+
32+
static public int getJavaMajorVersion() {
33+
String javaVersionString = Util.safeGetSystemProperty("java.version");
34+
return getJavaMajorVersion(javaVersionString);
35+
}
936

10-
static public int getJavaMajorVersion() {
11-
String javaVersionString = Util.safeGetSystemProperty("java.version");
12-
return getJavaMajorVersion(javaVersionString);
13-
}
37+
static public int getJavaMajorVersion(String versionString) {
38+
if (versionString == null)
39+
return DEFAULT_GUESS;
40+
if (versionString.startsWith("1.")) {
41+
return versionString.charAt(2) - '0';
42+
} else {
43+
String firstDigits = extractFirstDigits(versionString);
44+
try {
45+
return Integer.parseInt(firstDigits);
46+
} catch(NumberFormatException e) {
47+
return DEFAULT_GUESS;
48+
}
49+
}
50+
}
1451

15-
static int getJavaMajorVersion(String versionString) {
16-
if (versionString == null)
17-
return MINIMAL_VERSION;
18-
if (versionString.startsWith("1.")) {
19-
return versionString.charAt(2) - '0';
20-
} else {
21-
// we running under Java 9 or later
22-
try {
23-
Method versionMethod = Runtime.class.getMethod("version");
24-
Object versionObj = versionMethod.invoke(null);
25-
Method majorMethod = versionObj.getClass().getMethod("major");
26-
Integer resultInteger = (Integer) majorMethod.invoke(versionObj);
27-
return resultInteger.intValue();
28-
} catch (Exception e) {
29-
return MINIMAL_VERSION;
30-
}
31-
}
32-
}
52+
private static String extractFirstDigits(String versionString) {
53+
StringBuffer buf = new StringBuffer();
54+
for(char c : versionString.toCharArray()) {
55+
if(Character.isDigit(c))
56+
buf.append(c);
57+
else
58+
break;
59+
}
60+
return buf.toString();
61+
62+
}
3363
}

slf4j-log4j12/src/test/java/org/slf4j/impl/UtilVersionTest.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
3+
* All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining
6+
* a copy of this software and associated documentation files (the
7+
* "Software"), to deal in the Software without restriction, including
8+
* without limitation the rights to use, copy, modify, merge, publish,
9+
* distribute, sublicense, and/or sell copies of the Software, and to
10+
* permit persons to whom the Software is furnished to do so, subject to
11+
* the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
*
24+
*/
25+
package org.slf4j.impl;
26+
27+
import static org.junit.Assert.assertEquals;
28+
29+
import org.junit.Test;
30+
31+
public class VersionUtilTest {
32+
33+
@Test
34+
public void test() {
35+
System.out.println(System.getProperty("java.version"));
36+
assertEquals(6, VersionUtil.getJavaMajorVersion("1.6"));
37+
assertEquals(7, VersionUtil.getJavaMajorVersion("1.7.0_21-b11"));
38+
assertEquals(8, VersionUtil.getJavaMajorVersion("1.8.0_25"));
39+
}
40+
41+
@Test
42+
public void testJava9() {
43+
assertEquals(9, VersionUtil.getJavaMajorVersion("9"));
44+
assertEquals(9, VersionUtil.getJavaMajorVersion("9.12"));
45+
assertEquals(9, VersionUtil.getJavaMajorVersion("9ea"));
46+
47+
}
48+
49+
@Test
50+
public void testJava11() {
51+
assertEquals(11, VersionUtil.getJavaMajorVersion("11"));
52+
assertEquals(11, VersionUtil.getJavaMajorVersion("11.612"));
53+
54+
}
55+
56+
}

0 commit comments

Comments
 (0)