Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 30, 2017
1 parent 54ec28b commit 0013450
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 0013450

Please sign in to comment.