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) { }