This commit is contained in:
Redkale
2018-02-11 11:23:36 +08:00
parent 4b9cb3716b
commit ae9be7ac25
3 changed files with 26 additions and 20 deletions

View File

@@ -95,7 +95,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
if (factory.isConvertDisabled(field)) continue; if (factory.isConvertDisabled(field)) continue;
ref = factory.findRef(field); ref = factory.findRef(field);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), clazz), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t)); DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t));
if (ref != null) member.index = ref.getIndex(); if (ref != null) member.index = ref.getIndex();
list.add(member); list.add(member);
@@ -120,7 +120,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
} }
ref = factory.findRef(method); ref = factory.findRef(method);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericParameterTypes()[0], clazz), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericParameterTypes()[0], this.type), this.type);
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, null, method), factory.loadDecoder(t)); DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, null, method), factory.loadDecoder(t));
if (ref != null) member.index = ref.getIndex(); if (ref != null) member.index = ref.getIndex();
list.add(member); list.add(member);
@@ -150,7 +150,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
} catch (NoSuchMethodException ex) { } catch (NoSuchMethodException ex) {
getter = clazz.getMethod("is" + mn); getter = clazz.getMethod("is" + mn);
} }
Type t = TypeToken.createClassType(TypeToken.getGenericType(getter.getGenericParameterTypes()[0], clazz), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(getter.getGenericParameterTypes()[0], this.type), this.type);
list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, getter, null), factory.loadDecoder(t))); list.add(new DeMember(ObjectEncoder.createAttribute(factory, clazz, null, getter, null), factory.loadDecoder(t)));
} }
} }

View File

@@ -77,7 +77,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
if (factory.isConvertDisabled(field)) continue; if (factory.isConvertDisabled(field)) continue;
ref = factory.findRef(field); ref = factory.findRef(field);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), clazz), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
EnMember member = new EnMember(createAttribute(factory, clazz, field, null, null), factory.loadEncoder(t)); EnMember member = new EnMember(createAttribute(factory, clazz, field, null, null), factory.loadEncoder(t));
if (ref != null) member.index = ref.getIndex(); if (ref != null) member.index = ref.getIndex();
list.add(member); list.add(member);
@@ -102,7 +102,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
} }
ref = factory.findRef(method); ref = factory.findRef(method);
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), clazz), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), this.type), this.type);
EnMember member = new EnMember(createAttribute(factory, clazz, null, method, null), factory.loadEncoder(t)); EnMember member = new EnMember(createAttribute(factory, clazz, null, method, null), factory.loadEncoder(t));
if (ref != null) member.index = ref.getIndex(); if (ref != null) member.index = ref.getIndex();
list.add(member); list.add(member);

View File

@@ -98,23 +98,29 @@ public abstract class TypeToken<T> {
public static Type getGenericType(final Type type, final Type declaringClass) { public static Type getGenericType(final Type type, final Type declaringClass) {
if (type == null || declaringClass == null) return type; if (type == null || declaringClass == null) return type;
if (type instanceof TypeVariable) { if (type instanceof TypeVariable) {
Type superType = null;
Class declaringClass0 = null;
if (declaringClass instanceof Class) { if (declaringClass instanceof Class) {
final Class declaringClass0 = (Class) declaringClass; declaringClass0 = (Class) declaringClass;
Type superType = declaringClass0.getGenericSuperclass(); superType = declaringClass0.getGenericSuperclass();
while (superType instanceof Class && superType != Object.class) superType = ((Class) superType).getGenericSuperclass(); while (superType instanceof Class && superType != Object.class) superType = ((Class) superType).getGenericSuperclass();
if (superType instanceof ParameterizedType) { } else if (declaringClass instanceof ParameterizedType) {
ParameterizedType superPT = (ParameterizedType) superType; superType = declaringClass;
Type[] atas = superPT.getActualTypeArguments(); Type rawType = ((ParameterizedType) declaringClass).getRawType();
Class ss = declaringClass0; if (rawType instanceof Class) declaringClass0 = (Class) rawType;
TypeVariable[] asts = ss.getTypeParameters(); }
while (atas.length != asts.length && ss != Object.class) { if (declaringClass0 != null && superType instanceof ParameterizedType) {
ss = ss.getSuperclass(); ParameterizedType superPT = (ParameterizedType) superType;
asts = ss.getTypeParameters(); Type[] atas = superPT.getActualTypeArguments();
} Class ss = declaringClass0;
if (atas.length == asts.length) { TypeVariable[] asts = ss.getTypeParameters();
for (int i = 0; i < asts.length; i++) { while (atas.length != asts.length && ss != Object.class) {
if (asts[i] == type) return atas[i]; ss = ss.getSuperclass();
} asts = ss.getTypeParameters();
}
if (atas.length == asts.length) {
for (int i = 0; i < asts.length; i++) {
if (asts[i] == type) return atas[i];
} }
} }
} }