From e9a971f6806e5bebd2f6e677c3b126efad68a01c Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 5 Aug 2011 00:13:01 +0000 Subject: [PATCH] Revised getRuntimeTypeIfMoreSpecific to ignore the parent and just focus on the value. All uses of this method have already made a determination about the parent. --- .../google/gson/internal/bind/ArrayTypeAdapter.java | 2 +- .../gson/internal/bind/CollectionTypeAdapter.java | 2 +- .../com/google/gson/internal/bind/Reflection.java | 12 ++++-------- .../gson/internal/bind/ReflectiveTypeAdapter.java | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java index c0a710a3..dbe82cf9 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java @@ -88,7 +88,7 @@ public final class ArrayTypeAdapter extends TypeAdapter { writer.beginArray(); for (int i = 0, length = Array.getLength(array); i < length; i++) { final E value = (E) Array.get(array, i); - Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(componentType, array, value); + Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(componentType, value); TypeAdapter t = runtimeType != componentType ? context.getAdapter(TypeToken.get(runtimeType)) : componentTypeAdapter; t.write(writer, value); diff --git a/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapter.java index 33e96be7..c69c69be 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapter.java @@ -107,7 +107,7 @@ public final class CollectionTypeAdapter extends TypeAdapter> { writer.beginArray(); for (E element : collection) { - Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(elementType, collection, element); + Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(elementType, element); TypeAdapter t = runtimeType != elementType ? context.getAdapter(TypeToken.get(runtimeType)) : elementTypeAdapter; t.write(writer, element); diff --git a/gson/src/main/java/com/google/gson/internal/bind/Reflection.java b/gson/src/main/java/com/google/gson/internal/bind/Reflection.java index 5d18cde5..2b30a119 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/Reflection.java +++ b/gson/src/main/java/com/google/gson/internal/bind/Reflection.java @@ -23,15 +23,11 @@ import java.lang.reflect.TypeVariable; final class Reflection { /** * Finds a compatible runtime type if it is more specific - * In case of a field of an object, parent is the object instance, and child is the field value. - * In case of an Array, parent is the array instance, and the child is the array element. */ - public static Type getRuntimeTypeIfMoreSpecific(Type type, Object parent, Object child) { - if (parent == null || child == null) { - return type; - } - if (type == Object.class || type instanceof TypeVariable || type instanceof Class) { - type = (Class) child.getClass(); + public static Type getRuntimeTypeIfMoreSpecific(Type type, Object value) { + if (value != null + && (type == Object.class || type instanceof TypeVariable || type instanceof Class)) { + type = (Class) value.getClass(); } return type; } diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java index a036b357..d48c1a0a 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapter.java @@ -122,7 +122,7 @@ public final class ReflectiveTypeAdapter extends TypeAdapter { throws IOException, IllegalAccessException { Object fieldValue = field.get(value); Type declaredTypeOfField = fieldType.getType(); - Type resolvedTypeOfField = Reflection.getRuntimeTypeIfMoreSpecific(declaredTypeOfField, value, fieldValue); + Type resolvedTypeOfField = Reflection.getRuntimeTypeIfMoreSpecific(declaredTypeOfField, fieldValue); TypeAdapter t = resolvedTypeOfField != declaredTypeOfField ? context.getAdapter(TypeToken.get(resolvedTypeOfField)) : this.typeAdapter; t.write(writer, fieldValue);