Pure refactorings:

Replaced DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS and DEFAULT_INSTANCE_CREATORS with a single EMPTY_MAP.
Removed obsoleted TODO from Gson.
made ParameterizedTypeHandlerMap.makeUnmodifiable a builder method that returns this instance.
This commit is contained in:
Inderjeet Singh 2011-09-30 17:56:40 +00:00
parent 1621011bf2
commit d3f927eb42
5 changed files with 15 additions and 59 deletions

View File

@ -25,8 +25,6 @@ import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.google.gson.internal.ParameterizedTypeHandlerMap;
/**
* List of all the default type adapters ({@link JsonSerializer}s, {@link JsonDeserializer}s,
* and {@link InstanceCreator}s.
@ -35,38 +33,6 @@ import com.google.gson.internal.ParameterizedTypeHandlerMap;
* @author Joel Leitch
*/
final class DefaultTypeAdapters {
// The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS
// must be defined after the constants for the type adapters. Otherwise, the type adapter
// constants will appear as nulls.
static final ParameterizedTypeHandlerMap<JsonSerializer<?>> DEFAULT_SERIALIZERS =
createDefaultSerializers();
static final ParameterizedTypeHandlerMap<JsonDeserializer<?>> DEFAULT_DESERIALIZERS =
createDefaultDeserializers();
static final ParameterizedTypeHandlerMap<InstanceCreator<?>> DEFAULT_INSTANCE_CREATORS =
createDefaultInstanceCreators();
private static ParameterizedTypeHandlerMap<JsonSerializer<?>> createDefaultSerializers() {
ParameterizedTypeHandlerMap<JsonSerializer<?>> map =
new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
map.makeUnmodifiable();
return map;
}
private static ParameterizedTypeHandlerMap<JsonDeserializer<?>> createDefaultDeserializers() {
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
map.makeUnmodifiable();
return map;
}
private static ParameterizedTypeHandlerMap<InstanceCreator<?>> createDefaultInstanceCreators() {
ParameterizedTypeHandlerMap<InstanceCreator<?>> map
= new ParameterizedTypeHandlerMap<InstanceCreator<?>>();
map.makeUnmodifiable();
return map;
}
/**
* This type adapter supports three subclasses of date: Date, Timestamp, and
* java.sql.Date.

View File

@ -99,9 +99,9 @@ import java.util.Map;
* @author Joel Leitch
*/
public final class Gson {
//TODO(inder): get rid of all the registerXXX methods and take all such parameters in the
// constructor instead. At the minimum, mark those methods private.
@SuppressWarnings("unchecked")
static final ParameterizedTypeHandlerMap EMPTY_MAP =
new ParameterizedTypeHandlerMap().makeUnmodifiable();
static final boolean DEFAULT_JSON_NON_EXECUTABLE = false;
@ -170,11 +170,10 @@ public final class Gson {
* {@link GsonBuilder#excludeFieldsWithModifiers(int...)}.</li>
* </ul>
*/
@SuppressWarnings("unchecked")
public Gson() {
this(DEFAULT_EXCLUSION_STRATEGY, DEFAULT_EXCLUSION_STRATEGY, DEFAULT_NAMING_POLICY,
DefaultTypeAdapters.DEFAULT_INSTANCE_CREATORS,
false, DefaultTypeAdapters.DEFAULT_SERIALIZERS,
DefaultTypeAdapters.DEFAULT_DESERIALIZERS, false, DEFAULT_JSON_NON_EXECUTABLE, true,
EMPTY_MAP, false, EMPTY_MAP, EMPTY_MAP, false, DEFAULT_JSON_NON_EXECUTABLE, true,
false, false, LongSerializationPolicy.DEFAULT,
Collections.<TypeAdapter.Factory>emptyList());
}

View File

@ -677,24 +677,13 @@ public final class GsonBuilder {
deserializationStrategies.add(exposeAnnotationDeserializationExclusionStrategy);
serializationStrategies.add(exposeAnnotationSerializationExclusionStrategy);
}
ParameterizedTypeHandlerMap<JsonSerializer<?>> customSerializers = serializers.copyOf();
ParameterizedTypeHandlerMap<JsonDeserializer<?>> customDeserializers = deserializers.copyOf();
addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, customSerializers,
customDeserializers);
ParameterizedTypeHandlerMap<InstanceCreator<?>> customInstanceCreators =
instanceCreators.copyOf();
customSerializers.makeUnmodifiable();
customDeserializers.makeUnmodifiable();
customInstanceCreators.makeUnmodifiable();
addTypeAdaptersForDate(datePattern, dateStyle, timeStyle, serializers, deserializers);
return new Gson(new DisjunctionExclusionStrategy(deserializationStrategies),
new DisjunctionExclusionStrategy(serializationStrategies),
fieldNamingPolicy, customInstanceCreators, serializeNulls,
customSerializers, customDeserializers, complexMapKeySerialization,
generateNonExecutableJson, escapeHtmlChars, prettyPrinting,
fieldNamingPolicy, instanceCreators.copyOf().makeUnmodifiable(), serializeNulls,
serializers.copyOf().makeUnmodifiable(), deserializers.copyOf().makeUnmodifiable(),
complexMapKeySerialization, generateNonExecutableJson, escapeHtmlChars, prettyPrinting,
serializeSpecialFloatingPointValues, longSerializationPolicy, typeAdapterFactories);
}

View File

@ -34,6 +34,7 @@ import java.util.logging.Logger;
* @param <T> The handler that will be looked up by type
*/
public final class ParameterizedTypeHandlerMap<T> {
private static final Logger logger =
Logger.getLogger(ParameterizedTypeHandlerMap.class.getName());
/**
@ -158,8 +159,9 @@ public final class ParameterizedTypeHandlerMap<T> {
}
}
public synchronized void makeUnmodifiable() {
public synchronized ParameterizedTypeHandlerMap<T> makeUnmodifiable() {
modifiable = false;
return this;
}
public synchronized T getHandlerFor(Type type, boolean systemOnly) {

View File

@ -33,15 +33,15 @@ import junit.framework.TestCase;
*/
public class FunctionWithInternalDependenciesTest extends TestCase {
@SuppressWarnings("unchecked")
public void testAnonymousLocalClassesSerialization() throws Exception {
LinkedList<ExclusionStrategy> strategies = new LinkedList<ExclusionStrategy>();
strategies.add(new SyntheticFieldExclusionStrategy(true));
strategies.add(new ModifierBasedExclusionStrategy(Modifier.TRANSIENT, Modifier.STATIC));
ExclusionStrategy exclusionStrategy = new DisjunctionExclusionStrategy(strategies);
Gson gson = new Gson(exclusionStrategy, exclusionStrategy, Gson.DEFAULT_NAMING_POLICY,
DefaultTypeAdapters.DEFAULT_INSTANCE_CREATORS,
false, DefaultTypeAdapters.DEFAULT_SERIALIZERS,
DefaultTypeAdapters.DEFAULT_DESERIALIZERS, false, Gson.DEFAULT_JSON_NON_EXECUTABLE,
Gson.EMPTY_MAP, false, Gson.EMPTY_MAP, Gson.EMPTY_MAP, false,
Gson.DEFAULT_JSON_NON_EXECUTABLE,
true, false, false, LongSerializationPolicy.DEFAULT,
Collections.<TypeAdapter.Factory>emptyList());
assertEquals("{}", gson.toJson(new ClassWithNoFields() {