1 package org.codehaus.plexus.interpolation.reflection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22 import java.util.Hashtable;
23 import java.util.Map;
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class ClassMap {
38 private static final class CacheMiss {}
39
40 private static final CacheMiss CACHE_MISS = new CacheMiss();
41 private static final Object OBJECT = new Object();
42
43
44
45
46
47 private Class<?> clazz;
48
49
50
51
52
53 private Map<String, Object> methodCache = new Hashtable<String, Object>();
54
55 private MethodMap methodMap = new MethodMap();
56
57
58
59
60
61 public ClassMap(Class<?> clazz) {
62 this.clazz = clazz;
63 populateMethodCache();
64 }
65
66
67
68
69 Class<?> getCachedClass() {
70 return clazz;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public Method findMethod(String name, Object[] params) throws MethodMap.AmbiguousException {
88 String methodKey = makeMethodKey(name, params);
89 Object cacheEntry = methodCache.get(methodKey);
90
91 if (cacheEntry == CACHE_MISS) {
92 return null;
93 }
94
95 if (cacheEntry == null) {
96 try {
97 cacheEntry = methodMap.find(name, params);
98 } catch (MethodMap.AmbiguousException ae) {
99
100
101
102
103 methodCache.put(methodKey, CACHE_MISS);
104
105 throw ae;
106 }
107
108 if (cacheEntry == null) {
109 methodCache.put(methodKey, CACHE_MISS);
110 } else {
111 methodCache.put(methodKey, cacheEntry);
112 }
113 }
114
115
116
117 return (Method) cacheEntry;
118 }
119
120
121
122
123
124
125 private void populateMethodCache() {
126
127
128
129
130 Method[] methods = getAccessibleMethods(clazz);
131
132
133
134
135
136 for (Method method : methods) {
137
138
139
140
141
142
143 Method publicMethod = getPublicMethod(method);
144
145
146
147
148
149
150
151
152 if (publicMethod != null) {
153 methodMap.add(publicMethod);
154 methodCache.put(makeMethodKey(publicMethod), publicMethod);
155 }
156 }
157 }
158
159
160
161
162
163
164 private String makeMethodKey(Method method) {
165 Class<?>[] parameterTypes = method.getParameterTypes();
166
167 StringBuilder methodKey = new StringBuilder(method.getName());
168
169 for (Class<?> parameterType : parameterTypes) {
170
171
172
173
174
175
176 if (parameterType.isPrimitive()) {
177 if (parameterType.equals(Boolean.TYPE)) {
178 methodKey.append("java.lang.Boolean");
179 } else if (parameterType.equals(Byte.TYPE)) {
180 methodKey.append("java.lang.Byte");
181 } else if (parameterType.equals(Character.TYPE)) {
182 methodKey.append("java.lang.Character");
183 } else if (parameterType.equals(Double.TYPE)) {
184 methodKey.append("java.lang.Double");
185 } else if (parameterType.equals(Float.TYPE)) {
186 methodKey.append("java.lang.Float");
187 } else if (parameterType.equals(Integer.TYPE)) {
188 methodKey.append("java.lang.Integer");
189 } else if (parameterType.equals(Long.TYPE)) {
190 methodKey.append("java.lang.Long");
191 } else if (parameterType.equals(Short.TYPE)) {
192 methodKey.append("java.lang.Short");
193 }
194 } else {
195 methodKey.append(parameterType.getName());
196 }
197 }
198
199 return methodKey.toString();
200 }
201
202 private static String makeMethodKey(String method, Object[] params) {
203 if (params.length == 0) {
204 return method;
205 }
206
207 StringBuilder methodKey = new StringBuilder().append(method);
208
209 for (Object arg : params) {
210 if (arg == null) {
211 arg = OBJECT;
212 }
213
214 methodKey.append(arg.getClass().getName());
215 }
216
217 return methodKey.toString();
218 }
219
220
221
222
223
224
225
226 private static Method[] getAccessibleMethods(Class<?> clazz) {
227 Method[] methods = clazz.getMethods();
228
229
230
231
232
233
234 if (Modifier.isPublic(clazz.getModifiers())) {
235 return methods;
236 }
237
238
239
240
241
242 MethodInfo[] methodInfos = new MethodInfo[methods.length];
243
244 for (int i = methods.length; i-- > 0; ) {
245 methodInfos[i] = new MethodInfo(methods[i]);
246 }
247
248 int upcastCount = getAccessibleMethods(clazz, methodInfos, 0);
249
250
251
252
253
254 if (upcastCount < methods.length) {
255 methods = new Method[upcastCount];
256 }
257
258 int j = 0;
259 for (MethodInfo methodInfo : methodInfos) {
260 if (methodInfo.upcast) {
261 methods[j++] = methodInfo.method;
262 }
263 }
264 return methods;
265 }
266
267
268
269
270
271
272
273
274
275
276 private static int getAccessibleMethods(Class<?> clazz, MethodInfo[] methodInfos, int upcastCount) {
277 int l = methodInfos.length;
278
279
280
281
282
283
284 if (Modifier.isPublic(clazz.getModifiers())) {
285 for (int i = 0; i < l && upcastCount < l; ++i) {
286 try {
287 MethodInfo methodInfo = methodInfos[i];
288
289 if (!methodInfo.upcast) {
290 methodInfo.tryUpcasting(clazz);
291 upcastCount++;
292 }
293 } catch (NoSuchMethodException e) {
294
295
296
297
298 }
299 }
300
301
302
303
304
305 if (upcastCount == l) {
306 return upcastCount;
307 }
308 }
309
310
311
312
313
314 Class<?> superclazz = clazz.getSuperclass();
315
316 if (superclazz != null) {
317 upcastCount = getAccessibleMethods(superclazz, methodInfos, upcastCount);
318
319
320
321
322
323 if (upcastCount == l) {
324 return upcastCount;
325 }
326 }
327
328
329
330
331
332
333
334 Class<?>[] interfaces = clazz.getInterfaces();
335
336 for (int i = interfaces.length; i-- > 0; ) {
337 upcastCount = getAccessibleMethods(interfaces[i], methodInfos, upcastCount);
338
339
340
341
342
343 if (upcastCount == l) {
344 return upcastCount;
345 }
346 }
347
348 return upcastCount;
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362 public static Method getPublicMethod(Method method) {
363 Class<?> clazz = method.getDeclaringClass();
364
365
366
367
368
369
370 if ((clazz.getModifiers() & Modifier.PUBLIC) != 0) {
371 return method;
372 }
373
374 return getPublicMethod(clazz, method.getName(), method.getParameterTypes());
375 }
376
377
378
379
380
381
382
383
384
385 private static Method getPublicMethod(Class<?> clazz, String name, Class<?>[] paramTypes) {
386
387
388
389
390 if ((clazz.getModifiers() & Modifier.PUBLIC) != 0) {
391 try {
392 return clazz.getMethod(name, paramTypes);
393 } catch (NoSuchMethodException e) {
394
395
396
397
398
399 return null;
400 }
401 }
402
403
404
405
406
407 Class<?> superclazz = clazz.getSuperclass();
408
409 if (superclazz != null) {
410 Method superclazzMethod = getPublicMethod(superclazz, name, paramTypes);
411
412 if (superclazzMethod != null) {
413 return superclazzMethod;
414 }
415 }
416
417
418
419
420
421 for (Class<?> interface_ : clazz.getInterfaces()) {
422 Method interfaceMethod = getPublicMethod(interface_, name, paramTypes);
423
424 if (interfaceMethod != null) {
425 return interfaceMethod;
426 }
427 }
428
429 return null;
430 }
431
432
433
434
435 private static final class MethodInfo {
436 Method method;
437 String name;
438 Class<?>[] parameterTypes;
439 boolean upcast;
440
441 MethodInfo(Method method) {
442 this.method = null;
443 name = method.getName();
444 parameterTypes = method.getParameterTypes();
445 upcast = false;
446 }
447
448 void tryUpcasting(Class<?> clazz) throws NoSuchMethodException {
449 method = clazz.getMethod(name, parameterTypes);
450 name = null;
451 parameterTypes = null;
452 upcast = true;
453 }
454 }
455 }