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.
This commit is contained in:
Inderjeet Singh 2011-03-14 23:22:50 +00:00
parent 912add0779
commit 0c35edab65
2 changed files with 10 additions and 12 deletions

View File

@ -172,12 +172,10 @@ final class ParameterizedTypeHandlerMap<T> {
public synchronized ParameterizedTypeHandlerMap<T> copyOf() {
ParameterizedTypeHandlerMap<T> copy = new ParameterizedTypeHandlerMap<T>();
for (Map.Entry<Type, T> entry : map.entrySet()) {
copy.register(entry.getKey(), entry.getValue());
}
for (Pair<Class<?>, 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;
}

View File

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