From 777e17c723fa2c87d703326f6bcf1195645893bd Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Sun, 20 Nov 2011 18:03:46 +0000 Subject: [PATCH] No more system type adapters. --- .../java/com/google/gson/GsonBuilder.java | 55 +++++++----------- .../internal/ParameterizedTypeHandlerMap.java | 57 +++++-------------- .../ParameterizedTypeHandlerMapTest.java | 28 ++++----- 3 files changed, 49 insertions(+), 91 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 06d3fe82..5c029209 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -518,10 +518,6 @@ public final class GsonBuilder { * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { - return registerTypeAdapter(type, typeAdapter, false); - } - - private GsonBuilder registerTypeAdapter(Type type, Object typeAdapter, boolean isSystem) { $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator @@ -531,13 +527,13 @@ public final class GsonBuilder { "Cannot register type adapters for " + type); } if (typeAdapter instanceof InstanceCreator) { - registerInstanceCreator(type, (InstanceCreator) typeAdapter, isSystem); + registerInstanceCreator(type, (InstanceCreator) typeAdapter); } if (typeAdapter instanceof JsonSerializer) { - registerSerializer(type, (JsonSerializer) typeAdapter, isSystem); + registerSerializer(type, (JsonSerializer) typeAdapter); } if (typeAdapter instanceof JsonDeserializer) { - registerDeserializer(type, (JsonDeserializer) typeAdapter, isSystem); + registerDeserializer(type, (JsonDeserializer) typeAdapter); } if (typeAdapter instanceof TypeAdapter.Factory) { typeAdapterFactories.add((TypeAdapter.Factory) typeAdapter); @@ -551,14 +547,14 @@ public final class GsonBuilder { * takes a type instead of a Class object, it can be used to register a specific handler for a * generic type corresponding to a raw type. * - * @param the type for which instance creator is being registered + * * @param typeOfT The Type definition for T * @param instanceCreator the instance creator for T * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ private GsonBuilder registerInstanceCreator(Type typeOfT, - InstanceCreator instanceCreator, boolean isSystem) { - instanceCreators.register(typeOfT, instanceCreator, isSystem); + InstanceCreator instanceCreator) { + instanceCreators.register(typeOfT, instanceCreator); return this; } @@ -567,14 +563,13 @@ public final class GsonBuilder { * method if you want to register different serializers for different generic types corresponding * to a raw type. * - * @param the type for which the serializer is being registered + * * @param typeOfT The type definition for T * @param serializer the custom serializer * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ - private GsonBuilder registerSerializer(Type typeOfT, JsonSerializer serializer, - boolean isSystem) { - serializers.register(typeOfT, serializer, isSystem); + private GsonBuilder registerSerializer(Type typeOfT, JsonSerializer serializer) { + serializers.register(typeOfT, serializer); return this; } @@ -583,14 +578,13 @@ public final class GsonBuilder { * method if you want to register different deserializers for different generic types * corresponding to a raw type. * - * @param the type for which the deserializer is being registered + * * @param typeOfT The type definition for T * @param deserializer the custom deserializer * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ - private GsonBuilder registerDeserializer(Type typeOfT, JsonDeserializer deserializer, - boolean isSystem) { - deserializers.register(typeOfT, new JsonDeserializerExceptionWrapper(deserializer), isSystem); + private GsonBuilder registerDeserializer(Type typeOfT, JsonDeserializer deserializer) { + deserializers.register(typeOfT, new JsonDeserializerExceptionWrapper(deserializer)); return this; } @@ -612,41 +606,36 @@ public final class GsonBuilder { * @since 1.7 */ public GsonBuilder registerTypeHierarchyAdapter(Class baseType, Object typeAdapter) { - return registerTypeHierarchyAdapter(baseType, typeAdapter, false); - } - - private GsonBuilder registerTypeHierarchyAdapter(Class baseType, Object typeAdapter, - boolean isSystem) { $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator); if (typeAdapter instanceof InstanceCreator) { - registerInstanceCreatorForTypeHierarchy(baseType, (InstanceCreator) typeAdapter, isSystem); + registerInstanceCreatorForTypeHierarchy(baseType, (InstanceCreator) typeAdapter); } if (typeAdapter instanceof JsonSerializer) { - registerSerializerForTypeHierarchy(baseType, (JsonSerializer) typeAdapter, isSystem); + registerSerializerForTypeHierarchy(baseType, (JsonSerializer) typeAdapter); } if (typeAdapter instanceof JsonDeserializer) { - registerDeserializerForTypeHierarchy(baseType, (JsonDeserializer) typeAdapter, isSystem); + registerDeserializerForTypeHierarchy(baseType, (JsonDeserializer) typeAdapter); } return this; } private GsonBuilder registerInstanceCreatorForTypeHierarchy(Class classOfT, - InstanceCreator instanceCreator, boolean isSystem) { - instanceCreators.registerForTypeHierarchy(classOfT, instanceCreator, isSystem); + InstanceCreator instanceCreator) { + instanceCreators.registerForTypeHierarchy(classOfT, instanceCreator); return this; } private GsonBuilder registerSerializerForTypeHierarchy(Class classOfT, - JsonSerializer serializer, boolean isSystem) { - serializers.registerForTypeHierarchy(classOfT, serializer, isSystem); + JsonSerializer serializer) { + serializers.registerForTypeHierarchy(classOfT, serializer); return this; } private GsonBuilder registerDeserializerForTypeHierarchy(Class classOfT, - JsonDeserializer deserializer, boolean isSystem) { + JsonDeserializer deserializer) { deserializers.registerForTypeHierarchy(classOfT, - new JsonDeserializerExceptionWrapper(deserializer), isSystem); + new JsonDeserializerExceptionWrapper(deserializer)); return this; } @@ -736,7 +725,7 @@ public final class GsonBuilder { private static void registerIfAbsent(Class type, ParameterizedTypeHandlerMap adapters, T adapter) { if (!adapters.hasSpecificHandlerFor(type)) { - adapters.register(type, adapter, false); + adapters.register(type, adapter); } } } diff --git a/gson/src/main/java/com/google/gson/internal/ParameterizedTypeHandlerMap.java b/gson/src/main/java/com/google/gson/internal/ParameterizedTypeHandlerMap.java index 220c1dda..a208a13e 100644 --- a/gson/src/main/java/com/google/gson/internal/ParameterizedTypeHandlerMap.java +++ b/gson/src/main/java/com/google/gson/internal/ParameterizedTypeHandlerMap.java @@ -37,28 +37,24 @@ public final class ParameterizedTypeHandlerMap { private static final Logger logger = Logger.getLogger(ParameterizedTypeHandlerMap.class.getName()); - /** - * Map that is meant for storing default type adapters - */ - private final Map systemMap = new HashMap(); + + /** Map that is meant for storing default type adapters */ private final Map userMap = new HashMap(); - /** - * List of default type hierarchy adapters - */ - private final List, T>> systemTypeHierarchyList = new ArrayList, T>>(); + + /** List of default type hierarchy adapters */ private final List, T>> userTypeHierarchyList = new ArrayList, T>>(); private boolean modifiable = true; - public synchronized void registerForTypeHierarchy(Class typeOfT, T value, boolean isSystem) { + public synchronized void registerForTypeHierarchy(Class typeOfT, T value) { Pair, T> pair = new Pair, T>(typeOfT, value); - registerForTypeHierarchy(pair, isSystem); + registerForTypeHierarchy(pair); } - public synchronized void registerForTypeHierarchy(Pair, T> pair, boolean isSystem) { + public synchronized void registerForTypeHierarchy(Pair, T> pair) { if (!modifiable) { throw new IllegalStateException("Attempted to modify an unmodifiable map."); } - List, T>> typeHierarchyList = isSystem ? systemTypeHierarchyList : userTypeHierarchyList; + List, T>> typeHierarchyList = userTypeHierarchyList; int index = getIndexOfSpecificHandlerForTypeHierarchy(pair.first, typeHierarchyList); if (index >= 0) { logger.log(Level.WARNING, "Overriding the existing type handler for {0}", pair.first); @@ -85,14 +81,14 @@ public final class ParameterizedTypeHandlerMap { return -1; } - public synchronized void register(Type typeOfT, T value, boolean isSystem) { + public synchronized void register(Type typeOfT, T value) { if (!modifiable) { throw new IllegalStateException("Attempted to modify an unmodifiable map."); } if (hasSpecificHandlerFor(typeOfT)) { logger.log(Level.WARNING, "Overriding the existing type handler for {0}", typeOfT); } - Map map = isSystem ? systemMap : userMap; + Map map = userMap; map.put(typeOfT, value); } @@ -102,12 +98,7 @@ public final class ParameterizedTypeHandlerMap { } for (Map.Entry entry : other.userMap.entrySet()) { if (!userMap.containsKey(entry.getKey())) { - register(entry.getKey(), entry.getValue(), false); - } - } - for (Map.Entry entry : other.systemMap.entrySet()) { - if (!systemMap.containsKey(entry.getKey())) { - register(entry.getKey(), entry.getValue(), true); + register(entry.getKey(), entry.getValue()); } } // Quite important to traverse the typeHierarchyList from stack bottom first since @@ -116,14 +107,7 @@ public final class ParameterizedTypeHandlerMap { Pair, T> entry = other.userTypeHierarchyList.get(i); int index = getIndexOfSpecificHandlerForTypeHierarchy(entry.first, userTypeHierarchyList); if (index < 0) { - registerForTypeHierarchy(entry, false); - } - } - for (int i = other.systemTypeHierarchyList.size()-1; i >= 0; --i) { - Pair, T> entry = other.systemTypeHierarchyList.get(i); - int index = getIndexOfSpecificHandlerForTypeHierarchy(entry.first, systemTypeHierarchyList); - if (index < 0) { - registerForTypeHierarchy(entry, true); + registerForTypeHierarchy(entry); } } } @@ -141,10 +125,6 @@ public final class ParameterizedTypeHandlerMap { return handler; } } - handler = systemMap.get(type); - if (handler != null) { - return handler; - } Class rawClass = $Gson$Types.getRawType(type); if (rawClass != type) { handler = getHandlerFor(rawClass, systemOnly); @@ -165,16 +145,11 @@ public final class ParameterizedTypeHandlerMap { } } } - for (Pair, T> entry : systemTypeHierarchyList) { - if (entry.first.isAssignableFrom(type)) { - return entry.second; - } - } return null; } public synchronized boolean hasSpecificHandlerFor(Type type) { - return userMap.containsKey(type) || systemMap.containsKey(type); + return userMap.containsKey(type); } private static int getIndexOfSpecificHandlerForTypeHierarchy( @@ -194,9 +169,7 @@ public final class ParameterizedTypeHandlerMap { // TODO (inder): Performance optimization. We can probably just share the // systemMap and systemTypeHierarchyList instead of making copies - copy.systemMap.putAll(systemMap); copy.userMap.putAll(userMap); - copy.systemTypeHierarchyList.addAll(systemTypeHierarchyList); copy.userTypeHierarchyList.addAll(userTypeHierarchyList); return copy; } @@ -205,12 +178,8 @@ public final class ParameterizedTypeHandlerMap { public String toString() { StringBuilder sb = new StringBuilder("{userTypeHierarchyList:{"); appendList(sb, userTypeHierarchyList); - sb.append("},systemTypeHierarchyList:{"); - appendList(sb, systemTypeHierarchyList); sb.append("},userMap:{"); appendMap(sb, userMap); - sb.append("},systemMap:{"); - appendMap(sb, systemMap); sb.append("}"); return sb.toString(); } diff --git a/gson/src/test/java/com/google/gson/internal/ParameterizedTypeHandlerMapTest.java b/gson/src/test/java/com/google/gson/internal/ParameterizedTypeHandlerMapTest.java index 7a6a9692..9749d75b 100644 --- a/gson/src/test/java/com/google/gson/internal/ParameterizedTypeHandlerMapTest.java +++ b/gson/src/test/java/com/google/gson/internal/ParameterizedTypeHandlerMapTest.java @@ -46,7 +46,7 @@ public class ParameterizedTypeHandlerMapTest extends TestCase { public void testHasGenericButNotSpecific() throws Exception { Type specificType = new TypeToken>() {}.getType(); String handler = "blah"; - paramMap.register(List.class, handler, false); + paramMap.register(List.class, handler); assertFalse(paramMap.hasSpecificHandlerFor(specificType)); assertTrue(paramMap.hasSpecificHandlerFor(List.class)); @@ -58,7 +58,7 @@ public class ParameterizedTypeHandlerMapTest extends TestCase { public void testHasSpecificType() throws Exception { Type specificType = new TypeToken>() {}.getType(); String handler = "blah"; - paramMap.register(specificType, handler, false); + paramMap.register(specificType, handler); assertTrue(paramMap.hasSpecificHandlerFor(specificType)); assertFalse(paramMap.hasSpecificHandlerFor(List.class)); @@ -70,8 +70,8 @@ public class ParameterizedTypeHandlerMapTest extends TestCase { public void testTypeOverridding() throws Exception { String handler1 = "blah1"; String handler2 = "blah2"; - paramMap.register(String.class, handler1, false); - paramMap.register(String.class, handler2, false); + paramMap.register(String.class, handler1); + paramMap.register(String.class, handler2); assertTrue(paramMap.hasSpecificHandlerFor(String.class)); assertEquals(handler2, paramMap.getHandlerFor(String.class, false)); @@ -80,44 +80,44 @@ public class ParameterizedTypeHandlerMapTest extends TestCase { public void testMakeUnmodifiable() throws Exception { paramMap.makeUnmodifiable(); try { - paramMap.register(String.class, "blah", false); + paramMap.register(String.class, "blah"); fail("Can not register handlers when map is unmodifiable"); } catch (IllegalStateException expected) { } } public void testTypeHierarchy() { - paramMap.registerForTypeHierarchy(Base.class, "baseHandler", false); + paramMap.registerForTypeHierarchy(Base.class, "baseHandler"); String handler = paramMap.getHandlerFor(Sub.class, false); assertEquals("baseHandler", handler); } public void testTypeHierarchyMultipleHandlers() { - paramMap.registerForTypeHierarchy(Base.class, "baseHandler", false); - paramMap.registerForTypeHierarchy(Sub.class, "subHandler", false); + paramMap.registerForTypeHierarchy(Base.class, "baseHandler"); + paramMap.registerForTypeHierarchy(Sub.class, "subHandler"); String handler = paramMap.getHandlerFor(SubOfSub.class, false); assertEquals("subHandler", handler); } public void testTypeHierarchyRegisterIfAbsent() { - paramMap.registerForTypeHierarchy(Base.class, "baseHandler", false); + paramMap.registerForTypeHierarchy(Base.class, "baseHandler"); ParameterizedTypeHandlerMap otherMap = new ParameterizedTypeHandlerMap(); - otherMap.registerForTypeHierarchy(Base.class, "baseHandler2", false); + otherMap.registerForTypeHierarchy(Base.class, "baseHandler2"); paramMap.registerIfAbsent(otherMap); String handler = paramMap.getHandlerFor(Base.class, false); assertEquals("baseHandler", handler); } public void testReplaceExistingTypeHierarchyHandler() { - paramMap.registerForTypeHierarchy(Base.class, "baseHandler", false); - paramMap.registerForTypeHierarchy(Base.class, "base2Handler", false); + paramMap.registerForTypeHierarchy(Base.class, "baseHandler"); + paramMap.registerForTypeHierarchy(Base.class, "base2Handler"); String handler = paramMap.getHandlerFor(Base.class, false); assertEquals("base2Handler", handler); } public void testHidingExistingTypeHierarchyHandlerIsDisallowed() { - paramMap.registerForTypeHierarchy(Sub.class, "subHandler", false); + paramMap.registerForTypeHierarchy(Sub.class, "subHandler"); try { - paramMap.registerForTypeHierarchy(Base.class, "baseHandler", false); + paramMap.registerForTypeHierarchy(Base.class, "baseHandler"); fail("A handler that hides an existing type hierarchy handler is not allowed"); } catch (IllegalArgumentException expected) { }