From 3b0f8f4340c68d8fde0840befbc1b89afa14933f Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Wed, 23 Sep 2009 17:45:16 +0000 Subject: [PATCH] Removed all the JDK warnings about unused fields in test classes or unused constructors for use by Gson or instanceof calls on parameterized types. --- gson/src/main/java/com/google/gson/GsonBuilder.java | 10 +++++----- .../src/main/java/com/google/gson/TypeInfoFactory.java | 6 +++--- gson/src/main/java/com/google/gson/TypeInfoMap.java | 2 +- gson/src/main/java/com/google/gson/TypeUtils.java | 6 +++--- .../main/java/com/google/gson/reflect/TypeToken.java | 10 +++++----- .../ExposeAnnotationBasedExclusionStrategyTest.java | 1 + ...izedNameAnnotationInterceptingNamingPolicyTest.java | 1 + .../test/java/com/google/gson/TypeInfoFactoryTest.java | 1 + .../com/google/gson/VersionExclusionStrategyTest.java | 1 + .../com/google/gson/functional/CollectionTest.java | 6 ++++-- .../com/google/gson/functional/ConcurrencyTest.java | 3 ++- .../google/gson/functional/CustomDeserializerTest.java | 8 ++++++-- .../google/gson/functional/CustomTypeAdaptersTest.java | 1 + .../gson/functional/DefaultTypeAdaptersTest.java | 8 ++++++-- .../com/google/gson/functional/ExposeFieldsTest.java | 8 ++++++-- .../java/com/google/gson/functional/JsonTreeTest.java | 1 + .../test/java/com/google/gson/functional/MapTest.java | 2 ++ .../com/google/gson/functional/NamingPolicyTest.java | 1 + .../google/gson/functional/NullObjectAndFieldTest.java | 1 + .../java/com/google/gson/functional/ObjectTest.java | 9 +++++++-- .../google/gson/functional/ParameterizedTypesTest.java | 10 ++++++++-- .../java/com/google/gson/metrics/PerformanceTest.java | 9 +++++++-- 22 files changed, 73 insertions(+), 32 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 61b6f123..0a10b53c 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -320,15 +320,15 @@ public final class GsonBuilder { * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { - Preconditions.checkArgument(typeAdapter instanceof JsonSerializer - || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator); - if (typeAdapter instanceof InstanceCreator) { + Preconditions.checkArgument(typeAdapter instanceof JsonSerializer + || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator); + if (typeAdapter instanceof InstanceCreator) { registerInstanceCreator(type, (InstanceCreator) typeAdapter); } - if (typeAdapter instanceof JsonSerializer) { + if (typeAdapter instanceof JsonSerializer) { registerSerializer(type, (JsonSerializer) typeAdapter); } - if (typeAdapter instanceof JsonDeserializer) { + if (typeAdapter instanceof JsonDeserializer) { registerDeserializer(type, (JsonDeserializer) typeAdapter); } return this; diff --git a/gson/src/main/java/com/google/gson/TypeInfoFactory.java b/gson/src/main/java/com/google/gson/TypeInfoFactory.java index deb88a2f..1be0fac1 100644 --- a/gson/src/main/java/com/google/gson/TypeInfoFactory.java +++ b/gson/src/main/java/com/google/gson/TypeInfoFactory.java @@ -57,7 +57,7 @@ final class TypeInfoFactory { private static Type getActualType( Type typeToEvaluate, Type parentType, Class rawParentClass) { - if (typeToEvaluate instanceof Class) { + if (typeToEvaluate instanceof Class) { return typeToEvaluate; } else if (typeToEvaluate instanceof ParameterizedType) { ParameterizedType castedType = (ParameterizedType) typeToEvaluate; @@ -73,13 +73,13 @@ final class TypeInfoFactory { if (componentType.equals(actualType)) { return castedType; } else { - if (actualType instanceof Class) { + if (actualType instanceof Class) { return TypeUtils.wrapWithArray(TypeUtils.toRawClass(actualType)); } else { return new GenericArrayTypeImpl(actualType); } } - } else if (typeToEvaluate instanceof TypeVariable) { + } else if (typeToEvaluate instanceof TypeVariable) { if (parentType instanceof ParameterizedType) { // The class definition has the actual types used for the type variables. // Find the matching actual type for the Type Variable used for the field. diff --git a/gson/src/main/java/com/google/gson/TypeInfoMap.java b/gson/src/main/java/com/google/gson/TypeInfoMap.java index ea8eb07b..12fb1077 100644 --- a/gson/src/main/java/com/google/gson/TypeInfoMap.java +++ b/gson/src/main/java/com/google/gson/TypeInfoMap.java @@ -32,7 +32,7 @@ final class TypeInfoMap { private final Type valueType; public TypeInfoMap(Type mapType) { - if (mapType instanceof Class && Properties.class.isAssignableFrom((Class) mapType)) { + if (mapType instanceof Class && Properties.class.isAssignableFrom((Class) mapType)) { keyType = String.class; valueType = String.class; } else if (mapType instanceof ParameterizedType) { diff --git a/gson/src/main/java/com/google/gson/TypeUtils.java b/gson/src/main/java/com/google/gson/TypeUtils.java index acda71a6..701cf8f4 100644 --- a/gson/src/main/java/com/google/gson/TypeUtils.java +++ b/gson/src/main/java/com/google/gson/TypeUtils.java @@ -41,7 +41,7 @@ final class TypeUtils { * TypeUtils.getActualTypeForFirstTypeVariable(fooType) will return Integer.class. */ static Type getActualTypeForFirstTypeVariable(Type type) { - if (type instanceof Class) { + if (type instanceof Class) { return Object.class; } else if (type instanceof ParameterizedType) { return ((ParameterizedType)type).getActualTypeArguments()[0]; @@ -54,7 +54,7 @@ final class TypeUtils { } static boolean isArray(Type type) { - if (type instanceof Class) { + if (type instanceof Class) { return ((Class)type).isArray(); } else if (type instanceof GenericArrayType) { return true; @@ -67,7 +67,7 @@ final class TypeUtils { * This method returns the actual raw class associated with the specified type. */ static Class toRawClass(Type type) { - if (type instanceof Class) { + if (type instanceof Class) { return (Class) type; } else if (type instanceof ParameterizedType) { ParameterizedType actualType = (ParameterizedType)type; diff --git a/gson/src/main/java/com/google/gson/reflect/TypeToken.java b/gson/src/main/java/com/google/gson/reflect/TypeToken.java index c9eef2f1..c044d672 100644 --- a/gson/src/main/java/com/google/gson/reflect/TypeToken.java +++ b/gson/src/main/java/com/google/gson/reflect/TypeToken.java @@ -87,7 +87,7 @@ public abstract class TypeToken { */ static Type getSuperclassTypeParameter(Class subclass) { Type superclass = subclass.getGenericSuperclass(); - if (superclass instanceof Class) { + if (superclass instanceof Class) { throw new RuntimeException("Missing type parameter."); } return ((ParameterizedType) superclass).getActualTypeArguments()[0]; @@ -163,7 +163,7 @@ public abstract class TypeToken { return true; } - if (type instanceof Class) { + if (type instanceof Class) { return rawType.isAssignableFrom(getRawType(from)); } else if (type instanceof ParameterizedType) { return isAssignableFrom(from, (ParameterizedType) type, @@ -194,7 +194,7 @@ public abstract class TypeToken { Type t = from; if (from instanceof GenericArrayType) { t = ((GenericArrayType) from).getGenericComponentType(); - } else if (from instanceof Class) { + } else if (from instanceof Class) { Class classType = (Class) from; while (classType.isArray()) { classType = classType.getComponentType(); @@ -238,7 +238,7 @@ public abstract class TypeToken { for (int i = 0; i < tArgs.length; i++) { Type arg = tArgs[i]; TypeVariable var = tParams[i]; - while (arg instanceof TypeVariable) { + while (arg instanceof TypeVariable) { TypeVariable v = (TypeVariable) arg; arg = typeVarMap.get(v.getName()); } @@ -293,7 +293,7 @@ public abstract class TypeToken { Map typeMap) { if (to.equals(from)) return true; - if (from instanceof TypeVariable) { + if (from instanceof TypeVariable) { return to.equals(typeMap.get(((TypeVariable)from).getName())); } diff --git a/gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java b/gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java index 556d7506..1f63f95e 100644 --- a/gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java +++ b/gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java @@ -51,6 +51,7 @@ public class ExposeAnnotationBasedExclusionStrategyTest extends TestCase { assertFalse(strategy.shouldSkipField(f)); } + @SuppressWarnings("unused") private static class MockObject { @Expose public final int exposedField = 0; diff --git a/gson/src/test/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicyTest.java b/gson/src/test/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicyTest.java index bb9cf92b..a0144a58 100644 --- a/gson/src/test/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicyTest.java +++ b/gson/src/test/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicyTest.java @@ -53,6 +53,7 @@ public class SerializedNameAnnotationInterceptingNamingPolicyTest extends TestCa assertEquals(fieldName, policy.translateName(f)); } + @SuppressWarnings("unused") private static class SomeObject { @SerializedName(ANNOTATED_FIELD_NAME) public final int fieldWithAnnotation = 1; public final int fieldWithoutAnnotation = 1; diff --git a/gson/src/test/java/com/google/gson/TypeInfoFactoryTest.java b/gson/src/test/java/com/google/gson/TypeInfoFactoryTest.java index 913154b8..26155307 100644 --- a/gson/src/test/java/com/google/gson/TypeInfoFactoryTest.java +++ b/gson/src/test/java/com/google/gson/TypeInfoFactoryTest.java @@ -223,6 +223,7 @@ public class TypeInfoFactoryTest extends TestCase { assertEquals(List[].class, typeInfo.getRawClass()); } + @SuppressWarnings("unused") private static class ObjectWithDifferentFields { public static enum TestEnum { TEST_1, TEST_2; diff --git a/gson/src/test/java/com/google/gson/VersionExclusionStrategyTest.java b/gson/src/test/java/com/google/gson/VersionExclusionStrategyTest.java index f023b73f..cca213bc 100644 --- a/gson/src/test/java/com/google/gson/VersionExclusionStrategyTest.java +++ b/gson/src/test/java/com/google/gson/VersionExclusionStrategyTest.java @@ -67,6 +67,7 @@ public class VersionExclusionStrategyTest extends TestCase { @Since(VERSION) private static class MockObject { + @SuppressWarnings("unused") @Since(VERSION) public final int someField = 0; } diff --git a/gson/src/test/java/com/google/gson/functional/CollectionTest.java b/gson/src/test/java/com/google/gson/functional/CollectionTest.java index ef625121..d43d4ce2 100644 --- a/gson/src/test/java/com/google/gson/functional/CollectionTest.java +++ b/gson/src/test/java/com/google/gson/functional/CollectionTest.java @@ -283,7 +283,8 @@ public class CollectionTest extends TestCase { private static class ObjectWithWildcardCollection { private final Collection collection; - @SuppressWarnings("unchecked") + // For use by Gson + @SuppressWarnings({ "unchecked", "unused" }) public ObjectWithWildcardCollection() { this(Collections.EMPTY_LIST); } @@ -300,7 +301,8 @@ public class CollectionTest extends TestCase { private static class Entry { int value; // For use by Gson - Entry() { + @SuppressWarnings("unused") + private Entry() { this(10); } Entry(int value) { diff --git a/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java b/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java index 91a784bc..2dccf4b6 100755 --- a/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java +++ b/gson/src/test/java/com/google/gson/functional/ConcurrencyTest.java @@ -120,7 +120,8 @@ public class ConcurrencyTest extends TestCase { finishedLatch.await(); assertFalse(failed.get()); } - + + @SuppressWarnings("unused") private static class MyObject { String a; String b; diff --git a/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java b/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java index a3f337e1..e5ace902 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomDeserializerTest.java @@ -65,7 +65,9 @@ public class CustomDeserializerTest extends TestCase { private static class DataHolder { private final String data; - public DataHolder() { + // For use by Gson + @SuppressWarnings("unused") + private DataHolder() { throw new IllegalStateException(); } @@ -81,7 +83,9 @@ public class CustomDeserializerTest extends TestCase { private static class DataHolderWrapper { private final DataHolder wrappedData; - public DataHolderWrapper() { + // For use by Gson + @SuppressWarnings("unused") + private DataHolderWrapper() { this(new DataHolder(DEFAULT_VALUE)); } diff --git a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java index 63b0a101..526a42c5 100644 --- a/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/CustomTypeAdaptersTest.java @@ -169,6 +169,7 @@ public class CustomTypeAdaptersTest extends TestCase { } private static class Derived extends Base { + @SuppressWarnings("unused") int derivedValue = 3; } diff --git a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java index 88ca359d..1a6e825a 100644 --- a/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java +++ b/gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java @@ -289,7 +289,9 @@ public class DefaultTypeAdaptersTest extends TestCase { private static class ClassWithBigDecimal { BigDecimal value; - ClassWithBigDecimal() { } + // For use by Gson + @SuppressWarnings("unused") + private ClassWithBigDecimal() { } ClassWithBigDecimal(String value) { this.value = new BigDecimal(value); } @@ -300,7 +302,9 @@ public class DefaultTypeAdaptersTest extends TestCase { private static class ClassWithBigInteger { BigInteger value; - ClassWithBigInteger() { } + // For use by Gson + @SuppressWarnings("unused") + private ClassWithBigInteger() { } ClassWithBigInteger(String value) { this.value = new BigInteger(value); } diff --git a/gson/src/test/java/com/google/gson/functional/ExposeFieldsTest.java b/gson/src/test/java/com/google/gson/functional/ExposeFieldsTest.java index 96a89f7b..a9ef78a3 100644 --- a/gson/src/test/java/com/google/gson/functional/ExposeFieldsTest.java +++ b/gson/src/test/java/com/google/gson/functional/ExposeFieldsTest.java @@ -110,6 +110,7 @@ public class ExposeFieldsTest extends TestCase { assertNotNull(obj.interfaceField); } + @SuppressWarnings("unused") private static class ClassWithExposedFields { @Expose private final Integer a; private final Integer b; @@ -117,7 +118,8 @@ public class ExposeFieldsTest extends TestCase { @Expose(deserialize = false) final double d; @Expose(serialize = false, deserialize = false) final char e; - ClassWithExposedFields() { + // For use by Gson + private ClassWithExposedFields() { this(null, null); } @@ -166,7 +168,9 @@ public class ExposeFieldsTest extends TestCase { @Expose private final SomeInterface interfaceField; - public ClassWithInterfaceField() { + // For use by Gson + @SuppressWarnings("unused") + private ClassWithInterfaceField() { this(null); } diff --git a/gson/src/test/java/com/google/gson/functional/JsonTreeTest.java b/gson/src/test/java/com/google/gson/functional/JsonTreeTest.java index 85a6bd5e..195f6f99 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonTreeTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonTreeTest.java @@ -75,6 +75,7 @@ public class JsonTreeTest extends TestCase { } private static class SubTypeOfBagOfPrimitives extends BagOfPrimitives { + @SuppressWarnings("unused") float f = 1.2F; public SubTypeOfBagOfPrimitives(long l, int i, boolean b, String string, float f) { super(l, i, b, string); diff --git a/gson/src/test/java/com/google/gson/functional/MapTest.java b/gson/src/test/java/com/google/gson/functional/MapTest.java index 895602b8..6e084e0a 100755 --- a/gson/src/test/java/com/google/gson/functional/MapTest.java +++ b/gson/src/test/java/com/google/gson/functional/MapTest.java @@ -172,6 +172,7 @@ public class MapTest extends TestCase { private static class MyParameterizedMap extends LinkedHashMap { private static final long serialVersionUID = 1L; + @SuppressWarnings("unused") int foo = 10; } @@ -248,6 +249,7 @@ public class MapTest extends TestCase { private static class MyMap extends LinkedHashMap { private static final long serialVersionUID = 1L; + @SuppressWarnings("unused") int foo = 10; } diff --git a/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java b/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java index ef8a651a..7b1f9bb7 100644 --- a/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java +++ b/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java @@ -82,6 +82,7 @@ public class NamingPolicyTest extends TestCase { assertEquals("{\"a\":3.0}", actual); } + @SuppressWarnings("unused") private static class ClassWithDuplicateFields { public Integer a; @SerializedName("a") public Double b; diff --git a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java index f2b94f97..fe9f1ffe 100755 --- a/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java +++ b/gson/src/test/java/com/google/gson/functional/NullObjectAndFieldTest.java @@ -151,6 +151,7 @@ public class NullObjectAndFieldTest extends TestCase { private Long value; } + @SuppressWarnings("unused") private static class ClassWithMembers { String str; int[] array; diff --git a/gson/src/test/java/com/google/gson/functional/ObjectTest.java b/gson/src/test/java/com/google/gson/functional/ObjectTest.java index 170beeca..78ddfee9 100644 --- a/gson/src/test/java/com/google/gson/functional/ObjectTest.java +++ b/gson/src/test/java/com/google/gson/functional/ObjectTest.java @@ -366,6 +366,7 @@ public class ObjectTest extends TestCase { } private static class ClassWithObjectField { + @SuppressWarnings("unused") Object member; } @@ -391,6 +392,7 @@ public class ObjectTest extends TestCase { } private static class Parent { + @SuppressWarnings("unused") int value1 = 1; private class Child { int value2 = 2; @@ -404,7 +406,8 @@ public class ObjectTest extends TestCase { private SortedSet sortedSet; // For use by Gson - ClassWithSubInterfacesOfCollection() { + @SuppressWarnings("unused") + private ClassWithSubInterfacesOfCollection() { } public ClassWithSubInterfacesOfCollection(List list, Queue queue, Set set, @@ -496,7 +499,9 @@ public class ObjectTest extends TestCase { private static class SubTypeOfNested extends Nested { private final long value = 5; - public SubTypeOfNested() { + // Used by Gson + @SuppressWarnings("unused") + private SubTypeOfNested() { this(null, null); } diff --git a/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java b/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java index f59f3136..9c12ebd3 100644 --- a/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java +++ b/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java @@ -271,7 +271,9 @@ public class ParameterizedTypesTest extends TestCase { private final List listOfWildcardTypeParameters; private final List[] arrayOfListOfWildcardTypeParameters; - public ObjectWithTypeVariables() { + // For use by Gson + @SuppressWarnings("unused") + private ObjectWithTypeVariables() { this(null, null, null, null, null, null); } @@ -386,7 +388,9 @@ public class ParameterizedTypesTest extends TestCase { C c; D d; E e; - MultiParameters() { + // For use by Gson + @SuppressWarnings("unused") + private MultiParameters() { } MultiParameters(A a, B b, C c, D d, E e) { super(); @@ -461,9 +465,11 @@ public class ParameterizedTypesTest extends TestCase { // Begin: tests to reproduce issue 103 private static class Quantity { + @SuppressWarnings("unused") int q = 10; } private static class MyQuantity extends Quantity { + @SuppressWarnings("unused") int q2 = 20; } private interface Measurable { diff --git a/gson/src/test/java/com/google/gson/metrics/PerformanceTest.java b/gson/src/test/java/com/google/gson/metrics/PerformanceTest.java index 6595d1e0..6fb0f7f7 100644 --- a/gson/src/test/java/com/google/gson/metrics/PerformanceTest.java +++ b/gson/src/test/java/com/google/gson/metrics/PerformanceTest.java @@ -72,7 +72,10 @@ public class PerformanceTest extends TestCase { private static class ExceptionHolder { public final String message; public final String stackTrace; - public ExceptionHolder() { + + // For use by Gson + @SuppressWarnings("unused") + private ExceptionHolder() { this("", ""); } public ExceptionHolder(String message, String stackTrace) { @@ -81,11 +84,13 @@ public class PerformanceTest extends TestCase { } } + @SuppressWarnings("unused") private static class CollectionEntry { final String name; final String value; - CollectionEntry() { + // For use by Gson + private CollectionEntry() { this(null, null); }