Skip to content

Commit

Permalink
Fixed #1310
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 70 deletions.
Expand Up @@ -32,7 +32,7 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.google.gson.util.VersionUtils;
import com.google.gson.util.JavaVersion;

/**
* This type adapter supports three subclasses of date: Date, Timestamp, and
Expand All @@ -59,7 +59,7 @@ final class DefaultDateTypeAdapter extends TypeAdapter<Date> {
if (!Locale.getDefault().equals(Locale.US)) {
dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT));
}
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(DateFormat.DEFAULT, DateFormat.DEFAULT));
}
}
Expand All @@ -78,7 +78,7 @@ final class DefaultDateTypeAdapter extends TypeAdapter<Date> {
if (!Locale.getDefault().equals(Locale.US)) {
dateFormats.add(DateFormat.getDateInstance(style));
}
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
dateFormats.add(PreJava9DateFormatProvider.getUSDateFormat(style));
}
}
Expand All @@ -93,7 +93,7 @@ public DefaultDateTypeAdapter(Class<? extends Date> dateType, int dateStyle, int
if (!Locale.getDefault().equals(Locale.US)) {
dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle));
}
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(dateStyle, timeStyle));
}
}
Expand Down
Expand Up @@ -26,7 +26,7 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.google.gson.util.VersionUtils;
import com.google.gson.util.JavaVersion;

import java.io.IOException;
import java.text.DateFormat;
Expand Down Expand Up @@ -62,7 +62,7 @@ public DateTypeAdapter() {
if (!Locale.getDefault().equals(Locale.US)) {
dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT));
}
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(DateFormat.DEFAULT, DateFormat.DEFAULT));
}
}
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/
package com.google.gson.internal.reflect;

import com.google.gson.util.VersionUtils;
import com.google.gson.util.JavaVersion;

import java.lang.reflect.AccessibleObject;

Expand All @@ -33,7 +33,7 @@
public abstract class ReflectionAccessor {

// the singleton instance, use getInstance() to obtain
private static final ReflectionAccessor instance = VersionUtils.getMajorJavaVersion() < 9 ? new PreJava9ReflectionAccessor() : new UnsafeReflectionAccessor();
private static final ReflectionAccessor instance = JavaVersion.getMajorJavaVersion() < 9 ? new PreJava9ReflectionAccessor() : new UnsafeReflectionAccessor();

/**
* Does the same as {@code ao.setAccessible(true)}, but never throws
Expand Down
90 changes: 90 additions & 0 deletions gson/src/main/java/com/google/gson/util/JavaVersion.java
@@ -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;
}
}
49 changes: 0 additions & 49 deletions gson/src/main/java/com/google/gson/util/VersionUtils.java

This file was deleted.

Expand Up @@ -23,7 +23,7 @@
import java.util.Locale;
import java.util.TimeZone;

import com.google.gson.util.VersionUtils;
import com.google.gson.util.JavaVersion;
import junit.framework.TestCase;

/**
Expand All @@ -47,9 +47,9 @@ private void assertFormattingAlwaysEmitsUsLocale(Locale locale) {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(locale);
try {
String afterYearSep = VersionUtils.isJava9OrLater() ? ", " : " ";
String afterYearLongSep = VersionUtils.isJava9OrLater() ? " at " : " ";
String utcFull = VersionUtils.isJava9OrLater() ? "Coordinated Universal Time" : "UTC";
String afterYearSep = JavaVersion.isJava9OrLater() ? ", " : " ";
String afterYearLongSep = JavaVersion.isJava9OrLater() ? " at " : " ";
String utcFull = JavaVersion.isJava9OrLater() ? "Coordinated Universal Time" : "UTC";
assertFormatted(String.format("Jan 1, 1970%s12:00:00 AM", afterYearSep),
new DefaultDateTypeAdapter(Date.class));
assertFormatted("1/1/70", new DefaultDateTypeAdapter(Date.class, DateFormat.SHORT));
Expand All @@ -75,7 +75,7 @@ public void testParsingDatesFormattedWithSystemLocale() throws Exception {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.FRANCE);
try {
String afterYearSep = VersionUtils.isJava9OrLater() ? " à " : " ";
String afterYearSep = JavaVersion.isJava9OrLater() ? " à " : " ";
assertParsed(String.format("1 janv. 1970%s00:00:00", afterYearSep),
new DefaultDateTypeAdapter(Date.class));
assertParsed("01/01/70", new DefaultDateTypeAdapter(Date.class, DateFormat.SHORT));
Expand All @@ -87,7 +87,7 @@ public void testParsingDatesFormattedWithSystemLocale() throws Exception {
new DefaultDateTypeAdapter(DateFormat.MEDIUM, DateFormat.MEDIUM));
assertParsed(String.format("1 janvier 1970%s00:00:00 UTC", afterYearSep),
new DefaultDateTypeAdapter(DateFormat.LONG, DateFormat.LONG));
assertParsed(VersionUtils.isJava9OrLater() ?
assertParsed(JavaVersion.isJava9OrLater() ?
"jeudi 1 janvier 1970 à 00:00:00 Coordinated Universal Time" :
"jeudi 1 janvier 1970 00 h 00 UTC",
new DefaultDateTypeAdapter(DateFormat.FULL, DateFormat.FULL));
Expand Down Expand Up @@ -127,7 +127,7 @@ public void testFormatUsesDefaultTimezone() throws Exception {
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
try {
String afterYearSep = VersionUtils.isJava9OrLater() ? ", " : " ";
String afterYearSep = JavaVersion.isJava9OrLater() ? ", " : " ";
assertFormatted(String.format("Dec 31, 1969%s4:00:00 PM", afterYearSep),
new DefaultDateTypeAdapter(Date.class));
assertParsed("Dec 31, 1969 4:00:00 PM", new DefaultDateTypeAdapter(Date.class));
Expand Down
Expand Up @@ -56,7 +56,7 @@
import java.util.TreeSet;
import java.util.UUID;

import com.google.gson.util.VersionUtils;
import com.google.gson.util.JavaVersion;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -330,7 +330,7 @@ public void testBitSetDeserialization() throws Exception {
public void testDefaultDateSerialization() {
Date now = new Date(1315806903103L);
String json = gson.toJson(now);
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
assertEquals("\"Sep 11, 2011, 10:55:03 PM\"", json);
} else {
assertEquals("\"Sep 11, 2011 10:55:03 PM\"", json);
Expand Down Expand Up @@ -375,7 +375,7 @@ public void testDefaultJavaSqlDateDeserialization() {
public void testDefaultJavaSqlTimestampSerialization() {
Timestamp now = new java.sql.Timestamp(1259875082000L);
String json = gson.toJson(now);
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
assertEquals("\"Dec 3, 2009, 1:18:02 PM\"", json);
} else {
assertEquals("\"Dec 3, 2009 1:18:02 PM\"", json);
Expand Down Expand Up @@ -405,7 +405,7 @@ public void testDefaultDateSerializationUsingBuilder() throws Exception {
Gson gson = new GsonBuilder().create();
Date now = new Date(1315806903103L);
String json = gson.toJson(now);
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
assertEquals("\"Sep 11, 2011, 10:55:03 PM\"", json);
} else {
assertEquals("\"Sep 11, 2011 10:55:03 PM\"", json);
Expand Down
4 changes: 2 additions & 2 deletions gson/src/test/java/com/google/gson/functional/ObjectTest.java
Expand Up @@ -44,7 +44,7 @@
import java.util.Map;
import java.util.TimeZone;

import com.google.gson.util.VersionUtils;
import com.google.gson.util.JavaVersion;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -484,7 +484,7 @@ public void testSingletonLists() {
public void testDateAsMapObjectField() {
HasObjectMap a = new HasObjectMap();
a.map.put("date", new Date(0));
if (VersionUtils.isJava9OrLater()) {
if (JavaVersion.isJava9OrLater()) {
assertEquals("{\"map\":{\"date\":\"Dec 31, 1969, 4:00:00 PM\"}}", gson.toJson(a));
} else {
assertEquals("{\"map\":{\"date\":\"Dec 31, 1969 4:00:00 PM\"}}", gson.toJson(a));
Expand Down
79 changes: 79 additions & 0 deletions gson/src/test/java/com/google/gson/util/JavaVersionTest.java
@@ -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
}
}

0 comments on commit 32bf645

Please sign in to comment.