|
2 | 2 |
|
3 | 3 | import java.lang.annotation.Annotation;
|
4 | 4 | import java.lang.reflect.Field;
|
| 5 | +import java.lang.reflect.GenericDeclaration; |
5 | 6 | import java.lang.reflect.InvocationTargetException;
|
6 | 7 | import java.lang.reflect.Method;
|
7 | 8 | import java.lang.reflect.ParameterizedType;
|
@@ -69,9 +70,19 @@ public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type t
|
69 | 70 | fieldType = field.getGenericType();
|
70 | 71 | this.declaringClass = field.getDeclaringClass();
|
71 | 72 | }
|
| 73 | + |
| 74 | + if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) { |
| 75 | + TypeVariable<?> tv = (TypeVariable<?>) fieldType; |
| 76 | + Type genericFieldType = getInheritGenericType(clazz, tv); |
| 77 | + if (genericFieldType != null) { |
| 78 | + this.fieldClass = TypeUtils.getClass(genericFieldType); |
| 79 | + this.fieldType = genericFieldType; |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
72 | 83 |
|
73 | 84 | Type genericFieldType = getFieldType(clazz, type, fieldType);
|
74 |
| - |
| 85 | + |
75 | 86 | if (genericFieldType != fieldType) {
|
76 | 87 | if (genericFieldType instanceof ParameterizedType) {
|
77 | 88 | fieldClass = TypeUtils.getClass(genericFieldType);
|
@@ -107,6 +118,28 @@ public static Type getFieldType(Class<?> clazz, Type type, Type fieldType) {
|
107 | 118 |
|
108 | 119 | return fieldType;
|
109 | 120 | }
|
| 121 | + |
| 122 | + public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) { |
| 123 | + Type type = null; |
| 124 | + GenericDeclaration gd = tv.getGenericDeclaration(); |
| 125 | + do { |
| 126 | + type = clazz.getGenericSuperclass(); |
| 127 | + if (type instanceof ParameterizedType) { |
| 128 | + ParameterizedType ptype = (ParameterizedType) type; |
| 129 | + if (ptype.getRawType() == gd) { |
| 130 | + TypeVariable<?>[] tvs = gd.getTypeParameters(); |
| 131 | + Type[] types = ptype.getActualTypeArguments(); |
| 132 | + for (int i = 0; i < tvs.length; i++) { |
| 133 | + if (tvs[i] == tv) |
| 134 | + return types[i]; |
| 135 | + } |
| 136 | + return null; |
| 137 | + } |
| 138 | + } |
| 139 | + clazz = TypeUtils.getClass(type); |
| 140 | + } while (type != null); |
| 141 | + return null; |
| 142 | + } |
110 | 143 |
|
111 | 144 | public String toString() {
|
112 | 145 | return this.name;
|
|
0 commit comments