1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.easymock.internal; |
18 |
|
|
19 |
|
import java.lang.reflect.*; |
20 |
|
import java.util.*; |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
@link |
27 |
|
@link |
28 |
|
|
29 |
|
|
30 |
|
@link |
31 |
|
@link |
32 |
|
|
33 |
|
@link |
34 |
|
|
35 |
|
@link@link |
36 |
|
@link |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@author |
44 |
|
@author |
45 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
46 |
|
public final class BridgeMethodResolver { |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
53 |
|
private BridgeMethodResolver() {... |
54 |
|
} |
55 |
|
|
56 |
|
|
57 |
|
@link |
58 |
|
|
59 |
|
@link |
60 |
|
@link |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@param |
65 |
|
|
66 |
|
@return |
67 |
|
@throws |
68 |
|
@link |
69 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
70 |
|
public static Method findBridgedMethod(final Method bridgeMethod) {... |
71 |
|
assert bridgeMethod != null : "Method must not be null"; |
72 |
|
|
73 |
|
if (!bridgeMethod.isBridge()) { |
74 |
|
return bridgeMethod; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
final List<Method> candidateMethods = new ArrayList<Method>(); |
79 |
|
final Method[] methods = getAllDeclaredMethods(bridgeMethod.getDeclaringClass()); |
80 |
|
for (final Method candidateMethod : methods) { |
81 |
|
if (isBridgedCandidateFor(candidateMethod, bridgeMethod)) { |
82 |
|
candidateMethods.add(candidateMethod); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
|
Method result; |
87 |
|
|
88 |
|
if (candidateMethods.size() == 1) { |
89 |
|
result = candidateMethods.get(0); |
90 |
|
} else { |
91 |
|
result = searchCandidates(candidateMethods, bridgeMethod); |
92 |
|
} |
93 |
|
|
94 |
|
if (result == null) { |
95 |
|
throw new IllegalStateException("Unable to locate bridged method for bridge method '" |
96 |
|
+ bridgeMethod + "'"); |
97 |
|
} |
98 |
|
|
99 |
|
return result; |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
@param |
106 |
|
|
107 |
|
@param |
108 |
|
|
109 |
|
@return |
110 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
111 |
|
private static Method searchCandidates(final List<Method> candidateMethods, final Method bridgeMethod) {... |
112 |
|
final Map<TypeVariable<?>, Type> typeParameterMap = createTypeVariableMap(bridgeMethod |
113 |
|
.getDeclaringClass()); |
114 |
|
for (int i = 0; i < candidateMethods.size(); i++) { |
115 |
|
final Method candidateMethod = candidateMethods.get(i); |
116 |
|
if (isBridgeMethodFor(bridgeMethod, candidateMethod, typeParameterMap)) { |
117 |
|
return candidateMethod; |
118 |
|
} |
119 |
|
} |
120 |
|
return null; |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@link |
126 |
|
@link@link |
127 |
|
|
128 |
|
|
129 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
130 |
|
private static boolean isBridgedCandidateFor(final Method candidateMethod, final Method bridgeMethod) {... |
131 |
|
return (!candidateMethod.isBridge() && !candidateMethod.equals(bridgeMethod) |
132 |
|
&& candidateMethod.getName().equals(bridgeMethod.getName()) && candidateMethod |
133 |
|
.getParameterTypes().length == bridgeMethod.getParameterTypes().length); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
@link |
138 |
|
@link |
139 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
140 |
|
private static boolean isBridgeMethodFor(final Method bridgeMethod, final Method candidateMethod,... |
141 |
|
final Map<TypeVariable<?>, Type> typeVariableMap) { |
142 |
|
if (isResolvedTypeMatch(candidateMethod, bridgeMethod, typeVariableMap)) { |
143 |
|
return true; |
144 |
|
} |
145 |
|
final Method method = findGenericDeclaration(bridgeMethod); |
146 |
|
return (method != null ? isResolvedTypeMatch(method, candidateMethod, typeVariableMap) : false); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
@link |
151 |
|
|
152 |
|
|
153 |
|
@throws |
154 |
|
|
155 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
156 |
|
private static Method findGenericDeclaration(final Method bridgeMethod) {... |
157 |
|
|
158 |
|
Class<?> superclass = bridgeMethod.getDeclaringClass().getSuperclass(); |
159 |
|
while (!Object.class.equals(superclass)) { |
160 |
|
final Method method = searchForMatch(superclass, bridgeMethod); |
161 |
|
if (method != null && !method.isBridge()) { |
162 |
|
return method; |
163 |
|
} |
164 |
|
superclass = superclass.getSuperclass(); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
final Class<?>[] interfaces = getAllInterfacesForClass(bridgeMethod.getDeclaringClass()); |
169 |
|
for (final Class<?> anInterface : interfaces) { |
170 |
|
final Method method = searchForMatch(anInterface, bridgeMethod); |
171 |
|
if (method != null && !method.isBridge()) { |
172 |
|
return method; |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
return null; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
@link |
181 |
|
@link |
182 |
|
@link |
183 |
|
@link |
184 |
|
@link |
185 |
|
|
186 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
187 |
|
private static boolean isResolvedTypeMatch(final Method genericMethod, final Method candidateMethod,... |
188 |
|
final Map<TypeVariable<?>, Type> typeVariableMap) { |
189 |
|
final Type[] genericParameters = genericMethod.getGenericParameterTypes(); |
190 |
|
final Class<?>[] candidateParameters = candidateMethod.getParameterTypes(); |
191 |
|
if (genericParameters.length != candidateParameters.length) { |
192 |
|
return false; |
193 |
|
} |
194 |
|
for (int i = 0; i < genericParameters.length; i++) { |
195 |
|
final Type genericParameter = genericParameters[i]; |
196 |
|
final Class<?> candidateParameter = candidateParameters[i]; |
197 |
|
if (candidateParameter.isArray()) { |
198 |
|
|
199 |
|
final Type rawType = getRawType(genericParameter, typeVariableMap); |
200 |
|
if (rawType instanceof GenericArrayType) { |
201 |
|
if (!candidateParameter.getComponentType().equals( |
202 |
|
getRawType(((GenericArrayType) rawType).getGenericComponentType(), |
203 |
|
typeVariableMap))) { |
204 |
|
return false; |
205 |
|
} |
206 |
|
break; |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
if (!candidateParameter.equals(getRawType(genericParameter, typeVariableMap))) { |
211 |
|
return false; |
212 |
|
} |
213 |
|
} |
214 |
|
return true; |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
220 |
|
private static Type getRawType(final Type genericType, final Map<TypeVariable<?>, Type> typeVariableMap) {... |
221 |
|
if (genericType instanceof TypeVariable<?>) { |
222 |
|
final TypeVariable<?> tv = (TypeVariable<?>) genericType; |
223 |
|
final Type result = typeVariableMap.get(tv); |
224 |
|
return (result != null ? result : Object.class); |
225 |
|
} else if (genericType instanceof ParameterizedType) { |
226 |
|
return ((ParameterizedType) genericType).getRawType(); |
227 |
|
} else { |
228 |
|
return genericType; |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
@link@link |
234 |
|
@link |
235 |
|
@link |
236 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
237 |
|
private static Method searchForMatch(final Class<?> type, final Method bridgeMethod) {... |
238 |
|
return findMethod(type, bridgeMethod.getName(), bridgeMethod.getParameterTypes()); |
239 |
|
} |
240 |
|
|
241 |
|
|
242 |
|
@link |
243 |
|
@link@link |
244 |
|
|
245 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
246 |
|
private static Map<TypeVariable<?>, Type> createTypeVariableMap(final Class<?> cls) {... |
247 |
|
final Map<TypeVariable<?>, Type> typeVariableMap = new HashMap<TypeVariable<?>, Type>(); |
248 |
|
|
249 |
|
|
250 |
|
extractTypeVariablesFromGenericInterfaces(cls.getGenericInterfaces(), typeVariableMap); |
251 |
|
|
252 |
|
|
253 |
|
Type genericType = cls.getGenericSuperclass(); |
254 |
|
Class<?> type = cls.getSuperclass(); |
255 |
|
while (!Object.class.equals(type)) { |
256 |
|
if (genericType instanceof ParameterizedType) { |
257 |
|
final ParameterizedType pt = (ParameterizedType) genericType; |
258 |
|
populateTypeMapFromParameterizedType(pt, typeVariableMap); |
259 |
|
} |
260 |
|
extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), typeVariableMap); |
261 |
|
genericType = type.getGenericSuperclass(); |
262 |
|
type = type.getSuperclass(); |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
type = cls; |
267 |
|
while (type.isMemberClass()) { |
268 |
|
genericType = type.getGenericSuperclass(); |
269 |
|
if (genericType instanceof ParameterizedType) { |
270 |
|
final ParameterizedType pt = (ParameterizedType) genericType; |
271 |
|
populateTypeMapFromParameterizedType(pt, typeVariableMap); |
272 |
|
} |
273 |
|
type = type.getEnclosingClass(); |
274 |
|
} |
275 |
|
|
276 |
|
return typeVariableMap; |
277 |
|
} |
278 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
279 |
|
private static void extractTypeVariablesFromGenericInterfaces(final Type[] genericInterfaces,... |
280 |
|
final Map<TypeVariable<?>, Type> typeVariableMap) { |
281 |
|
for (final Type genericInterface : genericInterfaces) { |
282 |
|
if (genericInterface instanceof ParameterizedType) { |
283 |
|
final ParameterizedType pt = (ParameterizedType) genericInterface; |
284 |
|
populateTypeMapFromParameterizedType(pt, typeVariableMap); |
285 |
|
if (pt.getRawType() instanceof Class<?>) { |
286 |
|
extractTypeVariablesFromGenericInterfaces(((Class<?>) pt.getRawType()) |
287 |
|
.getGenericInterfaces(), typeVariableMap); |
288 |
|
} |
289 |
|
} else if (genericInterface instanceof Class<?>) { |
290 |
|
extractTypeVariablesFromGenericInterfaces(((Class<?>) genericInterface) |
291 |
|
.getGenericInterfaces(), typeVariableMap); |
292 |
|
} |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
@link |
298 |
|
@link |
299 |
|
@link |
300 |
|
@link |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
@link |
316 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
317 |
|
private static void populateTypeMapFromParameterizedType(final ParameterizedType type,... |
318 |
|
final Map<TypeVariable<?>, Type> typeVariableMap) { |
319 |
|
if (type.getRawType() instanceof Class<?>) { |
320 |
|
final Type[] actualTypeArguments = type.getActualTypeArguments(); |
321 |
|
final TypeVariable<?>[] typeVariables = ((Class<?>) type.getRawType()).getTypeParameters(); |
322 |
|
for (int i = 0; i < actualTypeArguments.length; i++) { |
323 |
|
final Type actualTypeArgument = actualTypeArguments[i]; |
324 |
|
final TypeVariable<?> variable = typeVariables[i]; |
325 |
|
if (actualTypeArgument instanceof Class<?>) { |
326 |
|
typeVariableMap.put(variable, actualTypeArgument); |
327 |
|
} else if (actualTypeArgument instanceof GenericArrayType) { |
328 |
|
typeVariableMap.put(variable, actualTypeArgument); |
329 |
|
} else if (actualTypeArgument instanceof ParameterizedType) { |
330 |
|
typeVariableMap.put(variable, ((ParameterizedType) actualTypeArgument).getRawType()); |
331 |
|
} else if (actualTypeArgument instanceof TypeVariable<?>) { |
332 |
|
|
333 |
|
|
334 |
|
final TypeVariable<?> typeVariableArgument = (TypeVariable<?>) actualTypeArgument; |
335 |
|
Type resolvedType = typeVariableMap.get(typeVariableArgument); |
336 |
|
if (resolvedType == null) { |
337 |
|
resolvedType = extractClassForTypeVariable(typeVariableArgument); |
338 |
|
} |
339 |
|
if (resolvedType != null) { |
340 |
|
typeVariableMap.put(variable, resolvedType); |
341 |
|
} |
342 |
|
} |
343 |
|
} |
344 |
|
} |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
|
@link |
349 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
350 |
|
private static Class<?> extractClassForTypeVariable(final TypeVariable<?> typeVariable) {... |
351 |
|
final Type[] bounds = typeVariable.getBounds(); |
352 |
|
Type result = null; |
353 |
|
if (bounds.length > 0) { |
354 |
|
final Type bound = bounds[0]; |
355 |
|
if (bound instanceof ParameterizedType) { |
356 |
|
result = ((ParameterizedType) bound).getRawType(); |
357 |
|
} else if (bound instanceof Class<?>) { |
358 |
|
result = bound; |
359 |
|
} else if (bound instanceof TypeVariable<?>) { |
360 |
|
result = extractClassForTypeVariable((TypeVariable<?>) bound); |
361 |
|
} |
362 |
|
} |
363 |
|
return (result instanceof Class<?> ? (Class<?>) result : null); |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@param |
373 |
|
|
374 |
|
@return |
375 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
376 |
|
private static Class<?>[] getAllInterfacesForClass(Class<?> clazz) {... |
377 |
|
assert clazz != null : "Class must not be null"; |
378 |
|
if (clazz.isInterface()) { |
379 |
|
return new Class[] { clazz }; |
380 |
|
} |
381 |
|
final List<Class<?>> interfaces = new ArrayList<Class<?>>(); |
382 |
|
while (clazz != null) { |
383 |
|
for (int i = 0; i < clazz.getInterfaces().length; i++) { |
384 |
|
final Class<?> ifc = clazz.getInterfaces()[i]; |
385 |
|
if (!interfaces.contains(ifc)) { |
386 |
|
interfaces.add(ifc); |
387 |
|
} |
388 |
|
} |
389 |
|
clazz = clazz.getSuperclass(); |
390 |
|
} |
391 |
|
return interfaces.toArray(new Class[interfaces.size()]); |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
@link |
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
@link |
400 |
|
|
401 |
|
@param |
402 |
|
|
403 |
|
@param |
404 |
|
|
405 |
|
@param |
406 |
|
|
407 |
|
@return |
408 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
409 |
|
private static Method findMethod(final Class<?> clazz, final String name, final Class<?>[] paramTypes) {... |
410 |
|
assert clazz != null : "Class must not be null"; |
411 |
|
assert name != null : "Method name must not be null"; |
412 |
|
Class<?> searchType = clazz; |
413 |
|
while (!Object.class.equals(searchType) && searchType != null) { |
414 |
|
final Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType |
415 |
|
.getDeclaredMethods()); |
416 |
|
for (final Method method : methods) { |
417 |
|
if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) { |
418 |
|
return method; |
419 |
|
} |
420 |
|
} |
421 |
|
searchType = searchType.getSuperclass(); |
422 |
|
} |
423 |
|
return null; |
424 |
|
} |
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
430 |
|
private static Method[] getAllDeclaredMethods(Class<?> leafClass) {... |
431 |
|
final List<Method> list = new LinkedList<Method>(); |
432 |
|
|
433 |
|
|
434 |
|
do { |
435 |
|
final Method[] methods = leafClass.getDeclaredMethods(); |
436 |
|
for (final Method method : methods) { |
437 |
|
list.add(method); |
438 |
|
} |
439 |
|
leafClass = leafClass.getSuperclass(); |
440 |
|
} while (leafClass != null); |
441 |
|
|
442 |
|
return list.toArray(new Method[list.size()]); |
443 |
|
} |
444 |
|
|
445 |
|
} |