Removed all the JDK warnings about unused fields in test classes or unused constructors for use by Gson or instanceof calls on parameterized types.

This commit is contained in:
Inderjeet Singh 2009-09-23 17:45:16 +00:00
parent 29ab864b2f
commit 3b0f8f4340
22 changed files with 73 additions and 32 deletions

View File

@ -320,15 +320,15 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
Preconditions.checkArgument(typeAdapter instanceof JsonSerializer Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator); || typeAdapter instanceof JsonDeserializer<?> || typeAdapter instanceof InstanceCreator<?>);
if (typeAdapter instanceof InstanceCreator) { if (typeAdapter instanceof InstanceCreator<?>) {
registerInstanceCreator(type, (InstanceCreator<?>) typeAdapter); registerInstanceCreator(type, (InstanceCreator<?>) typeAdapter);
} }
if (typeAdapter instanceof JsonSerializer) { if (typeAdapter instanceof JsonSerializer<?>) {
registerSerializer(type, (JsonSerializer<?>) typeAdapter); registerSerializer(type, (JsonSerializer<?>) typeAdapter);
} }
if (typeAdapter instanceof JsonDeserializer) { if (typeAdapter instanceof JsonDeserializer<?>) {
registerDeserializer(type, (JsonDeserializer<?>) typeAdapter); registerDeserializer(type, (JsonDeserializer<?>) typeAdapter);
} }
return this; return this;

View File

@ -57,7 +57,7 @@ final class TypeInfoFactory {
private static Type getActualType( private static Type getActualType(
Type typeToEvaluate, Type parentType, Class<?> rawParentClass) { Type typeToEvaluate, Type parentType, Class<?> rawParentClass) {
if (typeToEvaluate instanceof Class) { if (typeToEvaluate instanceof Class<?>) {
return typeToEvaluate; return typeToEvaluate;
} else if (typeToEvaluate instanceof ParameterizedType) { } else if (typeToEvaluate instanceof ParameterizedType) {
ParameterizedType castedType = (ParameterizedType) typeToEvaluate; ParameterizedType castedType = (ParameterizedType) typeToEvaluate;
@ -73,13 +73,13 @@ final class TypeInfoFactory {
if (componentType.equals(actualType)) { if (componentType.equals(actualType)) {
return castedType; return castedType;
} else { } else {
if (actualType instanceof Class) { if (actualType instanceof Class<?>) {
return TypeUtils.wrapWithArray(TypeUtils.toRawClass(actualType)); return TypeUtils.wrapWithArray(TypeUtils.toRawClass(actualType));
} else { } else {
return new GenericArrayTypeImpl(actualType); return new GenericArrayTypeImpl(actualType);
} }
} }
} else if (typeToEvaluate instanceof TypeVariable) { } else if (typeToEvaluate instanceof TypeVariable<?>) {
if (parentType instanceof ParameterizedType) { if (parentType instanceof ParameterizedType) {
// The class definition has the actual types used for the type variables. // 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. // Find the matching actual type for the Type Variable used for the field.

View File

@ -32,7 +32,7 @@ final class TypeInfoMap {
private final Type valueType; private final Type valueType;
public TypeInfoMap(Type mapType) { 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; keyType = String.class;
valueType = String.class; valueType = String.class;
} else if (mapType instanceof ParameterizedType) { } else if (mapType instanceof ParameterizedType) {

View File

@ -41,7 +41,7 @@ final class TypeUtils {
* <code>TypeUtils.getActualTypeForFirstTypeVariable(fooType)</code> will return Integer.class. * <code>TypeUtils.getActualTypeForFirstTypeVariable(fooType)</code> will return Integer.class.
*/ */
static Type getActualTypeForFirstTypeVariable(Type type) { static Type getActualTypeForFirstTypeVariable(Type type) {
if (type instanceof Class) { if (type instanceof Class<?>) {
return Object.class; return Object.class;
} else if (type instanceof ParameterizedType) { } else if (type instanceof ParameterizedType) {
return ((ParameterizedType)type).getActualTypeArguments()[0]; return ((ParameterizedType)type).getActualTypeArguments()[0];
@ -54,7 +54,7 @@ final class TypeUtils {
} }
static boolean isArray(Type type) { static boolean isArray(Type type) {
if (type instanceof Class) { if (type instanceof Class<?>) {
return ((Class<?>)type).isArray(); return ((Class<?>)type).isArray();
} else if (type instanceof GenericArrayType) { } else if (type instanceof GenericArrayType) {
return true; return true;
@ -67,7 +67,7 @@ final class TypeUtils {
* This method returns the actual raw class associated with the specified type. * This method returns the actual raw class associated with the specified type.
*/ */
static Class<?> toRawClass(Type type) { static Class<?> toRawClass(Type type) {
if (type instanceof Class) { if (type instanceof Class<?>) {
return (Class<?>) type; return (Class<?>) type;
} else if (type instanceof ParameterizedType) { } else if (type instanceof ParameterizedType) {
ParameterizedType actualType = (ParameterizedType)type; ParameterizedType actualType = (ParameterizedType)type;

View File

@ -87,7 +87,7 @@ public abstract class TypeToken<T> {
*/ */
static Type getSuperclassTypeParameter(Class<?> subclass) { static Type getSuperclassTypeParameter(Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass(); Type superclass = subclass.getGenericSuperclass();
if (superclass instanceof Class) { if (superclass instanceof Class<?>) {
throw new RuntimeException("Missing type parameter."); throw new RuntimeException("Missing type parameter.");
} }
return ((ParameterizedType) superclass).getActualTypeArguments()[0]; return ((ParameterizedType) superclass).getActualTypeArguments()[0];
@ -163,7 +163,7 @@ public abstract class TypeToken<T> {
return true; return true;
} }
if (type instanceof Class) { if (type instanceof Class<?>) {
return rawType.isAssignableFrom(getRawType(from)); return rawType.isAssignableFrom(getRawType(from));
} else if (type instanceof ParameterizedType) { } else if (type instanceof ParameterizedType) {
return isAssignableFrom(from, (ParameterizedType) type, return isAssignableFrom(from, (ParameterizedType) type,
@ -194,7 +194,7 @@ public abstract class TypeToken<T> {
Type t = from; Type t = from;
if (from instanceof GenericArrayType) { if (from instanceof GenericArrayType) {
t = ((GenericArrayType) from).getGenericComponentType(); t = ((GenericArrayType) from).getGenericComponentType();
} else if (from instanceof Class) { } else if (from instanceof Class<?>) {
Class<?> classType = (Class<?>) from; Class<?> classType = (Class<?>) from;
while (classType.isArray()) { while (classType.isArray()) {
classType = classType.getComponentType(); classType = classType.getComponentType();
@ -238,7 +238,7 @@ public abstract class TypeToken<T> {
for (int i = 0; i < tArgs.length; i++) { for (int i = 0; i < tArgs.length; i++) {
Type arg = tArgs[i]; Type arg = tArgs[i];
TypeVariable<?> var = tParams[i]; TypeVariable<?> var = tParams[i];
while (arg instanceof TypeVariable) { while (arg instanceof TypeVariable<?>) {
TypeVariable<?> v = (TypeVariable<?>) arg; TypeVariable<?> v = (TypeVariable<?>) arg;
arg = typeVarMap.get(v.getName()); arg = typeVarMap.get(v.getName());
} }
@ -293,7 +293,7 @@ public abstract class TypeToken<T> {
Map<String, Type> typeMap) { Map<String, Type> typeMap) {
if (to.equals(from)) return true; if (to.equals(from)) return true;
if (from instanceof TypeVariable) { if (from instanceof TypeVariable<?>) {
return to.equals(typeMap.get(((TypeVariable<?>)from).getName())); return to.equals(typeMap.get(((TypeVariable<?>)from).getName()));
} }

View File

@ -51,6 +51,7 @@ public class ExposeAnnotationBasedExclusionStrategyTest extends TestCase {
assertFalse(strategy.shouldSkipField(f)); assertFalse(strategy.shouldSkipField(f));
} }
@SuppressWarnings("unused")
private static class MockObject { private static class MockObject {
@Expose @Expose
public final int exposedField = 0; public final int exposedField = 0;

View File

@ -53,6 +53,7 @@ public class SerializedNameAnnotationInterceptingNamingPolicyTest extends TestCa
assertEquals(fieldName, policy.translateName(f)); assertEquals(fieldName, policy.translateName(f));
} }
@SuppressWarnings("unused")
private static class SomeObject { private static class SomeObject {
@SerializedName(ANNOTATED_FIELD_NAME) public final int fieldWithAnnotation = 1; @SerializedName(ANNOTATED_FIELD_NAME) public final int fieldWithAnnotation = 1;
public final int fieldWithoutAnnotation = 1; public final int fieldWithoutAnnotation = 1;

View File

@ -223,6 +223,7 @@ public class TypeInfoFactoryTest extends TestCase {
assertEquals(List[].class, typeInfo.getRawClass()); assertEquals(List[].class, typeInfo.getRawClass());
} }
@SuppressWarnings("unused")
private static class ObjectWithDifferentFields<T> { private static class ObjectWithDifferentFields<T> {
public static enum TestEnum { public static enum TestEnum {
TEST_1, TEST_2; TEST_1, TEST_2;

View File

@ -67,6 +67,7 @@ public class VersionExclusionStrategyTest extends TestCase {
@Since(VERSION) @Since(VERSION)
private static class MockObject { private static class MockObject {
@SuppressWarnings("unused")
@Since(VERSION) @Since(VERSION)
public final int someField = 0; public final int someField = 0;
} }

View File

@ -283,7 +283,8 @@ public class CollectionTest extends TestCase {
private static class ObjectWithWildcardCollection { private static class ObjectWithWildcardCollection {
private final Collection<? extends BagOfPrimitives> collection; private final Collection<? extends BagOfPrimitives> collection;
@SuppressWarnings("unchecked") // For use by Gson
@SuppressWarnings({ "unchecked", "unused" })
public ObjectWithWildcardCollection() { public ObjectWithWildcardCollection() {
this(Collections.EMPTY_LIST); this(Collections.EMPTY_LIST);
} }
@ -300,7 +301,8 @@ public class CollectionTest extends TestCase {
private static class Entry { private static class Entry {
int value; int value;
// For use by Gson // For use by Gson
Entry() { @SuppressWarnings("unused")
private Entry() {
this(10); this(10);
} }
Entry(int value) { Entry(int value) {

View File

@ -120,7 +120,8 @@ public class ConcurrencyTest extends TestCase {
finishedLatch.await(); finishedLatch.await();
assertFalse(failed.get()); assertFalse(failed.get());
} }
@SuppressWarnings("unused")
private static class MyObject { private static class MyObject {
String a; String a;
String b; String b;

View File

@ -65,7 +65,9 @@ public class CustomDeserializerTest extends TestCase {
private static class DataHolder { private static class DataHolder {
private final String data; private final String data;
public DataHolder() { // For use by Gson
@SuppressWarnings("unused")
private DataHolder() {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@ -81,7 +83,9 @@ public class CustomDeserializerTest extends TestCase {
private static class DataHolderWrapper { private static class DataHolderWrapper {
private final DataHolder wrappedData; private final DataHolder wrappedData;
public DataHolderWrapper() { // For use by Gson
@SuppressWarnings("unused")
private DataHolderWrapper() {
this(new DataHolder(DEFAULT_VALUE)); this(new DataHolder(DEFAULT_VALUE));
} }

View File

@ -169,6 +169,7 @@ public class CustomTypeAdaptersTest extends TestCase {
} }
private static class Derived extends Base { private static class Derived extends Base {
@SuppressWarnings("unused")
int derivedValue = 3; int derivedValue = 3;
} }

View File

@ -289,7 +289,9 @@ public class DefaultTypeAdaptersTest extends TestCase {
private static class ClassWithBigDecimal { private static class ClassWithBigDecimal {
BigDecimal value; BigDecimal value;
ClassWithBigDecimal() { } // For use by Gson
@SuppressWarnings("unused")
private ClassWithBigDecimal() { }
ClassWithBigDecimal(String value) { ClassWithBigDecimal(String value) {
this.value = new BigDecimal(value); this.value = new BigDecimal(value);
} }
@ -300,7 +302,9 @@ public class DefaultTypeAdaptersTest extends TestCase {
private static class ClassWithBigInteger { private static class ClassWithBigInteger {
BigInteger value; BigInteger value;
ClassWithBigInteger() { } // For use by Gson
@SuppressWarnings("unused")
private ClassWithBigInteger() { }
ClassWithBigInteger(String value) { ClassWithBigInteger(String value) {
this.value = new BigInteger(value); this.value = new BigInteger(value);
} }

View File

@ -110,6 +110,7 @@ public class ExposeFieldsTest extends TestCase {
assertNotNull(obj.interfaceField); assertNotNull(obj.interfaceField);
} }
@SuppressWarnings("unused")
private static class ClassWithExposedFields { private static class ClassWithExposedFields {
@Expose private final Integer a; @Expose private final Integer a;
private final Integer b; private final Integer b;
@ -117,7 +118,8 @@ public class ExposeFieldsTest extends TestCase {
@Expose(deserialize = false) final double d; @Expose(deserialize = false) final double d;
@Expose(serialize = false, deserialize = false) final char e; @Expose(serialize = false, deserialize = false) final char e;
ClassWithExposedFields() { // For use by Gson
private ClassWithExposedFields() {
this(null, null); this(null, null);
} }
@ -166,7 +168,9 @@ public class ExposeFieldsTest extends TestCase {
@Expose @Expose
private final SomeInterface interfaceField; private final SomeInterface interfaceField;
public ClassWithInterfaceField() { // For use by Gson
@SuppressWarnings("unused")
private ClassWithInterfaceField() {
this(null); this(null);
} }

View File

@ -75,6 +75,7 @@ public class JsonTreeTest extends TestCase {
} }
private static class SubTypeOfBagOfPrimitives extends BagOfPrimitives { private static class SubTypeOfBagOfPrimitives extends BagOfPrimitives {
@SuppressWarnings("unused")
float f = 1.2F; float f = 1.2F;
public SubTypeOfBagOfPrimitives(long l, int i, boolean b, String string, float f) { public SubTypeOfBagOfPrimitives(long l, int i, boolean b, String string, float f) {
super(l, i, b, string); super(l, i, b, string);

View File

@ -172,6 +172,7 @@ public class MapTest extends TestCase {
private static class MyParameterizedMap<K, V> extends LinkedHashMap<K, V> { private static class MyParameterizedMap<K, V> extends LinkedHashMap<K, V> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
int foo = 10; int foo = 10;
} }
@ -248,6 +249,7 @@ public class MapTest extends TestCase {
private static class MyMap extends LinkedHashMap<String, String> { private static class MyMap extends LinkedHashMap<String, String> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
int foo = 10; int foo = 10;
} }

View File

@ -82,6 +82,7 @@ public class NamingPolicyTest extends TestCase {
assertEquals("{\"a\":3.0}", actual); assertEquals("{\"a\":3.0}", actual);
} }
@SuppressWarnings("unused")
private static class ClassWithDuplicateFields { private static class ClassWithDuplicateFields {
public Integer a; public Integer a;
@SerializedName("a") public Double b; @SerializedName("a") public Double b;

View File

@ -151,6 +151,7 @@ public class NullObjectAndFieldTest extends TestCase {
private Long value; private Long value;
} }
@SuppressWarnings("unused")
private static class ClassWithMembers { private static class ClassWithMembers {
String str; String str;
int[] array; int[] array;

View File

@ -366,6 +366,7 @@ public class ObjectTest extends TestCase {
} }
private static class ClassWithObjectField { private static class ClassWithObjectField {
@SuppressWarnings("unused")
Object member; Object member;
} }
@ -391,6 +392,7 @@ public class ObjectTest extends TestCase {
} }
private static class Parent { private static class Parent {
@SuppressWarnings("unused")
int value1 = 1; int value1 = 1;
private class Child { private class Child {
int value2 = 2; int value2 = 2;
@ -404,7 +406,8 @@ public class ObjectTest extends TestCase {
private SortedSet<Character> sortedSet; private SortedSet<Character> sortedSet;
// For use by Gson // For use by Gson
ClassWithSubInterfacesOfCollection() { @SuppressWarnings("unused")
private ClassWithSubInterfacesOfCollection() {
} }
public ClassWithSubInterfacesOfCollection(List<Integer> list, Queue<Long> queue, Set<Float> set, public ClassWithSubInterfacesOfCollection(List<Integer> list, Queue<Long> queue, Set<Float> set,
@ -496,7 +499,9 @@ public class ObjectTest extends TestCase {
private static class SubTypeOfNested extends Nested { private static class SubTypeOfNested extends Nested {
private final long value = 5; private final long value = 5;
public SubTypeOfNested() { // Used by Gson
@SuppressWarnings("unused")
private SubTypeOfNested() {
this(null, null); this(null, null);
} }

View File

@ -271,7 +271,9 @@ public class ParameterizedTypesTest extends TestCase {
private final List<? extends T> listOfWildcardTypeParameters; private final List<? extends T> listOfWildcardTypeParameters;
private final List<? extends T>[] arrayOfListOfWildcardTypeParameters; private final List<? extends T>[] arrayOfListOfWildcardTypeParameters;
public ObjectWithTypeVariables() { // For use by Gson
@SuppressWarnings("unused")
private ObjectWithTypeVariables() {
this(null, null, null, null, null, null); this(null, null, null, null, null, null);
} }
@ -386,7 +388,9 @@ public class ParameterizedTypesTest extends TestCase {
C c; C c;
D d; D d;
E e; E e;
MultiParameters() { // For use by Gson
@SuppressWarnings("unused")
private MultiParameters() {
} }
MultiParameters(A a, B b, C c, D d, E e) { MultiParameters(A a, B b, C c, D d, E e) {
super(); super();
@ -461,9 +465,11 @@ public class ParameterizedTypesTest extends TestCase {
// Begin: tests to reproduce issue 103 // Begin: tests to reproduce issue 103
private static class Quantity { private static class Quantity {
@SuppressWarnings("unused")
int q = 10; int q = 10;
} }
private static class MyQuantity extends Quantity { private static class MyQuantity extends Quantity {
@SuppressWarnings("unused")
int q2 = 20; int q2 = 20;
} }
private interface Measurable<T> { private interface Measurable<T> {

View File

@ -72,7 +72,10 @@ public class PerformanceTest extends TestCase {
private static class ExceptionHolder { private static class ExceptionHolder {
public final String message; public final String message;
public final String stackTrace; public final String stackTrace;
public ExceptionHolder() {
// For use by Gson
@SuppressWarnings("unused")
private ExceptionHolder() {
this("", ""); this("", "");
} }
public ExceptionHolder(String message, String stackTrace) { public ExceptionHolder(String message, String stackTrace) {
@ -81,11 +84,13 @@ public class PerformanceTest extends TestCase {
} }
} }
@SuppressWarnings("unused")
private static class CollectionEntry { private static class CollectionEntry {
final String name; final String name;
final String value; final String value;
CollectionEntry() { // For use by Gson
private CollectionEntry() {
this(null, null); this(null, null);
} }