Merge pull request #729 from google/jdk6

updated minimum JDK version to 1.6.
This commit is contained in:
Jesse Wilson 2015-11-04 22:24:05 -05:00
commit 9e5f86d10b
36 changed files with 127 additions and 97 deletions

View File

@ -113,8 +113,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
@ -189,7 +189,7 @@
<includePackageNames>com.google.gson</includePackageNames>
<excludePackageNames>com.google.gson.internal:com.google.gson.internal.bind</excludePackageNames>
<links>
<link>http://docs.oracle.com/javase/1.5.0/docs/api/</link>
<link>http://docs.oracle.com/javase/6/docs/api/</link>
</links>
<version>true</version>
<show>protected</show>

View File

@ -67,6 +67,7 @@ final class DefaultDateTypeAdapter implements JsonSerializer<Date>, JsonDeserial
// These methods need to be synchronized since JDK DateFormat classes are not thread-safe
// See issue 162
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
synchronized (localFormat) {
String dateFormatAsString = enUsFormat.format(src);
@ -74,6 +75,7 @@ final class DefaultDateTypeAdapter implements JsonSerializer<Date>, JsonDeserial
}
}
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {

View File

@ -35,7 +35,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* unchanged.
*/
IDENTITY() {
public String translateName(Field f) {
@Override public String translateName(Field f) {
return f.getName();
}
},
@ -51,7 +51,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* </ul>
*/
UPPER_CAMEL_CASE() {
public String translateName(Field f) {
@Override public String translateName(Field f) {
return upperCaseFirstLetter(f.getName());
}
},
@ -70,7 +70,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* @since 1.4
*/
UPPER_CAMEL_CASE_WITH_SPACES() {
public String translateName(Field f) {
@Override public String translateName(Field f) {
return upperCaseFirstLetter(separateCamelCase(f.getName(), " "));
}
},
@ -88,7 +88,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* </ul>
*/
LOWER_CASE_WITH_UNDERSCORES() {
public String translateName(Field f) {
@Override public String translateName(Field f) {
return separateCamelCase(f.getName(), "_").toLowerCase(Locale.ENGLISH);
}
},
@ -111,7 +111,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* @since 1.4
*/
LOWER_CASE_WITH_DASHES() {
public String translateName(Field f) {
@Override public String translateName(Field f) {
return separateCamelCase(f.getName(), "-").toLowerCase(Locale.ENGLISH);
}
};
@ -166,4 +166,4 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
? firstCharacter + srcString.substring(indexOfSubstring)
: String.valueOf(firstCharacter);
}
}
}

View File

@ -125,16 +125,16 @@ public final class Gson {
final JsonDeserializationContext deserializationContext = new JsonDeserializationContext() {
@SuppressWarnings("unchecked")
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
@Override public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return (T) fromJson(json, typeOfT);
}
};
final JsonSerializationContext serializationContext = new JsonSerializationContext() {
public JsonElement serialize(Object src) {
@Override public JsonElement serialize(Object src) {
return toJsonTree(src);
}
public JsonElement serialize(Object src, Type typeOfSrc) {
@Override public JsonElement serialize(Object src, Type typeOfSrc) {
return toJsonTree(src, typeOfSrc);
}
};

View File

@ -32,7 +32,7 @@ public enum LongSerializationPolicy {
* {@code {"f":123}}.
*/
DEFAULT() {
public JsonElement serialize(Long value) {
@Override public JsonElement serialize(Long value) {
return new JsonPrimitive(value);
}
},
@ -43,7 +43,7 @@ public enum LongSerializationPolicy {
* {@code {"f":"123"}}.
*/
STRING() {
public JsonElement serialize(Long value) {
@Override public JsonElement serialize(Long value) {
return new JsonPrimitive(String.valueOf(value));
}
};

View File

@ -127,6 +127,7 @@ final class TreeTypeAdapter<T> extends TypeAdapter<T> {
}
@SuppressWarnings("unchecked") // guarded by typeToken.equals() call
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
boolean matches = exactType != null
? exactType.equals(type) || matchRawType && exactType.getType() == type.getRawType()

View File

@ -38,7 +38,7 @@ import com.google.gson.stream.JsonWriter;
public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
return null;
@ -60,7 +60,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
this.componentType = componentType;
}
public Object read(JsonReader in) throws IOException {
@Override public Object read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;

View File

@ -40,6 +40,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
this.constructorConstructor = constructorConstructor;
}
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
@ -69,7 +70,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
this.constructor = constructor;
}
public Collection<E> read(JsonReader in) throws IOException {
@Override public Collection<E> read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
@ -85,7 +86,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
return collection;
}
public void write(JsonWriter out, Collection<E> collection) throws IOException {
@Override public void write(JsonWriter out, Collection<E> collection) throws IOException {
if (collection == null) {
out.nullValue();
return;

View File

@ -41,7 +41,7 @@ import java.util.TimeZone;
public final class DateTypeAdapter extends TypeAdapter<Date> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.getRawType() == Date.class ? (TypeAdapter<T>) new DateTypeAdapter() : null;
}
};

View File

@ -38,6 +38,7 @@ public final class JsonAdapterAnnotationTypeAdapterFactory implements TypeAdapte
}
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
if (annotation == null) {

View File

@ -112,7 +112,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
this.complexMapKeySerialization = complexMapKeySerialization;
}
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
@ -157,7 +157,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
this.constructor = constructor;
}
public Map<K, V> read(JsonReader in) throws IOException {
@Override public Map<K, V> read(JsonReader in) throws IOException {
JsonToken peek = in.peek();
if (peek == JsonToken.NULL) {
in.nextNull();
@ -195,7 +195,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
return map;
}
public void write(JsonWriter out, Map<K, V> map) throws IOException {
@Override public void write(JsonWriter out, Map<K, V> map) throws IOException {
if (map == null) {
out.nullValue();
return;

View File

@ -37,7 +37,7 @@ import java.util.Map;
public final class ObjectTypeAdapter extends TypeAdapter<Object> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getRawType() == Object.class) {
return (TypeAdapter<T>) new ObjectTypeAdapter(gson);
}

View File

@ -86,7 +86,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
return fieldNames;
}
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
@Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
Class<? super T> raw = type.getRawType();
if (!Object.class.isAssignableFrom(raw)) {
@ -119,7 +119,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
field.set(value, fieldValue);
}
}
public boolean writeField(Object value) throws IOException, IllegalAccessException {
@Override public boolean writeField(Object value) throws IOException, IllegalAccessException {
if (!serialized) return false;
Object fieldValue = field.get(value);
return fieldValue != value; // avoid recursion for example for Throwable.cause

View File

@ -38,7 +38,7 @@ import java.text.SimpleDateFormat;
public final class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.getRawType() == java.sql.Date.class
? (TypeAdapter<T>) new SqlDateTypeAdapter() : null;
}

View File

@ -40,7 +40,7 @@ import java.util.Date;
public final class TimeTypeAdapter extends TypeAdapter<Time> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.getRawType() == Time.class ? (TypeAdapter<T>) new TimeTypeAdapter() : null;
}
};

View File

@ -83,7 +83,7 @@ public final class TypeAdapters {
public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS);
public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() {
public BitSet read(JsonReader in) throws IOException {
@Override public BitSet read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
@ -124,7 +124,7 @@ public final class TypeAdapters {
return bitset;
}
public void write(JsonWriter out, BitSet src) throws IOException {
@Override public void write(JsonWriter out, BitSet src) throws IOException {
if (src == null) {
out.nullValue();
return;
@ -516,7 +516,7 @@ public final class TypeAdapters {
public static final TypeAdapterFactory TIMESTAMP_FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Timestamp.class) {
return null;
}
@ -737,7 +737,7 @@ public final class TypeAdapters {
throw new AssertionError();
}
}
public T read(JsonReader in) throws IOException {
@Override public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
@ -745,14 +745,14 @@ public final class TypeAdapters {
return nameToConstant.get(in.nextString());
}
public void write(JsonWriter out, T value) throws IOException {
@Override public void write(JsonWriter out, T value) throws IOException {
out.value(value == null ? null : constantToName.get(value));
}
}
public static final TypeAdapterFactory ENUM_FACTORY = new TypeAdapterFactory() {
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
if (!Enum.class.isAssignableFrom(rawType) || rawType == Enum.class) {
return null;
@ -768,7 +768,7 @@ public final class TypeAdapters {
final TypeToken<TT> type, final TypeAdapter<TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.equals(type) ? (TypeAdapter<T>) typeAdapter : null;
}
};
@ -778,7 +778,7 @@ public final class TypeAdapters {
final Class<TT> type, final TypeAdapter<TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.getRawType() == type ? (TypeAdapter<T>) typeAdapter : null;
}
@Override public String toString() {
@ -791,7 +791,7 @@ public final class TypeAdapters {
final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == unboxed || rawType == boxed) ? (TypeAdapter<T>) typeAdapter : null;
}
@ -806,7 +806,7 @@ public final class TypeAdapters {
final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == base || rawType == sub) ? (TypeAdapter<T>) typeAdapter : null;
}
@ -825,7 +825,7 @@ public final class TypeAdapters {
final Class<T1> clazz, final TypeAdapter<T1> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
public <T2> TypeAdapter<T2> create(Gson gson, TypeToken<T2> typeToken) {
@Override public <T2> TypeAdapter<T2> create(Gson gson, TypeToken<T2> typeToken) {
final Class<? super T2> requestedType = typeToken.getRawType();
if (!clazz.isAssignableFrom(requestedType)) {
return null;

View File

@ -80,12 +80,11 @@ public class GsonTypeAdapterTest extends TestCase {
private static class ExceptionTypeAdapter
implements JsonSerializer<AtomicLong>, JsonDeserializer<AtomicLong> {
public JsonElement serialize(
@Override public JsonElement serialize(
AtomicLong src, Type typeOfSrc, JsonSerializationContext context) {
throw new IllegalStateException();
}
public AtomicLong deserialize(
@Override public AtomicLong deserialize(
JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
throw new IllegalStateException();
@ -94,11 +93,11 @@ public class GsonTypeAdapterTest extends TestCase {
private static class AtomicIntegerTypeAdapter
implements JsonSerializer<AtomicInteger>, JsonDeserializer<AtomicInteger> {
public JsonElement serialize(AtomicInteger src, Type typeOfSrc, JsonSerializationContext context) {
@Override public JsonElement serialize(AtomicInteger src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.incrementAndGet());
}
public AtomicInteger deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
@Override public AtomicInteger deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
int intValue = json.getAsInt();
return new AtomicInteger(--intValue);

View File

@ -31,10 +31,12 @@ final class MockExclusionStrategy implements ExclusionStrategy {
this.skipField = skipField;
}
@Override
public boolean shouldSkipField(FieldAttributes f) {
return skipField;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return skipClass;
}

View File

@ -118,12 +118,12 @@ public class ParameterizedTypeFixtures {
public MyParameterizedTypeInstanceCreator(T instanceOfT) {
this.instanceOfT = instanceOfT;
}
public MyParameterizedType<T> createInstance(Type type) {
@Override public MyParameterizedType<T> createInstance(Type type) {
return new MyParameterizedType<T>(instanceOfT);
}
}
public static class MyParameterizedTypeAdapter<T>
public static final class MyParameterizedTypeAdapter<T>
implements JsonSerializer<MyParameterizedType<T>>, JsonDeserializer<MyParameterizedType<T>> {
@SuppressWarnings("unchecked")
public static<T> String getExpectedJson(MyParameterizedType<T> obj) {
@ -142,7 +142,7 @@ public class ParameterizedTypeFixtures {
return sb.toString();
}
public JsonElement serialize(MyParameterizedType<T> src, Type classOfSrc,
@Override public JsonElement serialize(MyParameterizedType<T> src, Type classOfSrc,
JsonSerializationContext context) {
JsonObject json = new JsonObject();
T value = src.getValue();
@ -151,7 +151,7 @@ public class ParameterizedTypeFixtures {
}
@SuppressWarnings("unchecked")
public MyParameterizedType<T> deserialize(JsonElement json, Type typeOfT,
@Override public MyParameterizedType<T> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
Type genericClass = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
Class<?> rawType = $Gson$Types.getRawType(genericClass);

View File

@ -77,14 +77,16 @@ public class TestTypes {
public static class BaseSerializer implements JsonSerializer<Base> {
public static final String NAME = BaseSerializer.class.getSimpleName();
@Override
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty(Base.SERIALIZER_KEY, NAME);
return obj;
}
}
}
public static class SubSerializer implements JsonSerializer<Sub> {
public static final String NAME = SubSerializer.class.getSimpleName();
@Override
public JsonElement serialize(Sub src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty(Base.SERIALIZER_KEY, NAME);
@ -227,7 +229,7 @@ public class TestTypes {
}
public static class ClassWithNoFields {
// Nothing here.. .
// Nothing here..
@Override
public boolean equals(Object other) {
return other.getClass() == ClassWithNoFields.class;
@ -402,17 +404,18 @@ public class TestTypes {
return '{' + "\"fooBar\":" + f + ",\"Another Foo\":" + g + '}';
}
}
public static class CrazyLongTypeAdapter
implements JsonSerializer<Long>, JsonDeserializer<Long> {
public static final long DIFFERENCE = 5L;
@Override
public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src + DIFFERENCE);
}
@Override
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return json.getAsLong() - DIFFERENCE;
}
}
}
}

View File

@ -101,6 +101,7 @@ public class CustomDeserializerTest extends TestCase {
}
private static class DataHolderDeserializer implements JsonDeserializer<DataHolder> {
@Override
public DataHolder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObj = json.getAsJsonObject();
@ -112,7 +113,7 @@ public class CustomDeserializerTest extends TestCase {
public void testJsonTypeFieldBasedDeserialization() {
String json = "{field1:'abc',field2:'def',__type__:'SUB_TYPE1'}";
Gson gson = new GsonBuilder().registerTypeAdapter(MyBase.class, new JsonDeserializer<MyBase>() {
public MyBase deserialize(JsonElement json, Type pojoType,
@Override public MyBase deserialize(JsonElement json, Type pojoType,
JsonDeserializationContext context) throws JsonParseException {
String type = json.getAsJsonObject().get(MyBase.TYPE_ACCESS).getAsString();
return context.deserialize(json, SubTypes.valueOf(type).getSubclass());
@ -150,6 +151,7 @@ public class CustomDeserializerTest extends TestCase {
public void testCustomDeserializerReturnsNullForTopLevelObject() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
@Override
public Base deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
@ -163,6 +165,7 @@ public class CustomDeserializerTest extends TestCase {
public void testCustomDeserializerReturnsNull() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
@Override
public Base deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
@ -176,6 +179,7 @@ public class CustomDeserializerTest extends TestCase {
public void testCustomDeserializerReturnsNullForArrayElements() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
@Override
public Base deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
@ -190,6 +194,7 @@ public class CustomDeserializerTest extends TestCase {
public void testCustomDeserializerReturnsNullForArrayElementsForArrayField() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new JsonDeserializer<Base>() {
@Override
public Base deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return null;
@ -201,7 +206,7 @@ public class CustomDeserializerTest extends TestCase {
assertNull(target.bases[1]);
}
private static class ClassWithBaseArray {
private static final class ClassWithBaseArray {
Base[] bases;
}
}

View File

@ -57,8 +57,8 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomSerializers() {
Gson gson = builder.registerTypeAdapter(
ClassWithCustomTypeConverter.class, new JsonSerializer<ClassWithCustomTypeConverter>() {
public JsonElement serialize(ClassWithCustomTypeConverter src, Type typeOfSrc,
JsonSerializationContext context) {
@Override public JsonElement serialize(ClassWithCustomTypeConverter src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("bag", 5);
json.addProperty("value", 25);
@ -72,8 +72,8 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomDeserializers() {
Gson gson = new GsonBuilder().registerTypeAdapter(
ClassWithCustomTypeConverter.class, new JsonDeserializer<ClassWithCustomTypeConverter>() {
public ClassWithCustomTypeConverter deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) {
@Override public ClassWithCustomTypeConverter deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) {
JsonObject jsonObject = json.getAsJsonObject();
int value = jsonObject.get("bag").getAsInt();
return new ClassWithCustomTypeConverter(new BagOfPrimitives(value,
@ -109,7 +109,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomNestedSerializers() {
Gson gson = new GsonBuilder().registerTypeAdapter(
BagOfPrimitives.class, new JsonSerializer<BagOfPrimitives>() {
public JsonElement serialize(BagOfPrimitives src, Type typeOfSrc,
@Override public JsonElement serialize(BagOfPrimitives src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(6);
}
@ -121,7 +121,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomNestedDeserializers() {
Gson gson = new GsonBuilder().registerTypeAdapter(
BagOfPrimitives.class, new JsonDeserializer<BagOfPrimitives>() {
public BagOfPrimitives deserialize(JsonElement json, Type typeOfT,
@Override public BagOfPrimitives deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
int value = json.getAsInt();
return new BagOfPrimitives(value, value, false, "");
@ -134,6 +134,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomTypeAdapterDoesNotAppliesToSubClasses() {
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
@Override
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("value", src.baseValue);
@ -150,6 +151,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomTypeAdapterAppliesToSubClassesSerializedAsBaseClass() {
Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new JsonSerializer<Base> () {
@Override
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("value", src.baseValue);
@ -193,12 +195,14 @@ public class CustomTypeAdaptersTest extends TestCase {
}
}
public static class FooTypeAdapter implements JsonSerializer<Foo>, JsonDeserializer<Foo> {
public static final class FooTypeAdapter implements JsonSerializer<Foo>, JsonDeserializer<Foo> {
@Override
public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return context.deserialize(json, typeOfT);
}
@Override
public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src, typeOfSrc);
}
@ -207,7 +211,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomSerializerInvokedForPrimitives() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(boolean.class, new JsonSerializer<Boolean>() {
public JsonElement serialize(Boolean s, Type t, JsonSerializationContext c) {
@Override public JsonElement serialize(Boolean s, Type t, JsonSerializationContext c) {
return new JsonPrimitive(s ? 1 : 0);
}
})
@ -220,6 +224,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomDeserializerInvokedForPrimitives() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(boolean.class, new JsonDeserializer() {
@Override
public Object deserialize(JsonElement json, Type t, JsonDeserializationContext context) {
return json.getAsInt() != 0;
}
@ -231,6 +236,7 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomByteArraySerializer() {
Gson gson = new GsonBuilder().registerTypeAdapter(byte[].class, new JsonSerializer<byte[]>() {
@Override
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
StringBuilder sb = new StringBuilder(src.length);
for (byte b : src) {
@ -247,8 +253,8 @@ public class CustomTypeAdaptersTest extends TestCase {
public void testCustomByteArrayDeserializerAndInstanceCreator() {
GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(byte[].class,
new JsonDeserializer<byte[]>() {
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
@Override public byte[] deserialize(JsonElement json,
Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String str = json.getAsString();
byte[] data = new byte[str.length()];
for (int i = 0; i < data.length; ++i) {
@ -266,7 +272,7 @@ public class CustomTypeAdaptersTest extends TestCase {
}
}
private static class StringHolder {
private static final class StringHolder {
String part1;
String part2;
@ -284,17 +290,17 @@ public class CustomTypeAdaptersTest extends TestCase {
private static class StringHolderTypeAdapter implements JsonSerializer<StringHolder>,
JsonDeserializer<StringHolder>, InstanceCreator<StringHolder> {
public StringHolder createInstance(Type type) {
@Override public StringHolder createInstance(Type type) {
//Fill up with objects that will be thrown away
return new StringHolder("unknown:thing");
}
public StringHolder deserialize(JsonElement src, Type type,
@Override public StringHolder deserialize(JsonElement src, Type type,
JsonDeserializationContext context) {
return new StringHolder(src.getAsString());
}
public JsonElement serialize(StringHolder src, Type typeOfSrc,
@Override public JsonElement serialize(StringHolder src, Type typeOfSrc,
JsonSerializationContext context) {
String contents = src.part1 + ':' + src.part2;
return new JsonPrimitive(contents);
@ -423,6 +429,7 @@ public class CustomTypeAdaptersTest extends TestCase {
}
private static class DataHolderSerializer implements JsonSerializer<DataHolder> {
@Override
public JsonElement serialize(DataHolder src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty("myData", src.data);
@ -431,6 +438,7 @@ public class CustomTypeAdaptersTest extends TestCase {
}
private static class DataHolderDeserializer implements JsonDeserializer<DataHolder> {
@Override
public DataHolder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObj = json.getAsJsonObject();
@ -443,12 +451,13 @@ public class CustomTypeAdaptersTest extends TestCase {
}
private static class DateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return typeOfT == Date.class
? new Date(json.getAsLong())
: new java.sql.Date(json.getAsLong());
}
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}

View File

@ -39,6 +39,8 @@ public class DelegateTypeAdapterTest extends TestCase {
private StatsTypeAdapterFactory stats;
private Gson gson;
@Override
protected void setUp() throws Exception {
super.setUp();
stats = new StatsTypeAdapterFactory();
@ -72,7 +74,7 @@ public class DelegateTypeAdapterTest extends TestCase {
public int numReads = 0;
public int numWrites = 0;
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
@Override

View File

@ -182,11 +182,11 @@ public class EnumTest extends TestCase {
private static class MyEnumTypeAdapter
implements JsonSerializer<Roshambo>, JsonDeserializer<Roshambo> {
public JsonElement serialize(Roshambo src, Type typeOfSrc, JsonSerializationContext context) {
@Override public JsonElement serialize(Roshambo src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("123" + src.name());
}
public Roshambo deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context)
@Override public Roshambo deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context)
throws JsonParseException {
return Roshambo.valueOf(json.getAsString().substring(3));
}

View File

@ -37,10 +37,10 @@ import junit.framework.TestCase;
*/
public class ExclusionStrategyFunctionalTest extends TestCase {
private static final ExclusionStrategy EXCLUDE_SAMPLE_OBJECT_FOR_TEST = new ExclusionStrategy() {
public boolean shouldSkipField(FieldAttributes f) {
@Override public boolean shouldSkipField(FieldAttributes f) {
return false;
}
public boolean shouldSkipClass(Class<?> clazz) {
@Override public boolean shouldSkipClass(Class<?> clazz) {
return clazz == SampleObjectForTest.class;
}
};
@ -184,18 +184,18 @@ public class ExclusionStrategyFunctionalTest extends TestCase {
}
}
private static class MyExclusionStrategy implements ExclusionStrategy {
private static final class MyExclusionStrategy implements ExclusionStrategy {
private final Class<?> typeToSkip;
private MyExclusionStrategy(Class<?> typeToSkip) {
this.typeToSkip = typeToSkip;
}
public boolean shouldSkipClass(Class<?> clazz) {
@Override public boolean shouldSkipClass(Class<?> clazz) {
return (clazz == typeToSkip);
}
public boolean shouldSkipField(FieldAttributes f) {
@Override public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(Foo.class) != null;
}
}

View File

@ -153,7 +153,7 @@ public class ExposeFieldsTest extends TestCase {
}
private static class SomeInterfaceInstanceCreator implements InstanceCreator<SomeInterface> {
public SomeInterface createInstance(Type type) {
@Override public SomeInterface createInstance(Type type) {
return new SomeObject();
}
}

View File

@ -43,7 +43,7 @@ public class InstanceCreatorTest extends TestCase {
public void testInstanceCreatorReturnsBaseType() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
public Base createInstance(Type type) {
@Override public Base createInstance(Type type) {
return new Base();
}
})
@ -56,7 +56,7 @@ public class InstanceCreatorTest extends TestCase {
public void testInstanceCreatorReturnsSubTypeForTopLevelObject() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
public Base createInstance(Type type) {
@Override public Base createInstance(Type type) {
return new Sub();
}
})
@ -74,7 +74,7 @@ public class InstanceCreatorTest extends TestCase {
public void testInstanceCreatorReturnsSubTypeForField() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Base.class, new InstanceCreator<Base>() {
public Base createInstance(Type type) {
@Override public Base createInstance(Type type) {
return new Sub();
}
})
@ -90,7 +90,7 @@ public class InstanceCreatorTest extends TestCase {
@SuppressWarnings("serial")
class SubArrayList<T> extends ArrayList<T> {}
InstanceCreator<List<String>> listCreator = new InstanceCreator<List<String>>() {
public List<String> createInstance(Type type) {
@Override public List<String> createInstance(Type type) {
return new SubArrayList<String>();
}
};
@ -107,7 +107,7 @@ public class InstanceCreatorTest extends TestCase {
@SuppressWarnings("serial")
class SubTreeSet<T> extends TreeSet<T> {}
InstanceCreator<SortedSet> sortedSetCreator = new InstanceCreator<SortedSet>() {
public SortedSet createInstance(Type type) {
@Override public SortedSet createInstance(Type type) {
return new SubTreeSet();
}
};

View File

@ -157,7 +157,7 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
this.value = value;
}
static final class JsonAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
@Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
return new TypeAdapter<T>() {
@Override public void write(JsonWriter out, T value) throws IOException {
out.value("jsonAdapterFactory");

View File

@ -116,7 +116,7 @@ public final class JsonAdapterAnnotationOnFieldsTest extends TestCase {
}
private static class GizmoPartTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
@Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
return new TypeAdapter<T>() {
@Override public void write(JsonWriter out, T value) throws IOException {
out.value("GizmoPartTypeAdapterFactory");

View File

@ -149,11 +149,12 @@ public class NamingPolicyTest extends TestCase {
assertEquals("{\"@foo\":\"bar\"}", new Gson().toJson(new AtName()));
}
static class AtName {
static final class AtName {
@SerializedName("@foo") String f = "bar";
}
private static class UpperCaseNamingStrategy implements FieldNamingStrategy {
private static final class UpperCaseNamingStrategy implements FieldNamingStrategy {
@Override
public String translateName(Field f) {
return f.getName().toUpperCase();
}

View File

@ -192,7 +192,7 @@ public class NullObjectAndFieldTest extends TestCase {
}
private static class ClassWithObjectsSerializer implements JsonSerializer<ClassWithObjects> {
public JsonElement serialize(ClassWithObjects src, Type typeOfSrc,
@Override public JsonElement serialize(ClassWithObjects src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.add("bag", JsonNull.INSTANCE);
@ -210,7 +210,7 @@ public class NullObjectAndFieldTest extends TestCase {
public void testCustomTypeAdapterPassesNullSerialization() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectWithField.class, new JsonSerializer<ObjectWithField>() {
public JsonElement serialize(ObjectWithField src, Type typeOfSrc,
@Override public JsonElement serialize(ObjectWithField src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(null);
}
@ -224,7 +224,7 @@ public class NullObjectAndFieldTest extends TestCase {
public void testCustomTypeAdapterPassesNullDesrialization() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ObjectWithField.class, new JsonDeserializer<ObjectWithField>() {
public ObjectWithField deserialize(JsonElement json, Type type,
@Override public ObjectWithField deserialize(JsonElement json, Type type,
JsonDeserializationContext context) {
return context.deserialize(null, type);
}

View File

@ -146,7 +146,7 @@ public final class RuntimeTypeAdapterFactoryFunctionalTest extends TestCase {
return registerSubtype(type, type.getSimpleName());
}
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
@Override public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type.getRawType() != baseType) {
return null;
}

View File

@ -94,6 +94,7 @@ public class TreeTypeAdaptersTest extends TestCase {
JsonDeserializer<Id<?>> {
@SuppressWarnings("rawtypes")
@Override
public Id<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (!(typeOfT instanceof ParameterizedType)) {
@ -106,6 +107,7 @@ public class TreeTypeAdaptersTest extends TestCase {
return new Id(json.getAsString(), typeOfId);
}
@Override
public JsonElement serialize(Id<?> src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getValue());
}

View File

@ -122,6 +122,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
private JsonSerializer<Foo> newSerializer(final String name) {
return new JsonSerializer<Foo>() {
@Override
public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.name + " via " + name);
}
@ -130,6 +131,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
private JsonDeserializer<Foo> newDeserializer(final String name) {
return new JsonDeserializer<Foo>() {
@Override
public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return new Foo(json.getAsString() + " via " + name);
}

View File

@ -140,18 +140,18 @@ public final class TypeHierarchyAdapterTest extends TestCase {
}
static class ManagerAdapter implements JsonSerializer<Manager>, JsonDeserializer<Manager> {
public Manager deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
@Override public Manager deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
Manager result = new Manager();
result.userid = json.getAsString();
return result;
}
public JsonElement serialize(Manager src, Type typeOfSrc, JsonSerializationContext context) {
@Override public JsonElement serialize(Manager src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.userid);
}
}
static class EmployeeAdapter implements JsonSerializer<Employee>, JsonDeserializer<Employee> {
public JsonElement serialize(Employee employee, Type typeOfSrc,
@Override public JsonElement serialize(Employee employee, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("userid", context.serialize(employee.userid, String.class));
@ -165,7 +165,7 @@ public final class TypeHierarchyAdapterTest extends TestCase {
return result;
}
public Employee deserialize(JsonElement json, Type typeOfT,
@Override public Employee deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject object = json.getAsJsonObject();
Employee result = null;

View File

@ -118,7 +118,7 @@ public class UncategorizedTest extends TestCase {
Derived2() { opType = OperationType.OP2; }
}
private static class BaseTypeAdapter implements JsonDeserializer<Base> {
public Base deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
@Override public Base deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
String opTypeStr = json.getAsJsonObject().get("opType").getAsString();
OperationType opType = OperationType.valueOf(opTypeStr);