@@ -32,51 +32,43 @@ public class TypeParameterResolver {
32
32
* @return The return type of the method as {@link Type}. If it has type parameters in the declaration,<br>
33
33
* they will be resolved to the actual runtime {@link Type}.
34
34
*/
35
- public static Type resolveReturnType (Method method , Type mapper ) {
35
+ public static Type resolveReturnType (Method method , Type srcType ) {
36
36
Type returnType = method .getGenericReturnType ();
37
37
Class <?> declaringClass = method .getDeclaringClass ();
38
- Type result = null ;
39
- if (returnType instanceof TypeVariable ) {
40
- result = resolveTypeVar ((TypeVariable <?>) returnType , mapper , declaringClass );
41
- } else if (returnType instanceof ParameterizedType ) {
42
- result = resolveParameterizedType ((ParameterizedType ) returnType , mapper , declaringClass );
43
- } else if (returnType instanceof GenericArrayType ) {
44
- result = resolveGenericArrayType ((GenericArrayType ) returnType , mapper , declaringClass );
45
- } else {
46
- result = returnType ;
47
- }
48
- return result ;
38
+ return resolveType (returnType , srcType , declaringClass );
49
39
}
50
40
51
- public static Type [] resolveParamTypes (Method method , Type mapper ) {
41
+ public static Type [] resolveParamTypes (Method method , Type srcType ) {
52
42
Type [] paramTypes = method .getGenericParameterTypes ();
53
43
Class <?> declaringClass = method .getDeclaringClass ();
54
44
Type [] result = new Type [paramTypes .length ];
55
45
for (int i = 0 ; i < paramTypes .length ; i ++) {
56
- if (paramTypes [i ] instanceof Class ) {
57
- result [i ] = paramTypes [i ];
58
- } else if (paramTypes [i ] instanceof TypeVariable ) {
59
- result [i ] = resolveTypeVar ((TypeVariable <?>) paramTypes [i ], mapper , declaringClass );
60
- } else if (paramTypes [i ] instanceof ParameterizedType ) {
61
- result [i ] = resolveParameterizedType ((ParameterizedType ) paramTypes [i ], mapper , declaringClass );
62
- } else if (paramTypes [i ] instanceof GenericArrayType ) {
63
- result [i ] = resolveGenericArrayType ((GenericArrayType ) paramTypes [i ], mapper , declaringClass );
64
- } else {
65
- // should we support other types?
66
- }
46
+ result [i ] = resolveType (paramTypes [i ], srcType , declaringClass );
67
47
}
68
48
return result ;
69
49
}
70
50
71
- private static Type resolveGenericArrayType (GenericArrayType genericArrayType , Type mapper , Class <?> declaringClass ) {
51
+ private static Type resolveType (Type type , Type srcType , Class <?> declaringClass ) {
52
+ if (type instanceof TypeVariable ) {
53
+ return resolveTypeVar ((TypeVariable <?>) type , srcType , declaringClass );
54
+ } else if (type instanceof ParameterizedType ) {
55
+ return resolveParameterizedType ((ParameterizedType ) type , srcType , declaringClass );
56
+ } else if (type instanceof GenericArrayType ) {
57
+ return resolveGenericArrayType ((GenericArrayType ) type , srcType , declaringClass );
58
+ } else {
59
+ return type ;
60
+ }
61
+ }
62
+
63
+ private static Type resolveGenericArrayType (GenericArrayType genericArrayType , Type srcType , Class <?> declaringClass ) {
72
64
Type componentType = genericArrayType .getGenericComponentType ();
73
65
Type resolvedComponentType = null ;
74
66
if (componentType instanceof TypeVariable ) {
75
- resolvedComponentType = resolveTypeVar ((TypeVariable <?>) componentType , mapper , declaringClass );
67
+ resolvedComponentType = resolveTypeVar ((TypeVariable <?>) componentType , srcType , declaringClass );
76
68
} else if (componentType instanceof GenericArrayType ) {
77
- resolvedComponentType = resolveGenericArrayType ((GenericArrayType ) componentType , mapper , declaringClass );
69
+ resolvedComponentType = resolveGenericArrayType ((GenericArrayType ) componentType , srcType , declaringClass );
78
70
} else if (componentType instanceof ParameterizedType ) {
79
- resolvedComponentType = resolveParameterizedType ((ParameterizedType ) componentType , mapper , declaringClass );
71
+ resolvedComponentType = resolveParameterizedType ((ParameterizedType ) componentType , srcType , declaringClass );
80
72
}
81
73
if (resolvedComponentType instanceof Class ) {
82
74
return Array .newInstance ((Class <?>) resolvedComponentType , 0 ).getClass ();
@@ -85,79 +77,79 @@ private static Type resolveGenericArrayType(GenericArrayType genericArrayType, T
85
77
}
86
78
}
87
79
88
- private static ParameterizedType resolveParameterizedType (ParameterizedType parameterizedType , Type mapper , Class <?> declaringClass ) {
80
+ private static ParameterizedType resolveParameterizedType (ParameterizedType parameterizedType , Type srcType , Class <?> declaringClass ) {
89
81
Class <?> rawType = (Class <?>) parameterizedType .getRawType ();
90
82
Type [] typeArgs = parameterizedType .getActualTypeArguments ();
91
83
Type [] args = new Type [typeArgs .length ];
92
84
for (int i = 0 ; i < typeArgs .length ; i ++) {
93
85
if (typeArgs [i ] instanceof TypeVariable ) {
94
- args [i ] = resolveTypeVar ((TypeVariable <?>) typeArgs [i ], mapper , declaringClass );
86
+ args [i ] = resolveTypeVar ((TypeVariable <?>) typeArgs [i ], srcType , declaringClass );
95
87
} else if (typeArgs [i ] instanceof ParameterizedType ) {
96
- args [i ] = resolveParameterizedType ((ParameterizedType ) typeArgs [i ], mapper , declaringClass );
88
+ args [i ] = resolveParameterizedType ((ParameterizedType ) typeArgs [i ], srcType , declaringClass );
97
89
} else if (typeArgs [i ] instanceof WildcardType ) {
98
- args [i ] = resolveWildcardType ((WildcardType ) typeArgs [i ], mapper , declaringClass );
90
+ args [i ] = resolveWildcardType ((WildcardType ) typeArgs [i ], srcType , declaringClass );
99
91
} else {
100
92
args [i ] = typeArgs [i ];
101
93
}
102
94
}
103
95
return new ParameterizedTypeImpl (rawType , null , args );
104
96
}
105
97
106
- private static Type resolveWildcardType (WildcardType wildcardType , Type mapper , Class <?> declaringClass ) {
107
- Type [] lowerBounds = resolveWildcardTypeBounds (wildcardType .getLowerBounds (), mapper , declaringClass );
108
- Type [] upperBounds = resolveWildcardTypeBounds (wildcardType .getUpperBounds (), mapper , declaringClass );
98
+ private static Type resolveWildcardType (WildcardType wildcardType , Type srcType , Class <?> declaringClass ) {
99
+ Type [] lowerBounds = resolveWildcardTypeBounds (wildcardType .getLowerBounds (), srcType , declaringClass );
100
+ Type [] upperBounds = resolveWildcardTypeBounds (wildcardType .getUpperBounds (), srcType , declaringClass );
109
101
return new WildcardTypeImpl (lowerBounds , upperBounds );
110
102
}
111
103
112
- private static Type [] resolveWildcardTypeBounds (Type [] bounds , Type mapper , Class <?> declaringClass ) {
104
+ private static Type [] resolveWildcardTypeBounds (Type [] bounds , Type srcType , Class <?> declaringClass ) {
113
105
Type [] result = new Type [bounds .length ];
114
106
for (int i = 0 ; i < bounds .length ; i ++) {
115
107
if (bounds [i ] instanceof TypeVariable ) {
116
- result [i ] = resolveTypeVar ((TypeVariable <?>) bounds [i ], mapper , declaringClass );
108
+ result [i ] = resolveTypeVar ((TypeVariable <?>) bounds [i ], srcType , declaringClass );
117
109
} else if (bounds [i ] instanceof ParameterizedType ) {
118
- result [i ] = resolveParameterizedType ((ParameterizedType ) bounds [i ], mapper , declaringClass );
110
+ result [i ] = resolveParameterizedType ((ParameterizedType ) bounds [i ], srcType , declaringClass );
119
111
} else if (bounds [i ] instanceof WildcardType ) {
120
- result [i ] = resolveWildcardType ((WildcardType ) bounds [i ], mapper , declaringClass );
112
+ result [i ] = resolveWildcardType ((WildcardType ) bounds [i ], srcType , declaringClass );
121
113
} else {
122
114
result [i ] = bounds [i ];
123
115
}
124
116
}
125
117
return result ;
126
118
}
127
119
128
- private static Type resolveTypeVar (TypeVariable <?> typeVar , Type type , Class <?> declaringClass ) {
120
+ private static Type resolveTypeVar (TypeVariable <?> typeVar , Type srcType , Class <?> declaringClass ) {
129
121
Type result = null ;
130
122
Class <?> clazz = null ;
131
- if (type instanceof Class ) {
132
- clazz = (Class <?>) type ;
133
- } else if (type instanceof ParameterizedType ) {
134
- ParameterizedType parameterizedType = (ParameterizedType ) type ;
123
+ if (srcType instanceof Class ) {
124
+ clazz = (Class <?>) srcType ;
125
+ } else if (srcType instanceof ParameterizedType ) {
126
+ ParameterizedType parameterizedType = (ParameterizedType ) srcType ;
135
127
clazz = (Class <?>) parameterizedType .getRawType ();
136
128
} else {
137
- throw new IllegalArgumentException ("The 2nd arg must be Class or ParameterizedType, but was: " + type .getClass ());
129
+ throw new IllegalArgumentException ("The 2nd arg must be Class or ParameterizedType, but was: " + srcType .getClass ());
138
130
}
139
131
140
132
if (clazz == declaringClass ) {
141
133
return Object .class ;
142
134
}
143
135
144
136
Type superclass = clazz .getGenericSuperclass ();
145
- result = scanSuperTypes (typeVar , type , declaringClass , clazz , superclass );
137
+ result = scanSuperTypes (typeVar , srcType , declaringClass , clazz , superclass );
146
138
if (result != null ) {
147
139
return result ;
148
140
}
149
141
150
142
Type [] superInterfaces = clazz .getGenericInterfaces ();
151
143
for (Type superInterface : superInterfaces ) {
152
- result = scanSuperTypes (typeVar , type , declaringClass , clazz , superInterface );
144
+ result = scanSuperTypes (typeVar , srcType , declaringClass , clazz , superInterface );
153
145
if (result != null ) {
154
146
return result ;
155
147
}
156
148
}
157
149
return Object .class ;
158
150
}
159
151
160
- private static Type scanSuperTypes (TypeVariable <?> typeVar , Type type , Class <?> declaringClass , Class <?> clazz , Type superclass ) {
152
+ private static Type scanSuperTypes (TypeVariable <?> typeVar , Type srcType , Class <?> declaringClass , Class <?> clazz , Type superclass ) {
161
153
Type result = null ;
162
154
if (superclass instanceof ParameterizedType ) {
163
155
ParameterizedType parentAsType = (ParameterizedType ) superclass ;
@@ -171,8 +163,8 @@ private static Type scanSuperTypes(TypeVariable<?> typeVar, Type type, Class<?>
171
163
TypeVariable <?>[] typeParams = clazz .getTypeParameters ();
172
164
for (int j = 0 ; j < typeParams .length ; j ++) {
173
165
if (typeParams [j ] == typeArgs [i ]) {
174
- if (type instanceof ParameterizedType ) {
175
- result = ((ParameterizedType ) type ).getActualTypeArguments ()[j ];
166
+ if (srcType instanceof ParameterizedType ) {
167
+ result = ((ParameterizedType ) srcType ).getActualTypeArguments ()[j ];
176
168
}
177
169
break ;
178
170
}
0 commit comments