1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.util.generics; |
11 | |
|
12 | |
import java.lang.ref.Reference; |
13 | |
import java.lang.ref.WeakReference; |
14 | |
import java.lang.reflect.GenericArrayType; |
15 | |
import java.lang.reflect.Method; |
16 | |
import java.lang.reflect.ParameterizedType; |
17 | |
import java.lang.reflect.Type; |
18 | |
import java.lang.reflect.TypeVariable; |
19 | |
import java.util.Collections; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.Map; |
22 | |
import java.util.WeakHashMap; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public abstract class GenericTypeResolver |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | private static final Map<Class, Reference<Map<TypeVariable, Type>>> typeVariableCache = |
39 | |
Collections.synchronizedMap(new WeakHashMap<Class, Reference<Map<TypeVariable, Type>>>()); |
40 | |
|
41 | |
|
42 | |
private GenericTypeResolver() |
43 | 0 | { |
44 | |
|
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static Type getTargetType(MethodParameter methodParam) |
54 | |
{ |
55 | 0 | if (methodParam.getConstructor() != null) |
56 | |
{ |
57 | 0 | return methodParam.getConstructor().getGenericParameterTypes()[methodParam.getParameterIndex()]; |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | if (methodParam.getParameterIndex() >= 0) |
62 | |
{ |
63 | 0 | return methodParam.getMethod().getGenericParameterTypes()[methodParam.getParameterIndex()]; |
64 | |
} |
65 | |
else |
66 | |
{ |
67 | 0 | return methodParam.getMethod().getGenericReturnType(); |
68 | |
} |
69 | |
} |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public static Class<?> resolveParameterType(MethodParameter methodParam, Class clazz) |
80 | |
{ |
81 | 0 | Type genericType = getTargetType(methodParam); |
82 | 0 | Map<TypeVariable, Type> typeVariableMap = getTypeVariableMap(clazz); |
83 | 0 | Type rawType = getRawType(genericType, typeVariableMap); |
84 | 0 | Class result = (rawType instanceof Class ? (Class) rawType : methodParam.getParameterType()); |
85 | 0 | methodParam.setParameterType(result); |
86 | 0 | methodParam.typeVariableMap = typeVariableMap; |
87 | 0 | return result; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public static Class<?> resolveReturnType(Method method, Class clazz) |
98 | |
{ |
99 | 0 | Type genericType = method.getGenericReturnType(); |
100 | 0 | Map<TypeVariable, Type> typeVariableMap = getTypeVariableMap(clazz); |
101 | 0 | Type rawType = getRawType(genericType, typeVariableMap); |
102 | 0 | return (rawType instanceof Class ? (Class) rawType : method.getReturnType()); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public static Class<?> resolveTypeArgument(Class clazz, Class genericIfc) |
115 | |
{ |
116 | 0 | Class[] typeArgs = resolveTypeArguments(clazz, genericIfc); |
117 | 0 | if (typeArgs == null) |
118 | |
{ |
119 | 0 | return null; |
120 | |
} |
121 | 0 | if (typeArgs.length != 1) |
122 | |
{ |
123 | 0 | throw new IllegalArgumentException("Expected 1 type argument on generic interface [" + |
124 | |
genericIfc.getName() + "] but found " + typeArgs.length); |
125 | |
} |
126 | 0 | return typeArgs[0]; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public static Class[] resolveTypeArguments(Class clazz, Class genericIfc) |
140 | |
{ |
141 | 0 | return doResolveTypeArguments(clazz, clazz, genericIfc); |
142 | |
} |
143 | |
|
144 | |
private static Class[] doResolveTypeArguments(Class ownerClass, Class classToIntrospect, Class genericIfc) |
145 | |
{ |
146 | 0 | while (classToIntrospect != null) |
147 | |
{ |
148 | 0 | Type[] ifcs = classToIntrospect.getGenericInterfaces(); |
149 | 0 | for (Type ifc : ifcs) |
150 | |
{ |
151 | 0 | if (ifc instanceof ParameterizedType) |
152 | |
{ |
153 | 0 | ParameterizedType paramIfc = (ParameterizedType) ifc; |
154 | 0 | Type rawType = paramIfc.getRawType(); |
155 | 0 | if (genericIfc.equals(rawType)) |
156 | |
{ |
157 | 0 | Type[] typeArgs = paramIfc.getActualTypeArguments(); |
158 | 0 | Class[] result = new Class[typeArgs.length]; |
159 | 0 | for (int i = 0; i < typeArgs.length; i++) |
160 | |
{ |
161 | 0 | Type arg = typeArgs[i]; |
162 | 0 | if (arg instanceof TypeVariable) |
163 | |
{ |
164 | 0 | arg = getTypeVariableMap(ownerClass).get((TypeVariable) arg); |
165 | |
} |
166 | 0 | result[i] = (arg instanceof Class ? (Class) arg : Object.class); |
167 | |
} |
168 | 0 | return result; |
169 | |
} |
170 | 0 | else if (genericIfc.isAssignableFrom((Class) rawType)) |
171 | |
{ |
172 | 0 | return doResolveTypeArguments(ownerClass, (Class) rawType, genericIfc); |
173 | |
} |
174 | 0 | } |
175 | 0 | else if (genericIfc.isAssignableFrom((Class) ifc)) |
176 | |
{ |
177 | 0 | return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc); |
178 | |
} |
179 | |
} |
180 | 0 | classToIntrospect = classToIntrospect.getSuperclass(); |
181 | 0 | } |
182 | 0 | return null; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
static Class resolveType(Type genericType, Map<TypeVariable, Type> typeVariableMap) |
194 | |
{ |
195 | 0 | Type rawType = getRawType(genericType, typeVariableMap); |
196 | 0 | return (rawType instanceof Class ? (Class) rawType : Object.class); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
static Type getRawType(Type genericType, Map<TypeVariable, Type> typeVariableMap) |
207 | |
{ |
208 | 0 | Type resolvedType = genericType; |
209 | 0 | if (genericType instanceof TypeVariable) |
210 | |
{ |
211 | 0 | TypeVariable tv = (TypeVariable) genericType; |
212 | 0 | resolvedType = typeVariableMap.get(tv); |
213 | 0 | if (resolvedType == null) |
214 | |
{ |
215 | 0 | resolvedType = extractBoundForTypeVariable(tv); |
216 | |
} |
217 | |
} |
218 | 0 | if (resolvedType instanceof ParameterizedType) |
219 | |
{ |
220 | 0 | return ((ParameterizedType) resolvedType).getRawType(); |
221 | |
} |
222 | |
else |
223 | |
{ |
224 | 0 | return resolvedType; |
225 | |
} |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) |
234 | |
{ |
235 | 0 | Reference<Map<TypeVariable, Type>> ref = typeVariableCache.get(clazz); |
236 | 0 | Map<TypeVariable, Type> typeVariableMap = (ref != null ? ref.get() : null); |
237 | |
|
238 | 0 | if (typeVariableMap == null) |
239 | |
{ |
240 | 0 | typeVariableMap = new HashMap<TypeVariable, Type>(); |
241 | |
|
242 | |
|
243 | 0 | extractTypeVariablesFromGenericInterfaces(clazz.getGenericInterfaces(), typeVariableMap); |
244 | |
|
245 | |
|
246 | 0 | Type genericType = clazz.getGenericSuperclass(); |
247 | 0 | Class type = clazz.getSuperclass(); |
248 | 0 | while (type != null && !Object.class.equals(type)) |
249 | |
{ |
250 | 0 | if (genericType instanceof ParameterizedType) |
251 | |
{ |
252 | 0 | ParameterizedType pt = (ParameterizedType) genericType; |
253 | 0 | populateTypeMapFromParameterizedType(pt, typeVariableMap); |
254 | |
} |
255 | 0 | extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), typeVariableMap); |
256 | 0 | genericType = type.getGenericSuperclass(); |
257 | 0 | type = type.getSuperclass(); |
258 | |
} |
259 | |
|
260 | |
|
261 | 0 | type = clazz; |
262 | 0 | while (type.isMemberClass()) |
263 | |
{ |
264 | 0 | genericType = type.getGenericSuperclass(); |
265 | 0 | if (genericType instanceof ParameterizedType) |
266 | |
{ |
267 | 0 | ParameterizedType pt = (ParameterizedType) genericType; |
268 | 0 | populateTypeMapFromParameterizedType(pt, typeVariableMap); |
269 | |
} |
270 | 0 | type = type.getEnclosingClass(); |
271 | |
} |
272 | |
|
273 | 0 | typeVariableCache.put(clazz, new WeakReference<Map<TypeVariable, Type>>(typeVariableMap)); |
274 | |
} |
275 | |
|
276 | 0 | return typeVariableMap; |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
static Type extractBoundForTypeVariable(TypeVariable typeVariable) |
283 | |
{ |
284 | 0 | Type[] bounds = typeVariable.getBounds(); |
285 | 0 | if (bounds.length == 0) |
286 | |
{ |
287 | 0 | return Object.class; |
288 | |
} |
289 | 0 | Type bound = bounds[0]; |
290 | 0 | if (bound instanceof TypeVariable) |
291 | |
{ |
292 | 0 | bound = extractBoundForTypeVariable((TypeVariable) bound); |
293 | |
} |
294 | 0 | return bound; |
295 | |
} |
296 | |
|
297 | |
private static void extractTypeVariablesFromGenericInterfaces(Type[] genericInterfaces, Map<TypeVariable, Type> typeVariableMap) |
298 | |
{ |
299 | 0 | for (Type genericInterface : genericInterfaces) |
300 | |
{ |
301 | 0 | if (genericInterface instanceof ParameterizedType) |
302 | |
{ |
303 | 0 | ParameterizedType pt = (ParameterizedType) genericInterface; |
304 | 0 | populateTypeMapFromParameterizedType(pt, typeVariableMap); |
305 | 0 | if (pt.getRawType() instanceof Class) |
306 | |
{ |
307 | 0 | extractTypeVariablesFromGenericInterfaces( |
308 | |
((Class) pt.getRawType()).getGenericInterfaces(), typeVariableMap); |
309 | |
} |
310 | 0 | } |
311 | 0 | else if (genericInterface instanceof Class) |
312 | |
{ |
313 | 0 | extractTypeVariablesFromGenericInterfaces( |
314 | |
((Class) genericInterface).getGenericInterfaces(), typeVariableMap); |
315 | |
} |
316 | |
} |
317 | 0 | } |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
private static void populateTypeMapFromParameterizedType(ParameterizedType type, Map<TypeVariable, Type> typeVariableMap) |
336 | |
{ |
337 | 0 | if (type.getRawType() instanceof Class) |
338 | |
{ |
339 | 0 | Type[] actualTypeArguments = type.getActualTypeArguments(); |
340 | 0 | TypeVariable[] typeVariables = ((Class) type.getRawType()).getTypeParameters(); |
341 | 0 | for (int i = 0; i < actualTypeArguments.length; i++) |
342 | |
{ |
343 | 0 | Type actualTypeArgument = actualTypeArguments[i]; |
344 | 0 | TypeVariable variable = typeVariables[i]; |
345 | 0 | if (actualTypeArgument instanceof Class) |
346 | |
{ |
347 | 0 | typeVariableMap.put(variable, actualTypeArgument); |
348 | |
} |
349 | 0 | else if (actualTypeArgument instanceof GenericArrayType) |
350 | |
{ |
351 | 0 | typeVariableMap.put(variable, actualTypeArgument); |
352 | |
} |
353 | 0 | else if (actualTypeArgument instanceof ParameterizedType) |
354 | |
{ |
355 | 0 | typeVariableMap.put(variable, actualTypeArgument); |
356 | |
} |
357 | 0 | else if (actualTypeArgument instanceof TypeVariable) |
358 | |
{ |
359 | |
|
360 | |
|
361 | 0 | TypeVariable typeVariableArgument = (TypeVariable) actualTypeArgument; |
362 | 0 | Type resolvedType = typeVariableMap.get(typeVariableArgument); |
363 | 0 | if (resolvedType == null) { |
364 | 0 | resolvedType = extractBoundForTypeVariable(typeVariableArgument); |
365 | |
} |
366 | 0 | typeVariableMap.put(variable, resolvedType); |
367 | |
} |
368 | |
} |
369 | |
} |
370 | 0 | } |
371 | |
|
372 | |
} |