updated minimum JDK version to 1.6.

Added Overrides for methods implementing an interface.
This commit is contained in:
Inderjeet Singh 2015-11-04 18:52:20 -08:00
parent 55b4a3f9b0
commit e5b3f6368d
36 changed files with 126 additions and 96 deletions

View File

@ -113,8 +113,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version> <version>3.3</version>
<configuration> <configuration>
<source>1.5</source> <source>1.6</source>
<target>1.5</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

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 // These methods need to be synchronized since JDK DateFormat classes are not thread-safe
// See issue 162 // See issue 162
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
synchronized (localFormat) { synchronized (localFormat) {
String dateFormatAsString = enUsFormat.format(src); 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) public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException { throws JsonParseException {
if (!(json instanceof JsonPrimitive)) { if (!(json instanceof JsonPrimitive)) {

View File

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

View File

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

View File

@ -32,7 +32,7 @@ public enum LongSerializationPolicy {
* {@code {"f":123}}. * {@code {"f":123}}.
*/ */
DEFAULT() { DEFAULT() {
public JsonElement serialize(Long value) { @Override public JsonElement serialize(Long value) {
return new JsonPrimitive(value); return new JsonPrimitive(value);
} }
}, },
@ -43,7 +43,7 @@ public enum LongSerializationPolicy {
* {@code {"f":"123"}}. * {@code {"f":"123"}}.
*/ */
STRING() { STRING() {
public JsonElement serialize(Long value) { @Override public JsonElement serialize(Long value) {
return new JsonPrimitive(String.valueOf(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 @SuppressWarnings("unchecked") // guarded by typeToken.equals() call
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
boolean matches = exactType != null boolean matches = exactType != null
? exactType.equals(type) || matchRawType && exactType.getType() == type.getRawType() ? 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 final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings({"unchecked", "rawtypes"}) @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(); Type type = typeToken.getType();
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) { if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
return null; return null;
@ -60,7 +60,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
this.componentType = componentType; this.componentType = componentType;
} }
public Object read(JsonReader in) throws IOException { @Override public Object read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) { if (in.peek() == JsonToken.NULL) {
in.nextNull(); in.nextNull();
return null; return null;

View File

@ -40,6 +40,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
this.constructorConstructor = constructorConstructor; this.constructorConstructor = constructorConstructor;
} }
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType(); Type type = typeToken.getType();
@ -69,7 +70,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
this.constructor = constructor; 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) { if (in.peek() == JsonToken.NULL) {
in.nextNull(); in.nextNull();
return null; return null;
@ -85,7 +86,7 @@ public final class CollectionTypeAdapterFactory implements TypeAdapterFactory {
return collection; 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) { if (collection == null) {
out.nullValue(); out.nullValue();
return; return;

View File

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

View File

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

View File

@ -112,7 +112,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
this.complexMapKeySerialization = complexMapKeySerialization; 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(); Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType(); Class<? super T> rawType = typeToken.getRawType();
@ -157,7 +157,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
this.constructor = constructor; 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(); JsonToken peek = in.peek();
if (peek == JsonToken.NULL) { if (peek == JsonToken.NULL) {
in.nextNull(); in.nextNull();
@ -195,7 +195,7 @@ public final class MapTypeAdapterFactory implements TypeAdapterFactory {
return map; 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) { if (map == null) {
out.nullValue(); out.nullValue();
return; return;

View File

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

View File

@ -86,7 +86,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
return fieldNames; 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(); Class<? super T> raw = type.getRawType();
if (!Object.class.isAssignableFrom(raw)) { if (!Object.class.isAssignableFrom(raw)) {
@ -119,7 +119,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
field.set(value, fieldValue); 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; if (!serialized) return false;
Object fieldValue = field.get(value); Object fieldValue = field.get(value);
return fieldValue != value; // avoid recursion for example for Throwable.cause 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 final class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal @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 return typeToken.getRawType() == java.sql.Date.class
? (TypeAdapter<T>) new SqlDateTypeAdapter() : null; ? (TypeAdapter<T>) new SqlDateTypeAdapter() : null;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -182,11 +182,11 @@ public class EnumTest extends TestCase {
private static class MyEnumTypeAdapter private static class MyEnumTypeAdapter
implements JsonSerializer<Roshambo>, JsonDeserializer<Roshambo> { 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()); 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 { throws JsonParseException {
return Roshambo.valueOf(json.getAsString().substring(3)); return Roshambo.valueOf(json.getAsString().substring(3));
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -146,7 +146,7 @@ public final class RuntimeTypeAdapterFactoryFunctionalTest extends TestCase {
return registerSubtype(type, type.getSimpleName()); 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) { if (type.getRawType() != baseType) {
return null; return null;
} }

View File

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

View File

@ -122,6 +122,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
private JsonSerializer<Foo> newSerializer(final String name) { private JsonSerializer<Foo> newSerializer(final String name) {
return new JsonSerializer<Foo>() { return new JsonSerializer<Foo>() {
@Override
public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.name + " via " + name); return new JsonPrimitive(src.name + " via " + name);
} }
@ -130,6 +131,7 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
private JsonDeserializer<Foo> newDeserializer(final String name) { private JsonDeserializer<Foo> newDeserializer(final String name) {
return new JsonDeserializer<Foo>() { return new JsonDeserializer<Foo>() {
@Override
public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
return new Foo(json.getAsString() + " via " + name); 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> { 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(); Manager result = new Manager();
result.userid = json.getAsString(); result.userid = json.getAsString();
return result; 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); return new JsonPrimitive(src.userid);
} }
} }
static class EmployeeAdapter implements JsonSerializer<Employee>, JsonDeserializer<Employee> { 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) { JsonSerializationContext context) {
JsonObject result = new JsonObject(); JsonObject result = new JsonObject();
result.add("userid", context.serialize(employee.userid, String.class)); result.add("userid", context.serialize(employee.userid, String.class));
@ -165,7 +165,7 @@ public final class TypeHierarchyAdapterTest extends TestCase {
return result; return result;
} }
public Employee deserialize(JsonElement json, Type typeOfT, @Override public Employee deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException { JsonDeserializationContext context) throws JsonParseException {
JsonObject object = json.getAsJsonObject(); JsonObject object = json.getAsJsonObject();
Employee result = null; Employee result = null;

View File

@ -118,7 +118,7 @@ public class UncategorizedTest extends TestCase {
Derived2() { opType = OperationType.OP2; } Derived2() { opType = OperationType.OP2; }
} }
private static class BaseTypeAdapter implements JsonDeserializer<Base> { 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 { throws JsonParseException {
String opTypeStr = json.getAsJsonObject().get("opType").getAsString(); String opTypeStr = json.getAsJsonObject().get("opType").getAsString();
OperationType opType = OperationType.valueOf(opTypeStr); OperationType opType = OperationType.valueOf(opTypeStr);