From 0c35edab6584a01d89ec5533ee6acac8f7002a8b Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Mon, 14 Mar 2011 23:22:50 +0000 Subject: [PATCH] Fixed the bug where copyOf() for a ParameterizedTypeHandlerMap was failing when hierarchical type adapters for a sub class and its base class were present. Fixed previously broken tests to verify the behavior that a hierarchical type adapter for a sub-class can be registered after registering a hierarchical type adapter for the base-class. The vice-versa is not allowed as it would result in hiding the sub-class hierarchical type adapter. --- .../com/google/gson/ParameterizedTypeHandlerMap.java | 10 ++++------ .../gson/functional/TypeHierarchyAdapterTest.java | 12 ++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/gson/src/main/java/com/google/gson/ParameterizedTypeHandlerMap.java b/gson/src/main/java/com/google/gson/ParameterizedTypeHandlerMap.java index 8458d583..ac7a2e75 100644 --- a/gson/src/main/java/com/google/gson/ParameterizedTypeHandlerMap.java +++ b/gson/src/main/java/com/google/gson/ParameterizedTypeHandlerMap.java @@ -172,12 +172,10 @@ final class ParameterizedTypeHandlerMap { public synchronized ParameterizedTypeHandlerMap copyOf() { ParameterizedTypeHandlerMap copy = new ParameterizedTypeHandlerMap(); - for (Map.Entry entry : map.entrySet()) { - copy.register(entry.getKey(), entry.getValue()); - } - for (Pair, T> entry : typeHierarchyList) { - copy.registerForTypeHierarchy(entry); - } + // Instead of individually registering entries in the map, make an efficient copy + // of the list and map + copy.map.putAll(map); + copy.typeHierarchyList.addAll(typeHierarchyList); return copy; } diff --git a/gson/src/test/java/com/google/gson/functional/TypeHierarchyAdapterTest.java b/gson/src/test/java/com/google/gson/functional/TypeHierarchyAdapterTest.java index 24ba2241..58eec7ae 100644 --- a/gson/src/test/java/com/google/gson/functional/TypeHierarchyAdapterTest.java +++ b/gson/src/test/java/com/google/gson/functional/TypeHierarchyAdapterTest.java @@ -116,7 +116,7 @@ public final class TypeHierarchyAdapterTest extends TestCase { ((Manager) company.ceo.minions[2]).minions[1].userid); } - public void testRegisterSubtypeFirst() { + public void testRegisterSuperTypeFirst() { Gson gson = new GsonBuilder() .registerTypeHierarchyAdapter(Employee.class, new EmployeeAdapter()) .registerTypeHierarchyAdapter(Manager.class, new ManagerAdapter()) @@ -131,12 +131,12 @@ public final class TypeHierarchyAdapterTest extends TestCase { assertEquals(manager.userid, copied.userid); } - public void testRegisterSupertypeFirst() { - GsonBuilder builder = new GsonBuilder() - .registerTypeHierarchyAdapter(Manager.class, new ManagerAdapter()) - .registerTypeHierarchyAdapter(Employee.class, new EmployeeAdapter()); + public void testRegisterSubTypeFirstNotAllowed() { try { - builder.create(); + Gson gson = new GsonBuilder() + .registerTypeHierarchyAdapter(Manager.class, new ManagerAdapter()) + .registerTypeHierarchyAdapter(Employee.class, new EmployeeAdapter()) + .create(); fail(); } catch (IllegalArgumentException expected) { }