1 package org.codehaus.plexus.interpolation.reflection;
2
3 /* ====================================================================
4 * Copyright 2001-2004 The Apache Software Foundation.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
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 * <b>NOTE:</b> This class was copied from plexus-utils, to allow this library
30 * to stand completely self-contained.
31 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
32 * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
33 * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
34 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
35 * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
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 * Keep track of all methods with the same name.
44 */
45 Map<String, List<Method>> methodByNameMap = new Hashtable<String, List<Method>>();
46
47 /**
48 * Add a method to a list of methods by name.
49 * For a particular class we are keeping track
50 * of all the methods with the same name.
51 * @param method {@link Method}
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 * Return a list of methods with the same name.
68 *
69 * @param key the key
70 * @return list of methods
71 */
72 public List<Method> get(String key) {
73 return methodByNameMap.get(key);
74 }
75
76 /**
77 * <p>
78 * Find a method. Attempts to find the
79 * most specific applicable method using the
80 * algorithm described in the JLS section
81 * 15.12.2 (with the exception that it can't
82 * distinguish a primitive type argument from
83 * an object type argument, since in reflection
84 * primitive type arguments are represented by
85 * their object counterparts, so for an argument of
86 * type (say) java.lang.Integer, it will not be able
87 * to decide between a method that takes int and a
88 * method that takes java.lang.Integer as a parameter.
89 * </p>
90 *
91 * <p>
92 * This turns out to be a relatively rare case
93 * where this is needed - however, functionality
94 * like this is needed.
95 * </p>
96 *
97 * @param methodName name of method
98 * @param args the actual arguments with which the method is called
99 * @return the most specific applicable method, or null if no
100 * method is applicable.
101 * @throws AmbiguousException if there is more than one maximally
102 * specific applicable method
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 * if we are careful down below, a null argument goes in there
119 * so we can know that the null was passed to the method
120 */
121 classes[i] = arg == null ? null : arg.getClass();
122 }
123
124 return getMostSpecific(methodList, classes);
125 }
126
127 /**
128 * simple distinguishable exception, used when
129 * we run across ambiguous overloading
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 * This list will contain the maximally specific methods. Hopefully at
146 * the end of the below loop, the list will contain exactly one method,
147 * (the most specific method) otherwise we have ambiguity.
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 * This method is more specific than the previously
163 * known maximally specific, so remove the old maximum.
164 */
165
166 maximal.remove();
167 break;
168 }
169
170 case LESS_SPECIFIC: {
171 /*
172 * This method is less specific than some of the
173 * currently known maximally specific methods, so we
174 * won't add it into the set of maximally specific
175 * methods
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 // We have more than one maximally specific method
191 throw new AmbiguousException();
192 }
193
194 return maximals.getFirst();
195 }
196
197 /**
198 * Determines which method signature (represented by a class array) is more
199 * specific. This defines a partial ordering on the method signatures.
200 * @param c1 first signature to compare
201 * @param c2 second signature to compare
202 * @return MORE_SPECIFIC if c1 is more specific than c2, LESS_SPECIFIC if
203 * c1 is less specific than c2, INCOMPARABLE if they are incomparable.
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 * Incomparable due to cross-assignable arguments (i.e.
220 * foo(String, Object) vs. foo(Object, String))
221 */
222
223 return INCOMPARABLE;
224 }
225
226 return MORE_SPECIFIC;
227 }
228
229 if (c2MoreSpecific) {
230 return LESS_SPECIFIC;
231 }
232
233 /*
234 * Incomparable due to non-related arguments (i.e.
235 * foo(Runnable) vs. foo(Serializable))
236 */
237
238 return INCOMPARABLE;
239 }
240
241 /**
242 * Returns all methods that are applicable to actual argument types.
243 * @param methods list of all candidate methods
244 * @param classes the actual types of the arguments
245 * @return a list that contains only applicable methods (number of
246 * formal and actual arguments matches, and argument types are assignable
247 * to formal types through a method invocation conversion).
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 * Returns true if the supplied method is applicable to actual
262 * argument types.
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 * Determines whether a type represented by a class object is
282 * convertible to another type represented by a class object using a
283 * method invocation conversion, treating object types of primitive
284 * types as if they were primitive types (that is, a Boolean actual
285 * parameter type matches boolean primitive formal type). This behavior
286 * is because this method is used to determine applicable methods for
287 * an actual parameter list, and primitive types are represented by
288 * their object duals in reflective method calls.
289 *
290 * @param formal the formal parameter type to which the actual
291 * parameter type should be convertible
292 * @param actual the actual parameter type.
293 * @return true if either formal type is assignable from actual type,
294 * or formal is a primitive type and actual is its corresponding object
295 * type or an object type of a primitive type that can be converted to
296 * the formal type.
297 */
298 private static boolean isMethodInvocationConvertible(Class<?> formal, Class<?> actual) {
299 /*
300 * if it's a null, it means the arg was null
301 */
302 if (actual == null && !formal.isPrimitive()) {
303 return true;
304 }
305
306 /*
307 * Check for identity or widening reference conversion
308 */
309
310 if (actual != null && formal.isAssignableFrom(actual)) {
311 return true;
312 }
313
314 /*
315 * Check for boxing with widening primitive conversion. Note that
316 * actual parameters are never primitives.
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 * Determines whether a type represented by a class object is
360 * convertible to another type represented by a class object using a
361 * method invocation conversion, without matching object and primitive
362 * types. This method is used to determine the more specific type when
363 * comparing signatures of methods.
364 *
365 * @param formal the formal parameter type to which the actual
366 * parameter type should be convertible
367 * @param actual the actual parameter type.
368 * @return true if either formal type is assignable from actual type,
369 * or formal and actual are both primitive types and actual can be
370 * subject to widening conversion to formal.
371 */
372 private static boolean isStrictMethodInvocationConvertible(Class<?> formal, Class<?> actual) {
373 /*
374 * we shouldn't get a null into, but if so
375 */
376 if (actual == null && !formal.isPrimitive()) {
377 return true;
378 }
379
380 /*
381 * Check for identity or widening reference conversion
382 */
383
384 if (formal.isAssignableFrom(actual)) {
385 return true;
386 }
387
388 /*
389 * Check for widening primitive conversion.
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 }