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.util.ArrayList;
21 import java.util.Hashtable;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26
27
28
29
30
31
32
33
34
35 public class MethodMap
36 {
37 private static final int MORE_SPECIFIC = 0;
38
39 private static final int LESS_SPECIFIC = 1;
40
41 private static final int INCOMPARABLE = 2;
42
43
44
45
46 Map<String, List<Method>> methodByNameMap = new Hashtable<String, List<Method>>();
47
48
49
50
51
52
53
54 public void add( Method method )
55 {
56 String methodName = method.getName();
57
58 List<Method> l = get( methodName );
59
60 if ( l == null )
61 {
62 l = new ArrayList<Method>();
63 methodByNameMap.put( methodName, l );
64 }
65
66 l.add( method );
67 }
68
69
70
71
72
73
74
75 public List<Method> get( String key )
76 {
77 return methodByNameMap.get( key );
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public Method find( String methodName, Object[] args )
98 throws AmbiguousException
99 {
100 List<Method> methodList = get( methodName );
101
102 if ( methodList == null )
103 {
104 return null;
105 }
106
107 int l = args.length;
108 Class[] classes = new Class[l];
109
110 for ( int i = 0; i < l; ++i )
111 {
112 Object arg = args[i];
113
114
115
116
117
118 classes[i] = arg == null ? null : arg.getClass();
119 }
120
121 return getMostSpecific( methodList, classes );
122 }
123
124
125
126
127 public static class AmbiguousException
128 extends Exception
129 {
130 }
131
132 private static Method getMostSpecific( List<Method> methods, Class[] classes )
133 throws AmbiguousException
134 {
135 LinkedList<Method> applicables = getApplicables( methods, classes );
136
137 if ( applicables.isEmpty() )
138 {
139 return null;
140 }
141
142 if ( applicables.size() == 1 )
143 {
144 return applicables.getFirst();
145 }
146
147
148
149
150
151
152 LinkedList<Method> maximals = new LinkedList<Method>();
153
154 for ( Method app : applicables )
155 {
156 Class[] appArgs = app.getParameterTypes();
157 boolean lessSpecific = false;
158
159 for ( Iterator<Method> maximal = maximals.iterator(); !lessSpecific && maximal.hasNext(); )
160 {
161 Method max = maximal.next();
162
163 switch ( moreSpecific( appArgs, max.getParameterTypes() ) )
164 {
165 case MORE_SPECIFIC:
166 {
167
168
169
170
171
172 maximal.remove();
173 break;
174 }
175
176 case LESS_SPECIFIC:
177 {
178
179
180
181
182
183 lessSpecific = true;
184 break;
185 }
186 }
187 }
188
189 if ( !lessSpecific )
190 {
191 maximals.addLast( app );
192 }
193 }
194
195 if ( maximals.size() > 1 )
196 {
197
198 throw new AmbiguousException();
199 }
200
201 return maximals.getFirst();
202 }
203
204
205
206
207
208
209
210
211
212
213 private static int moreSpecific( Class[] c1, Class[] c2 )
214 {
215 boolean c1MoreSpecific = false;
216 boolean c2MoreSpecific = false;
217
218 for ( int i = 0; i < c1.length; ++i )
219 {
220 if ( c1[i] != c2[i] )
221 {
222 c1MoreSpecific = c1MoreSpecific || isStrictMethodInvocationConvertible( c2[i], c1[i] );
223 c2MoreSpecific = c2MoreSpecific || isStrictMethodInvocationConvertible( c1[i], c2[i] );
224 }
225 }
226
227 if ( c1MoreSpecific )
228 {
229 if ( c2MoreSpecific )
230 {
231
232
233
234
235 return INCOMPARABLE;
236 }
237
238 return MORE_SPECIFIC;
239 }
240
241 if ( c2MoreSpecific )
242 {
243 return LESS_SPECIFIC;
244 }
245
246
247
248
249
250 return INCOMPARABLE;
251 }
252
253
254
255
256
257
258
259
260
261 private static LinkedList<Method> getApplicables( List<Method> methods, Class[] classes )
262 {
263 LinkedList<Method> list = new LinkedList<Method>();
264
265 for ( Object method1 : methods )
266 {
267 Method method = (Method) method1;
268
269 if ( isApplicable( method, classes ) )
270 {
271 list.add( method );
272 }
273
274 }
275 return list;
276 }
277
278
279
280
281
282
283
284
285 private static boolean isApplicable( Method method, Class[] classes )
286 {
287 Class[] methodArgs = method.getParameterTypes();
288
289 if ( methodArgs.length != classes.length )
290 {
291 return false;
292 }
293
294 for ( int i = 0; i < classes.length; ++i )
295 {
296 if ( !isMethodInvocationConvertible( methodArgs[i], classes[i] ) )
297 {
298 return false;
299 }
300 }
301
302 return true;
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 private static boolean isMethodInvocationConvertible( Class formal, Class actual )
319 {
320
321
322
323 if ( actual == null && !formal.isPrimitive() )
324 {
325 return true;
326 }
327
328
329
330
331
332 if ( actual != null && formal.isAssignableFrom( actual ) )
333 {
334 return true;
335 }
336
337
338
339
340
341 if ( formal.isPrimitive() )
342 {
343 if ( formal == Boolean.TYPE && actual == Boolean.class )
344 return true;
345 if ( formal == Character.TYPE && actual == Character.class )
346 return true;
347 if ( formal == Byte.TYPE && actual == Byte.class )
348 return true;
349 if ( formal == Short.TYPE && ( actual == Short.class || actual == Byte.class ) )
350 return true;
351 if ( formal == Integer.TYPE
352 && ( actual == Integer.class || actual == Short.class || actual == Byte.class ) )
353 return true;
354 if ( formal == Long.TYPE && ( actual == Long.class || actual == Integer.class || actual == Short.class
355 || actual == Byte.class ) )
356 return true;
357 if ( formal == Float.TYPE && ( actual == Float.class || actual == Long.class || actual == Integer.class
358 || actual == Short.class || actual == Byte.class ) )
359 return true;
360 if ( formal == Double.TYPE && ( actual == Double.class || actual == Float.class || actual == Long.class
361 || actual == Integer.class || actual == Short.class || actual == Byte.class ) )
362 return true;
363 }
364
365 return false;
366 }
367
368
369
370
371
372
373
374
375
376
377
378 private static boolean isStrictMethodInvocationConvertible( Class formal, Class actual )
379 {
380
381
382
383 if ( actual == null && !formal.isPrimitive() )
384 {
385 return true;
386 }
387
388
389
390
391
392 if ( formal.isAssignableFrom( actual ) )
393 {
394 return true;
395 }
396
397
398
399
400
401 if ( formal.isPrimitive() )
402 {
403 if ( formal == Short.TYPE && ( actual == Byte.TYPE ) )
404 return true;
405 if ( formal == Integer.TYPE && ( actual == Short.TYPE || actual == Byte.TYPE ) )
406 return true;
407 if ( formal == Long.TYPE && ( actual == Integer.TYPE || actual == Short.TYPE || actual == Byte.TYPE ) )
408 return true;
409 if ( formal == Float.TYPE
410 && ( actual == Long.TYPE || actual == Integer.TYPE || actual == Short.TYPE || actual == Byte.TYPE ) )
411 return true;
412 if ( formal == Double.TYPE && ( actual == Float.TYPE || actual == Long.TYPE || actual == Integer.TYPE
413 || actual == Short.TYPE || actual == Byte.TYPE ) )
414 return true;
415 }
416 return false;
417 }
418 }