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