-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also renamed VersionUtils to more readable abstraction JavaVersion Added support for debian naming convention Using min supported version (6) as the default if JDK version can't be figured out
- Loading branch information
Inderjeet Singh
committed
May 4, 2018
1 parent
57085d6
commit 32bf645
Showing
9 changed files
with
190 additions
and
70 deletions.
There are no files selected for viewing
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
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
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
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,90 @@ | ||
/* | ||
* Copyright (C) 2017 The Gson authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gson.util; | ||
|
||
/** | ||
* Utility to check the major Java version of the current JVM. | ||
*/ | ||
public final class JavaVersion { | ||
// Oracle defines naming conventions at http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html | ||
// However, many alternate implementations differ. For example, Debian used 9-debian as the version string | ||
|
||
private static final int majorJavaVersion = determineMajorJavaVersion(); | ||
|
||
private static int determineMajorJavaVersion() { | ||
String javaVersion = System.getProperty("java.version"); | ||
return getMajorJavaVersion(javaVersion); | ||
} | ||
|
||
// Visible for testing only | ||
static int getMajorJavaVersion(String javaVersion) { | ||
int version = parseDotted(javaVersion); | ||
if (version == -1) { | ||
version = extractBeginningInt(javaVersion); | ||
} | ||
if (version == -1) { | ||
return 6; // Choose minimum supported JDK version as default | ||
} | ||
return version; | ||
} | ||
|
||
// Parses both legacy 1.8 style and newer 9.0.4 style | ||
private static int parseDotted(String javaVersion) { | ||
try { | ||
String[] parts = javaVersion.split("[._]"); | ||
int firstVer = Integer.parseInt(parts[0]); | ||
if (firstVer == 1 && parts.length > 1) { | ||
return Integer.parseInt(parts[1]); | ||
} else { | ||
return firstVer; | ||
} | ||
} catch (NumberFormatException e) { | ||
return -1; | ||
} | ||
} | ||
|
||
private static int extractBeginningInt(String javaVersion) { | ||
try { | ||
StringBuilder num = new StringBuilder(); | ||
for (int i = 0; i < javaVersion.length(); ++i) { | ||
char c = javaVersion.charAt(i); | ||
if (Character.isDigit(c)) { | ||
num.append(c); | ||
} else { | ||
break; | ||
} | ||
} | ||
return Integer.parseInt(num.toString()); | ||
} catch (NumberFormatException e) { | ||
return -1; | ||
} | ||
} | ||
|
||
/** | ||
* @return the major Java version, i.e. '8' for Java 1.8, '9' for Java 9 etc. | ||
*/ | ||
public static int getMajorJavaVersion() { | ||
return majorJavaVersion; | ||
} | ||
|
||
/** | ||
* @return {@code true} if the application is running on Java 9 or later; and {@code false} otherwise. | ||
*/ | ||
public static boolean isJava9OrLater() { | ||
return majorJavaVersion >= 9; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
79 changes: 79 additions & 0 deletions
79
gson/src/test/java/com/google/gson/util/JavaVersionTest.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,79 @@ | ||
/* | ||
* Copyright (C) 2017 The Gson authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.gson.util; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Unit and functional tests for {@link JavaVersion} | ||
* | ||
* @author Inderjeet Singh | ||
*/ | ||
public class JavaVersionTest { | ||
// Borrowed some of test strings from https://github.com/prestodb/presto/blob/master/presto-main/src/test/java/com/facebook/presto/server/TestJavaVersion.java | ||
|
||
@Test | ||
public void testGetMajorJavaVersion() { | ||
JavaVersion.getMajorJavaVersion(); | ||
} | ||
|
||
@Test | ||
public void testJava6() { | ||
assertEquals(6, JavaVersion.getMajorJavaVersion("1.6.0")); // http://www.oracle.com/technetwork/java/javase/version-6-141920.html | ||
} | ||
|
||
@Test | ||
public void testJava7() { | ||
assertEquals(7, JavaVersion.getMajorJavaVersion("1.7.0")); // http://www.oracle.com/technetwork/java/javase/jdk7-naming-418744.html | ||
} | ||
|
||
@Test | ||
public void testJava8() { | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8")); | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8.0")); | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8.0_131")); | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8.0_60-ea")); | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8.0_111-internal")); | ||
|
||
// openjdk8 per https://github.com/AdoptOpenJDK/openjdk-build/issues/93 | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8.0-internal")); | ||
assertEquals(8, JavaVersion.getMajorJavaVersion("1.8.0_131-adoptopenjdk")); | ||
} | ||
|
||
@Test | ||
public void testJava9() { | ||
// Legacy style | ||
assertEquals(9, JavaVersion.getMajorJavaVersion("9.0.4")); // Oracle JDK 9 | ||
assertEquals(9, JavaVersion.getMajorJavaVersion("9-Debian")); // Debian as reported in https://github.com/google/gson/issues/1310 | ||
// New style | ||
assertEquals(9, JavaVersion.getMajorJavaVersion("9-ea+19")); | ||
assertEquals(9, JavaVersion.getMajorJavaVersion("9+100")); | ||
assertEquals(9, JavaVersion.getMajorJavaVersion("9.0.1+20")); | ||
assertEquals(9, JavaVersion.getMajorJavaVersion("9.1.1+20")); | ||
} | ||
|
||
@Test | ||
public void testJava10() { | ||
assertEquals(10, JavaVersion.getMajorJavaVersion("10.0.1")); // Oracle JDK 10.0.1 | ||
} | ||
|
||
@Test | ||
public void testUnknownVersionFormat() { | ||
assertEquals(6, JavaVersion.getMajorJavaVersion("Java9")); // unknown format | ||
} | ||
} |