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