-
-
Notifications
You must be signed in to change notification settings - Fork 989
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ceki Gulcu <[email protected]>
- Loading branch information
Showing
3 changed files
with
111 additions
and
49 deletions.
There are no files selected for viewing
80 changes: 55 additions & 25 deletions
80
slf4j-log4j12/src/main/java/org/slf4j/impl/VersionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,63 @@ | ||
/** | ||
* Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland) | ||
* All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
package org.slf4j.impl; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.slf4j.helpers.Util; | ||
|
||
public class VersionUtil { | ||
static final int MINIMAL_VERSION = 5; | ||
static final int DEFAULT_GUESS = 8; | ||
|
||
static public int getJavaMajorVersion() { | ||
String javaVersionString = Util.safeGetSystemProperty("java.version"); | ||
return getJavaMajorVersion(javaVersionString); | ||
} | ||
|
||
static public int getJavaMajorVersion() { | ||
String javaVersionString = Util.safeGetSystemProperty("java.version"); | ||
return getJavaMajorVersion(javaVersionString); | ||
} | ||
static public int getJavaMajorVersion(String versionString) { | ||
if (versionString == null) | ||
return DEFAULT_GUESS; | ||
if (versionString.startsWith("1.")) { | ||
return versionString.charAt(2) - '0'; | ||
} else { | ||
String firstDigits = extractFirstDigits(versionString); | ||
try { | ||
return Integer.parseInt(firstDigits); | ||
} catch(NumberFormatException e) { | ||
return DEFAULT_GUESS; | ||
} | ||
} | ||
} | ||
|
||
static int getJavaMajorVersion(String versionString) { | ||
if (versionString == null) | ||
return MINIMAL_VERSION; | ||
if (versionString.startsWith("1.")) { | ||
return versionString.charAt(2) - '0'; | ||
} else { | ||
// we running under Java 9 or later | ||
try { | ||
Method versionMethod = Runtime.class.getMethod("version"); | ||
Object versionObj = versionMethod.invoke(null); | ||
Method majorMethod = versionObj.getClass().getMethod("major"); | ||
Integer resultInteger = (Integer) majorMethod.invoke(versionObj); | ||
return resultInteger.intValue(); | ||
} catch (Exception e) { | ||
return MINIMAL_VERSION; | ||
} | ||
} | ||
} | ||
private static String extractFirstDigits(String versionString) { | ||
StringBuffer buf = new StringBuffer(); | ||
for(char c : versionString.toCharArray()) { | ||
if(Character.isDigit(c)) | ||
buf.append(c); | ||
else | ||
break; | ||
} | ||
return buf.toString(); | ||
|
||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
slf4j-log4j12/src/test/java/org/slf4j/impl/UtilVersionTest.java
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
slf4j-log4j12/src/test/java/org/slf4j/impl/VersionUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland) | ||
* All rights reserved. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
package org.slf4j.impl; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class VersionUtilTest { | ||
|
||
@Test | ||
public void test() { | ||
System.out.println(System.getProperty("java.version")); | ||
assertEquals(6, VersionUtil.getJavaMajorVersion("1.6")); | ||
assertEquals(7, VersionUtil.getJavaMajorVersion("1.7.0_21-b11")); | ||
assertEquals(8, VersionUtil.getJavaMajorVersion("1.8.0_25")); | ||
} | ||
|
||
@Test | ||
public void testJava9() { | ||
assertEquals(9, VersionUtil.getJavaMajorVersion("9")); | ||
assertEquals(9, VersionUtil.getJavaMajorVersion("9.12")); | ||
assertEquals(9, VersionUtil.getJavaMajorVersion("9ea")); | ||
|
||
} | ||
|
||
@Test | ||
public void testJava11() { | ||
assertEquals(11, VersionUtil.getJavaMajorVersion("11")); | ||
assertEquals(11, VersionUtil.getJavaMajorVersion("11.612")); | ||
|
||
} | ||
|
||
} |