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.
This commit is contained in:
parent
64dc53ffc4
commit
e9a971f680
@ -88,7 +88,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
|||||||
writer.beginArray();
|
writer.beginArray();
|
||||||
for (int i = 0, length = Array.getLength(array); i < length; i++) {
|
for (int i = 0, length = Array.getLength(array); i < length; i++) {
|
||||||
final E value = (E) Array.get(array, 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 ?
|
TypeAdapter t = runtimeType != componentType ?
|
||||||
context.getAdapter(TypeToken.get(runtimeType)) : componentTypeAdapter;
|
context.getAdapter(TypeToken.get(runtimeType)) : componentTypeAdapter;
|
||||||
t.write(writer, value);
|
t.write(writer, value);
|
||||||
|
@ -107,7 +107,7 @@ public final class CollectionTypeAdapter<E> extends TypeAdapter<Collection<E>> {
|
|||||||
|
|
||||||
writer.beginArray();
|
writer.beginArray();
|
||||||
for (E element : collection) {
|
for (E element : collection) {
|
||||||
Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(elementType, collection, element);
|
Type runtimeType = Reflection.getRuntimeTypeIfMoreSpecific(elementType, element);
|
||||||
TypeAdapter t = runtimeType != elementType ?
|
TypeAdapter t = runtimeType != elementType ?
|
||||||
context.getAdapter(TypeToken.get(runtimeType)) : elementTypeAdapter;
|
context.getAdapter(TypeToken.get(runtimeType)) : elementTypeAdapter;
|
||||||
t.write(writer, element);
|
t.write(writer, element);
|
||||||
|
@ -23,15 +23,11 @@ import java.lang.reflect.TypeVariable;
|
|||||||
final class Reflection {
|
final class Reflection {
|
||||||
/**
|
/**
|
||||||
* Finds a compatible runtime type if it is more specific
|
* 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) {
|
public static Type getRuntimeTypeIfMoreSpecific(Type type, Object value) {
|
||||||
if (parent == null || child == null) {
|
if (value != null
|
||||||
return type;
|
&& (type == Object.class || type instanceof TypeVariable || type instanceof Class<?>)) {
|
||||||
}
|
type = (Class<?>) value.getClass();
|
||||||
if (type == Object.class || type instanceof TypeVariable || type instanceof Class<?>) {
|
|
||||||
type = (Class<?>) child.getClass();
|
|
||||||
}
|
}
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ public final class ReflectiveTypeAdapter<T> extends TypeAdapter<T> {
|
|||||||
throws IOException, IllegalAccessException {
|
throws IOException, IllegalAccessException {
|
||||||
Object fieldValue = field.get(value);
|
Object fieldValue = field.get(value);
|
||||||
Type declaredTypeOfField = fieldType.getType();
|
Type declaredTypeOfField = fieldType.getType();
|
||||||
Type resolvedTypeOfField = Reflection.getRuntimeTypeIfMoreSpecific(declaredTypeOfField, value, fieldValue);
|
Type resolvedTypeOfField = Reflection.getRuntimeTypeIfMoreSpecific(declaredTypeOfField, fieldValue);
|
||||||
TypeAdapter t = resolvedTypeOfField != declaredTypeOfField ?
|
TypeAdapter t = resolvedTypeOfField != declaredTypeOfField ?
|
||||||
context.getAdapter(TypeToken.get(resolvedTypeOfField)) : this.typeAdapter;
|
context.getAdapter(TypeToken.get(resolvedTypeOfField)) : this.typeAdapter;
|
||||||
t.write(writer, fieldValue);
|
t.write(writer, fieldValue);
|
||||||
|
Loading…
Reference in New Issue
Block a user