From 001345062033495928c429093da9065f14a6c0e9 Mon Sep 17 00:00:00 2001 From: wenshao Date: Wed, 31 May 2017 00:10:44 +0800 Subject: [PATCH] refactor --- .../com/alibaba/fastjson/util/FieldInfo.java | 67 +++++++++---------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/alibaba/fastjson/util/FieldInfo.java b/src/main/java/com/alibaba/fastjson/util/FieldInfo.java index dce21159fd..8eef8cec1d 100755 --- a/src/main/java/com/alibaba/fastjson/util/FieldInfo.java +++ b/src/main/java/com/alibaba/fastjson/util/FieldInfo.java @@ -174,7 +174,36 @@ public FieldInfo(String name, // && fieldType instanceof TypeVariable) { TypeVariable tv = (TypeVariable) fieldType; - Type genericFieldType = getInheritGenericType(clazz, type, tv); + Type genericFieldType = null; + { + Type[] arguments = null; + if (type instanceof ParameterizedType) { + ParameterizedType ptype = (ParameterizedType) type; + arguments = ptype.getActualTypeArguments(); + } + + for (Class c = clazz; c != null && c != Object.class && c != declaringClass; c = c.getSuperclass()) { + Type superType = c.getGenericSuperclass(); + + if (superType instanceof ParameterizedType) { + ParameterizedType p_superType = (ParameterizedType) superType; + Type[] p_superType_args = p_superType.getActualTypeArguments(); + getArgument(p_superType_args, c.getTypeParameters(), arguments); + arguments = p_superType_args; + } + } + + if (arguments != null) { + TypeVariable[] typeVariables = declaringClass.getTypeParameters(); + for (int j = 0; j < typeVariables.length; ++j) { + if (tv.equals(typeVariables[j])) { + genericFieldType = arguments[j]; + break; + } + } + } + } + if (genericFieldType != null) { this.fieldClass = TypeUtils.getClass(genericFieldType); this.fieldType = genericFieldType; @@ -309,42 +338,6 @@ private static boolean getArgument(Type[] typeArgs, TypeVariable[] typeVariables return changed; } - private static Type getInheritGenericType(Class clazz, Type type, TypeVariable tv) { - Class gd = (Class) tv.getGenericDeclaration(); - - Type[] arguments = null; - if (type instanceof ParameterizedType) { - ParameterizedType ptype = (ParameterizedType) type; - arguments = ptype.getActualTypeArguments(); - } - - for (Class c = clazz; c != null && c != Object.class && c != gd; c = c.getSuperclass()) { - Type superType = c.getGenericSuperclass(); - - if (superType instanceof ParameterizedType) { - ParameterizedType p_superType = (ParameterizedType) superType; - Type[] p_superType_args = p_superType.getActualTypeArguments(); - getArgument(p_superType_args, c.getTypeParameters(), arguments); - arguments = p_superType_args; - } - } - - if (arguments == null) { - return null; - } - - Type actualType = null; - TypeVariable[] typeVariables = gd.getTypeParameters(); - for (int j = 0; j < typeVariables.length; ++j) { - if (tv.equals(typeVariables[j])) { - actualType = arguments[j]; - break; - } - } - - return actualType; - } - public String toString() { return this.name; }