Delete dead code found by coverage

This commit is contained in:
Jesse Wilson 2011-10-01 02:04:48 +00:00
parent 38ce53766e
commit de727d8c48
20 changed files with 34 additions and 167 deletions

View File

@ -40,12 +40,4 @@ interface Cache<K, V> {
* @return the cached value for the given {@code key}
*/
V getElement(K key);
/**
* Removes the value from the cache for the given key.
*
* @param key the key identifying the value to remove
* @return the value for the given {@code key}
*/
V removeElement(K key);
}

View File

@ -17,8 +17,6 @@
package com.google.gson;
import com.google.gson.internal.$Gson$Preconditions;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.internal.Pair;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
@ -50,7 +48,6 @@ public final class FieldAttributes {
private final boolean isSynthetic;
private final int modifiers;
private final String name;
private final Type resolvedType;
// Fields used for lazy initialization
private Type genericType;
@ -60,16 +57,14 @@ public final class FieldAttributes {
* Constructs a Field Attributes object from the {@code f}.
*
* @param f the field to pull attributes from
* @param declaringType The type in which the field is declared
*/
FieldAttributes(Class<?> declaringClazz, Field f, Type declaringType) {
FieldAttributes(Class<?> declaringClazz, Field f) {
this.declaringClazz = $Gson$Preconditions.checkNotNull(declaringClazz);
this.name = f.getName();
this.declaredType = f.getType();
this.isSynthetic = f.isSynthetic();
this.modifiers = f.getModifiers();
this.field = f;
this.resolvedType = getTypeInfoForField(f, declaringType);
}
private static int getMaxCacheSize() {
@ -121,7 +116,7 @@ public final class FieldAttributes {
}
/**
* Returns the {@code Class<?>} object that was declared for this field.
* Returns the {@code Class} object that was declared for this field.
*
* <p>For example, assume the following class definition:
* <pre class="code">
@ -185,16 +180,6 @@ public final class FieldAttributes {
return (modifiers & modifier) != 0;
}
/**
* This is exposed internally only for the removing synthetic fields from the JSON output.
*
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
void set(Object instance, Object value) throws IllegalAccessException {
field.set(instance, value);
}
/**
* This is exposed internally only for the removing synthetic fields from the JSON output.
*
@ -223,10 +208,6 @@ public final class FieldAttributes {
return field;
}
Type getResolvedType() {
return resolvedType;
}
@SuppressWarnings("unchecked")
private static <T extends Annotation> T getAnnotationFromArray(
Collection<Annotation> annotations, Class<T> annotation) {
@ -237,21 +218,4 @@ public final class FieldAttributes {
}
return null;
}
/**
* Evaluates the "actual" type for the field. If the field is a "TypeVariable" or has a
* "TypeVariable" in a parameterized type then it evaluates the real type.
*
* @param f the actual field object to retrieve the type from
* @param typeDefiningF the type that contains the field {@code f}
* @return the type information for the field
*/
static Type getTypeInfoForField(Field f, Type typeDefiningF) {
Class<?> rawType = $Gson$Types.getRawType(typeDefiningF);
if (!f.getDeclaringClass().isAssignableFrom(rawType)) {
// this field is unrelated to the type; the user probably omitted type information
return f.getGenericType();
}
return $Gson$Types.resolve(typeDefiningF, rawType, f.getGenericType());
}
}

View File

@ -111,7 +111,7 @@ public final class Gson {
static final SyntheticFieldExclusionStrategy DEFAULT_SYNTHETIC_FIELD_EXCLUSION_STRATEGY =
new SyntheticFieldExclusionStrategy(true);
static final ModifierBasedExclusionStrategy DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY =
new ModifierBasedExclusionStrategy(new int[] { Modifier.TRANSIENT, Modifier.STATIC });
new ModifierBasedExclusionStrategy(Modifier.TRANSIENT, Modifier.STATIC);
static final FieldNamingStrategy2 DEFAULT_NAMING_POLICY =
new SerializedNameAnnotationInterceptingNamingPolicy(new JavaFieldNamingPolicy());
@ -209,20 +209,20 @@ public final class Gson {
= new ReflectiveTypeAdapterFactory(constructorConstructor) {
@Override
public String getFieldName(Class<?> declaringClazz, Field f, Type declaredType) {
return fieldNamingPolicy.translateName(new FieldAttributes(declaringClazz, f, declaredType));
return fieldNamingPolicy.translateName(new FieldAttributes(declaringClazz, f));
}
@Override
public boolean serializeField(Class<?> declaringClazz, Field f, Type declaredType) {
ExclusionStrategy strategy = Gson.this.serializationExclusionStrategy;
return !strategy.shouldSkipClass(f.getType())
&& !strategy.shouldSkipField(new FieldAttributes(declaringClazz, f, declaredType));
&& !strategy.shouldSkipField(new FieldAttributes(declaringClazz, f));
}
@Override
public boolean deserializeField(Class<?> declaringClazz, Field f, Type declaredType) {
ExclusionStrategy strategy = Gson.this.deserializationExclusionStrategy;
return !strategy.shouldSkipClass(f.getType())
&& !strategy.shouldSkipField(new FieldAttributes(declaringClazz, f, declaredType));
&& !strategy.shouldSkipField(new FieldAttributes(declaringClazz, f));
}
};
@ -254,7 +254,7 @@ public final class Gson {
builder.factory(factory);
}
builder.factory(new GsonToMiniGsonTypeAdapterFactory(this, serializers, deserializers, serializeNulls))
builder.factory(new GsonToMiniGsonTypeAdapterFactory(this, serializers, deserializers))
.factory(TypeAdapters.URL_FACTORY)
.factory(TypeAdapters.URI_FACTORY)
.factory(TypeAdapters.UUID_FACTORY)
@ -685,7 +685,7 @@ public final class Gson {
*/
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
JsonReader jsonReader = new JsonReader(json);
T object = this.<T>fromJson(jsonReader, typeOfT);
T object = fromJson(jsonReader, typeOfT);
assertFullConsumption(object, jsonReader);
return object;
}

View File

@ -30,15 +30,12 @@ final class GsonToMiniGsonTypeAdapterFactory implements TypeAdapter.Factory {
private final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers;
private final JsonDeserializationContext deserializationContext;
private final JsonSerializationContext serializationContext;
private final boolean serializeNulls;
public GsonToMiniGsonTypeAdapterFactory(final Gson gson,
ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
boolean serializeNulls) {
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers) {
this.serializers = serializers;
this.deserializers = deserializers;
this.serializeNulls = serializeNulls;
this.deserializationContext = new JsonDeserializationContext() {
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {

View File

@ -19,7 +19,6 @@ package com.google.gson;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@ -74,13 +73,6 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
return result;
}
/**
* Reverses the elements of the array.
*/
void reverse() {
Collections.reverse(elements);
}
/**
* Returns the number of elements in the array.
*
@ -290,22 +282,6 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
throw new IllegalStateException();
}
/**
* convenience method to get this array as an Object if it contains a single element.
*
* @return get this element as an Object if it is single element array.
* @throws ClassCastException if the element in the array is of not a {@link JsonPrimitive} and
* is not a valid Object.
* @throws IllegalStateException if the array has more than one element.
*/
@Override
Object getAsObject() {
if (elements.size() == 1) {
return elements.get(0).getAsObject();
}
throw new IllegalStateException();
}
@Override
public boolean equals(Object o) {
return (o == this) || (o instanceof JsonArray && ((JsonArray) o).elements.equals(elements));

View File

@ -306,19 +306,6 @@ public abstract class JsonElement {
throw new UnsupportedOperationException(getClass().getSimpleName());
}
/**
* convenience method to get this element as an {@link Object} value.
*
* @return get this element as an Object value.
* @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid
* Object value.
* @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
* more than a single element.
*/
Object getAsObject() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}
/**
* Returns a deep copy of this. Immutable values can return this.
*/

View File

@ -275,25 +275,6 @@ public final class JsonPrimitive extends JsonElement {
return getAsString().charAt(0);
}
/**
* convenience method to get this element as an Object.
*
* @return get this element as an Object that can be converted to a suitable value.
*/
@Override
Object getAsObject() {
if (value instanceof BigInteger) {
BigInteger big = (BigInteger) value;
if (big.compareTo(INTEGER_MAX) < 0) {
return big.intValue();
} else if (big.compareTo(LONG_MAX) < 0) {
return big.longValue();
}
}
// No need to convert to float or double since those lose precision
return value;
}
private static boolean isPrimitiveOrString(Object target) {
if (target instanceof String) {
return true;

View File

@ -46,10 +46,6 @@ final class LruCache<K, V> extends LinkedHashMap<K, V> implements Cache<K, V> {
return get(key);
}
public synchronized V removeElement(K key) {
return remove(key);
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> entry) {
return size() > maxCapacity;

View File

@ -128,37 +128,6 @@ public final class ParameterizedTypeHandlerMap<T> {
}
}
public synchronized void register(ParameterizedTypeHandlerMap<T> other) {
if (!modifiable) {
throw new IllegalStateException("Attempted to modify an unmodifiable map.");
}
for (Map.Entry<Type, T> entry : other.userMap.entrySet()) {
register(entry.getKey(), entry.getValue(), false);
}
for (Map.Entry<Type, T> entry : other.systemMap.entrySet()) {
register(entry.getKey(), entry.getValue(), true);
}
// Quite important to traverse the typeHierarchyList from stack bottom first since
// we want to register the handlers in the same order to preserve priority order
for (int i = other.userTypeHierarchyList.size()-1; i >= 0; --i) {
Pair<Class<?>, T> entry = other.userTypeHierarchyList.get(i);
registerForTypeHierarchy(entry, false);
}
for (int i = other.systemTypeHierarchyList.size()-1; i >= 0; --i) {
Pair<Class<?>, T> entry = other.systemTypeHierarchyList.get(i);
registerForTypeHierarchy(entry, true);
}
}
public synchronized void registerIfAbsent(Type typeOfT, T value) {
if (!modifiable) {
throw new IllegalStateException("Attempted to modify an unmodifiable map.");
}
if (!userMap.containsKey(typeOfT)) {
register(typeOfT, value, false);
}
}
public synchronized ParameterizedTypeHandlerMap<T> makeUnmodifiable() {
modifiable = false;
return this;

View File

@ -121,11 +121,6 @@ public final class Streams {
} else if (element.isJsonArray()) {
writer.beginArray();
for (JsonElement e : element.getAsJsonArray()) {
/* always print null when its parent element is an array! */
if (e.isJsonNull()) {
writer.nullValue();
continue;
}
write(e, writer);
}
writer.endArray();

View File

@ -35,7 +35,7 @@ public class DisjunctionExclusionStrategyTest extends TestCase {
private static final Class<?> CLAZZ = String.class;
private static final FieldAttributes FIELD =
new FieldAttributes(CLAZZ, CLAZZ.getFields()[0], CLAZZ);
new FieldAttributes(CLAZZ, CLAZZ.getFields()[0]);
public void testBadInstantiation() throws Exception {
try {

View File

@ -75,7 +75,7 @@ public class ExposeAnnotationExclusionStrategyTest extends TestCase {
private static FieldAttributes createFieldAttributes(String fieldName) throws Exception {
Field f = MockObject.class.getField(fieldName);
return new FieldAttributes(MockObject.class, f, MockObject.class);
return new FieldAttributes(MockObject.class, f);
}
@SuppressWarnings("unused")

View File

@ -36,12 +36,12 @@ public class FieldAttributesTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
fieldAttributes = new FieldAttributes(Foo.class, Foo.class.getField("bar"), Foo.class);
fieldAttributes = new FieldAttributes(Foo.class, Foo.class.getField("bar"));
}
public void testNullField() throws Exception {
try {
new FieldAttributes(Foo.class, null, Foo.class);
new FieldAttributes(Foo.class, null);
fail("Field parameter can not be null");
} catch (NullPointerException expected) { }
}

View File

@ -34,7 +34,7 @@ public class FieldNamingStrategy2AdapterTest extends TestCase {
FieldNamingStrategy2 adapter =
new FieldNamingStrategy2Adapter(new UpperCaseNamingStrategy());
assertEquals(expectedFieldName, adapter.translateName(
new FieldAttributes(String.class, field, String.class)));
new FieldAttributes(String.class, field)));
}
private static class UpperCaseNamingStrategy implements FieldNamingStrategy {

View File

@ -46,7 +46,7 @@ public class InnerClassExclusionStrategyTest extends TestCase {
public void testExcludeInnerClassField() throws Exception {
Field f = getClass().getField("innerClass");
assertTrue(strategy.shouldSkipField(new FieldAttributes(getClass(), f, getClass())));
assertTrue(strategy.shouldSkipField(new FieldAttributes(getClass(), f)));
}
public void testIncludeStaticNestedClassObject() throws Exception {
@ -56,7 +56,7 @@ public class InnerClassExclusionStrategyTest extends TestCase {
public void testIncludeStaticNestedClassField() throws Exception {
Field f = getClass().getField("staticNestedClass");
assertFalse(strategy.shouldSkipField(new FieldAttributes(getClass(), f, getClass())));
assertFalse(strategy.shouldSkipField(new FieldAttributes(getClass(), f)));
}
class InnerClass {

View File

@ -34,7 +34,7 @@ public class JavaFieldNamingPolicyTest extends TestCase {
}
public void testFieldNamingPolicy() throws Exception {
FieldAttributes f = new FieldAttributes(String.class, String.class.getFields()[0], String.class);
FieldAttributes f = new FieldAttributes(String.class, String.class.getFields()[0]);
assertEquals(f.getName(), namingPolicy.translateName(f));
}

View File

@ -155,7 +155,17 @@ public class ParamterizedTypeFixtures {
Type genericClass = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
Class<?> rawType = $Gson$Types.getRawType(genericClass);
String className = rawType.getSimpleName();
T value = (T) json.getAsJsonObject().get(className).getAsObject();
JsonElement jsonElement = json.getAsJsonObject().get(className);
T value;
if (genericClass == Integer.class) {
value = (T) Integer.valueOf(jsonElement.getAsInt());
} else if (genericClass == String.class) {
value = (T) jsonElement.getAsString();
} else {
value = (T) jsonElement;
}
if (Primitives.isPrimitive(genericClass)) {
PrimitiveTypeAdapter typeAdapter = new PrimitiveTypeAdapter();
value = (T) typeAdapter.adaptType(value, rawType);

View File

@ -39,7 +39,7 @@ public class SerializedNameAnnotationInterceptingNamingPolicyTest extends TestCa
public void testFieldWithAnnotation() throws Exception {
String fieldName = "fieldWithAnnotation";
FieldAttributes f = new FieldAttributes(
SomeObject.class, SomeObject.class.getField(fieldName), SomeObject.class);
SomeObject.class, SomeObject.class.getField(fieldName));
assertFalse(ANNOTATED_FIELD_NAME.equals(fieldName));
assertEquals(ANNOTATED_FIELD_NAME, policy.translateName(f));
@ -48,7 +48,7 @@ public class SerializedNameAnnotationInterceptingNamingPolicyTest extends TestCa
public void testFieldWithoutAnnotation() throws Exception {
String fieldName = "fieldWithoutAnnotation";
FieldAttributes f = new FieldAttributes(
SomeObject.class, SomeObject.class.getField(fieldName), SomeObject.class);
SomeObject.class, SomeObject.class.getField(fieldName));
assertEquals(fieldName, policy.translateName(f));
}

View File

@ -43,7 +43,7 @@ public class VersionExclusionStrategyTest extends TestCase {
VersionExclusionStrategy strategy = new VersionExclusionStrategy(VERSION);
assertFalse(strategy.shouldSkipClass(clazz));
FieldAttributes fieldAttributes = new FieldAttributes(clazz, f, clazz);
FieldAttributes fieldAttributes = new FieldAttributes(clazz, f);
assertFalse(strategy.shouldSkipField(fieldAttributes));
}
@ -53,7 +53,7 @@ public class VersionExclusionStrategyTest extends TestCase {
VersionExclusionStrategy strategy = new VersionExclusionStrategy(VERSION + 1);
assertFalse(strategy.shouldSkipClass(clazz));
FieldAttributes fieldAttributes = new FieldAttributes(clazz, f, clazz);
FieldAttributes fieldAttributes = new FieldAttributes(clazz, f);
assertFalse(strategy.shouldSkipField(fieldAttributes));
}
@ -63,7 +63,7 @@ public class VersionExclusionStrategyTest extends TestCase {
VersionExclusionStrategy strategy = new VersionExclusionStrategy(VERSION - 1);
assertTrue(strategy.shouldSkipClass(clazz));
FieldAttributes fieldAttributes = new FieldAttributes(clazz, f, clazz);
FieldAttributes fieldAttributes = new FieldAttributes(clazz, f);
assertTrue(strategy.shouldSkipField(fieldAttributes));
}

View File

@ -127,7 +127,7 @@ public class ParameterizedTypesTest extends TestCase {
MyParameterizedType<Integer> src = new MyParameterizedType<Integer>(10);
String json = MyParameterizedTypeAdapter.<Integer>getExpectedJson(src);
MyParameterizedType<Integer> intTarget = gson.fromJson(json, ptIntegerType);
assertEquals(10, ((Number)intTarget.value).intValue());
assertEquals(10, intTarget.value.intValue());
MyParameterizedType<String> srcStr = new MyParameterizedType<String>("abc");
json = MyParameterizedTypeAdapter.<String>getExpectedJson(srcStr);