Remove dead code and fold contents of single-member helper classes into their clients.

This commit is contained in:
Jesse Wilson 2011-11-20 21:02:26 +00:00
parent e9521471aa
commit f777a192ee
20 changed files with 25 additions and 218 deletions

View File

@ -116,7 +116,7 @@ public final class GsonBuilder {
serializeExclusionStrategies.add(Gson.DEFAULT_SYNTHETIC_FIELD_EXCLUSION_STRATEGY);
// setup default values
ignoreVersionsAfter = VersionConstants.IGNORE_VERSIONS;
ignoreVersionsAfter = VersionExclusionStrategy.IGNORE_VERSIONS;
serializeInnerClasses = true;
prettyPrinting = false;
escapeHtmlChars = true;
@ -650,7 +650,7 @@ public final class GsonBuilder {
deserializationStrategies.add(innerClassExclusionStrategy);
serializationStrategies.add(innerClassExclusionStrategy);
}
if (ignoreVersionsAfter != VersionConstants.IGNORE_VERSIONS) {
if (ignoreVersionsAfter != VersionExclusionStrategy.IGNORE_VERSIONS) {
VersionExclusionStrategy versionExclusionStrategy =
new VersionExclusionStrategy(ignoreVersionsAfter);
deserializationStrategies.add(versionExclusionStrategy);

View File

@ -31,7 +31,6 @@ import java.lang.reflect.Type;
* @author Joel Leitch
*/
final class JsonDeserializerExceptionWrapper<T> implements JsonDeserializer<T> {
private final JsonDeserializer<T> delegate;
/**

View File

@ -23,7 +23,6 @@ package com.google.gson;
* @author Joel Leitch
*/
public final class JsonIOException extends JsonParseException {
private static final long serialVersionUID = 1L;
public JsonIOException(String msg) {

View File

@ -50,7 +50,6 @@ import com.google.gson.stream.MalformedJsonException;
* @since 1.4
*/
public final class JsonStreamParser implements Iterator<JsonElement> {
private final JsonReader parser;
private final Object lock;

View File

@ -52,7 +52,7 @@ final class ModifyFirstLetterNamingPolicy extends RecursiveFieldNamingPolicy {
public enum LetterModifier {
UPPER,
LOWER;
LOWER
}
private final LetterModifier letterModifier;

View File

@ -40,5 +40,4 @@ final class SyntheticFieldExclusionStrategy implements ExclusionStrategy {
public boolean shouldSkipField(FieldAttributes f) {
return skipSyntheticFields && f.isSynthetic();
}
}

View File

@ -37,7 +37,6 @@ package com.google.gson;
* @author Joel Leitch
*/
final class UpperCamelCaseSeparatorNamingPolicy extends CompositionFieldNamingPolicy {
public UpperCamelCaseSeparatorNamingPolicy(String separatorString) {
super(new CamelCaseSeparatorNamingPolicy(separatorString),
new ModifyFirstLetterNamingPolicy(ModifyFirstLetterNamingPolicy.LetterModifier.UPPER));

View File

@ -1,29 +0,0 @@
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson;
/**
* Class contain all constants for versioning support.
*
* @author Joel Leitch
*/
final class VersionConstants {
// Prevent instantiation
private VersionConstants() { }
static final double IGNORE_VERSIONS = -1D;
}

View File

@ -27,6 +27,7 @@ import com.google.gson.internal.$Gson$Preconditions;
* @author Joel Leitch
*/
final class VersionExclusionStrategy implements ExclusionStrategy {
static final double IGNORE_VERSIONS = -1D;
private final double version;
VersionExclusionStrategy(double version) {

View File

@ -42,10 +42,4 @@ public final class $Gson$Preconditions {
throw new IllegalArgumentException();
}
}
public static void checkState(boolean condition) {
if (!condition) {
throw new IllegalStateException();
}
}
}

View File

@ -269,14 +269,6 @@ public final class $Gson$Types {
$Gson$Types.getGenericSupertype(context, contextRawType, supertype));
}
/**
* Returns true if this type is an array.
*/
public static boolean isArray(Type type) {
return type instanceof GenericArrayType
|| (type instanceof Class && ((Class<?>) type).isArray());
}
/**
* Returns the component type of this array type.
* @throws ClassCastException if this type is not an array.

View File

@ -45,10 +45,8 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
Type componentType = $Gson$Types.getArrayComponentType(type);
TypeAdapter<?> componentTypeAdapter = context.getAdapter(TypeToken.get(componentType));
// create() doesn't define a type parameter
TypeAdapter<T> result = new ArrayTypeAdapter(
return new ArrayTypeAdapter(
context, componentTypeAdapter, $Gson$Types.getRawType(componentType));
return result;
}
};
@ -84,13 +82,13 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter writer, Object array) throws IOException {
if (array == null) {
writer.nullValue(); // TODO: better policy here?
writer.nullValue();
return;
}
writer.beginArray();
for (int i = 0, length = Array.getLength(array); i < length; i++) {
final E value = (E) Array.get(array, i);
E value = (E) Array.get(array, i);
componentTypeAdapter.write(writer, value);
}
writer.endArray();

View File

@ -48,10 +48,7 @@ public final class ExcludedTypeAdapterFactory implements TypeAdapter.Factory {
}
return new TypeAdapter<T>() {
/**
* The delegate is lazily created because it may not be needed, and
* creating it may fail.
*/
/** The delegate is lazily created because it may not be needed, and creating it may fail. */
private TypeAdapter<T> delegate;
@Override public T read(JsonReader reader) throws IOException {

View File

@ -195,7 +195,7 @@ public final class MapTypeAdapterFactory implements TypeAdapter.Factory {
public void write(JsonWriter writer, Map<K, V> map) throws IOException {
if (map == null) {
writer.nullValue(); // TODO: better policy here?
writer.nullValue();
return;
}

View File

@ -90,7 +90,7 @@ public final class ObjectTypeAdapter extends TypeAdapter<Object> {
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter writer, Object value) throws IOException {
if (value == null) {
writer.nullValue(); // TODO: better policy here?
writer.nullValue();
return;
}

View File

@ -1,32 +0,0 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.internal.bind;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
final class Reflection {
/**
* Finds a compatible runtime type if it is more specific
*/
public static Type getRuntimeTypeIfMoreSpecific(Type type, Object value) {
if (value != null
&& (type == Object.class || type instanceof TypeVariable<?> || type instanceof Class<?>)) {
type = value.getClass();
}
return type;
}
}

View File

@ -1,109 +0,0 @@
/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.internal.bind;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.ObjectConstructor;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
/**
* Adapt a map whose keys are strings.
*/
public final class StringToValueMapTypeAdapterFactory implements TypeAdapter.Factory {
private final ConstructorConstructor constructorConstructor;
public StringToValueMapTypeAdapterFactory(ConstructorConstructor constructorConstructor) {
this.constructorConstructor = constructorConstructor;
}
public <T> TypeAdapter<T> create(Gson context, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof ParameterizedType)) {
return null;
}
Class<? super T> rawType = typeToken.getRawType();
if (!Map.class.isAssignableFrom(rawType)) {
return null;
}
Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(type, rawType);
if (keyAndValueTypes[0] != String.class) {
return null;
}
TypeAdapter<?> valueAdapter = context.getAdapter(TypeToken.get(keyAndValueTypes[1]));
ObjectConstructor<?> constructor = constructorConstructor.getConstructor(typeToken);
@SuppressWarnings({"unchecked", "rawtypes"})
// we don't define a type parameter for the key or value types
TypeAdapter<T> result = new Adapter(valueAdapter, constructor);
return result;
}
private final class Adapter<V> extends TypeAdapter<Map<String, V>> {
private final TypeAdapter<V> valueTypeAdapter;
private final ObjectConstructor<? extends Map<String, V>> constructor;
public Adapter(TypeAdapter<V> valueTypeAdapter,
ObjectConstructor<? extends Map<String, V>> constructor) {
this.valueTypeAdapter = valueTypeAdapter;
this.constructor = constructor;
}
public Map<String, V> read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
Map<String, V> map = constructor.construct();
reader.beginObject();
while (reader.hasNext()) {
String key = reader.nextName();
V value = valueTypeAdapter.read(reader);
map.put(key, value);
}
reader.endObject();
return map;
}
public void write(JsonWriter writer, Map<String, V> map) throws IOException {
if (map == null) {
writer.nullValue(); // TODO: better policy here?
return;
}
writer.beginObject();
for (Map.Entry<String, V> entry : map.entrySet()) {
writer.name(entry.getKey());
valueTypeAdapter.write(writer, entry.getValue());
}
writer.endObject();
}
}
}

View File

@ -22,9 +22,9 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
final class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
private final Gson context;
private final TypeAdapter<T> delegate;
private final Type type;
@ -50,7 +50,7 @@ final class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
// Fourth preference: reflective type adapter for the declared type
TypeAdapter chosen = delegate;
Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(type, value);
Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value);
if (runtimeType != type) {
TypeAdapter runtimeTypeAdapter = context.getAdapter(TypeToken.get(runtimeType));
if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) {
@ -67,4 +67,15 @@ final class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
}
chosen.write(writer, value);
}
/**
* Finds a compatible runtime type if it is more specific
*/
private Type getRuntimeTypeIfMoreSpecific(Type type, Object value) {
if (value != null
&& (type == Object.class || type instanceof TypeVariable<?> || type instanceof Class<?>)) {
type = value.getClass();
}
return type;
}
}

View File

@ -239,9 +239,6 @@ public final class TypeAdapters {
}
};
public static final TypeAdapter.Factory LONG_FACTORY
= newFactory(long.class, Long.class, LONG);
public static final TypeAdapter<Number> FLOAT = new TypeAdapter<Number>() {
@Override
public Number read(JsonReader reader) throws IOException {
@ -257,9 +254,6 @@ public final class TypeAdapters {
}
};
public static final TypeAdapter.Factory FLOAT_FACTORY
= newFactory(float.class, Float.class, FLOAT);
public static final TypeAdapter<Number> DOUBLE = new TypeAdapter<Number>() {
@Override
public Number read(JsonReader reader) throws IOException {
@ -275,9 +269,6 @@ public final class TypeAdapters {
}
};
public static final TypeAdapter.Factory DOUBLE_FACTORY
= newFactory(double.class, Double.class, DOUBLE);
public static final TypeAdapter<Number> NUMBER = new TypeAdapter<Number>() {
@Override
public Number read(JsonReader reader) throws IOException {
@ -663,13 +654,13 @@ public final class TypeAdapters {
reader.nextNull();
return null;
}
return (T) Enum.valueOf((Class<T>) classOfT, reader.nextString());
return Enum.valueOf(classOfT, reader.nextString());
}
public void write(JsonWriter writer, T value) throws IOException {
writer.value(value == null ? null : value.name());
}
};
}
public static final TypeAdapter.Factory ENUM_FACTORY = newEnumTypeHierarchyFactory(Enum.class);
@ -749,5 +740,4 @@ public final class TypeAdapters {
}
};
}
}

View File

@ -45,7 +45,6 @@ import java.util.Map;
* @author Jesse Wilson
*/
public class TypeToken<T> {
final Class<? super T> rawType;
final Type type;
final int hashCode;