001: /*
002: * Copyright 2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl.util.introspection;
018:
019: import java.lang.reflect.Method;
020: import java.util.ArrayList;
021: import java.util.Hashtable;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: /**
028: *
029: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
030: * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
031: * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
032: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
033: * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
034: * @since 1.0
035: * @version $Id: MethodMap.java 398495 2006-05-01 01:33:43Z dion $
036: */
037: public class MethodMap {
038: /** whether a method is more specific than a previously compared one. */
039: private static final int MORE_SPECIFIC = 0;
040: /** whether a method is less specific than a previously compared one. */
041: private static final int LESS_SPECIFIC = 1;
042: /** A method doesn't match a previously compared one. */
043: private static final int INCOMPARABLE = 2;
044:
045: /**
046: * Keep track of all methods with the same name.
047: */
048: protected Map methodByNameMap = new Hashtable();
049:
050: /**
051: * Add a method to a list of methods by name. For a particular class we are
052: * keeping track of all the methods with the same name.
053: * @param method the method.
054: */
055: public void add(Method method) {
056: String methodName = method.getName();
057:
058: List l = get(methodName);
059:
060: if (l == null) {
061: l = new ArrayList();
062: methodByNameMap.put(methodName, l);
063: }
064:
065: l.add(method);
066: }
067:
068: /**
069: * Return a list of methods with the same name.
070: *
071: * @param key The method name.
072: * @return List list of methods
073: */
074: public List get(String key) {
075: return (List) methodByNameMap.get(key);
076: }
077:
078: /**
079: * <p>
080: * Find a method. Attempts to find the most specific applicable method using
081: * the algorithm described in the JLS section 15.12.2 (with the exception
082: * that it can't distinguish a primitive type argument from an object type
083: * argument, since in reflection primitive type arguments are represented by
084: * their object counterparts, so for an argument of type (say)
085: * java.lang.Integer, it will not be able to decide between a method that
086: * takes int and a method that takes java.lang.Integer as a parameter.
087: * </p>
088: *
089: * <p>
090: * This turns out to be a relatively rare case where this is needed -
091: * however, functionality like this is needed.
092: * </p>
093: *
094: * @param methodName name of method
095: * @param args the actual arguments with which the method is called
096: * @return the most specific applicable method, or null if no method is
097: * applicable.
098: * @throws AmbiguousException if there is more than one maximally specific
099: * applicable method
100: */
101: public Method find(String methodName, Object[] args)
102: throws AmbiguousException {
103: List methodList = get(methodName);
104:
105: if (methodList == null) {
106: return null;
107: }
108:
109: int l = args.length;
110: Class[] classes = new Class[l];
111:
112: for (int i = 0; i < l; ++i) {
113: Object arg = args[i];
114:
115: /*
116: * if we are careful down below, a null argument goes in there so we
117: * can know that the null was passed to the method
118: */
119: classes[i] = arg == null ? null : arg.getClass();
120: }
121:
122: return getMostSpecific(methodList, classes);
123: }
124:
125: /**
126: * simple distinguishable exception, used when we run across ambiguous
127: * overloading.
128: */
129: public static class AmbiguousException extends Exception {
130: /** serialization version id jdk13 generated. */
131: static final long serialVersionUID = 8758118091728717367L;
132: }
133:
134: /**
135: * Gets the most specific method from a list.
136: * @param methods list of {@link Method methods}
137: * @param classes argument types
138: * @return the most specific method, or null
139: * @throws AmbiguousException if there is more than one specific method
140: */
141: private static Method getMostSpecific(List methods, Class[] classes)
142: throws AmbiguousException {
143: LinkedList applicables = getApplicables(methods, classes);
144:
145: if (applicables.isEmpty()) {
146: return null;
147: }
148:
149: if (applicables.size() == 1) {
150: return (Method) applicables.getFirst();
151: }
152:
153: /*
154: * This list will contain the maximally specific methods. Hopefully at
155: * the end of the below loop, the list will contain exactly one method,
156: * (the most specific method) otherwise we have ambiguity.
157: */
158:
159: LinkedList maximals = new LinkedList();
160:
161: for (Iterator applicable = applicables.iterator(); applicable
162: .hasNext();) {
163: Method app = (Method) applicable.next();
164: Class[] appArgs = app.getParameterTypes();
165: boolean lessSpecific = false;
166:
167: for (Iterator maximal = maximals.iterator(); !lessSpecific
168: && maximal.hasNext();) {
169: Method max = (Method) maximal.next();
170:
171: switch (moreSpecific(appArgs, max.getParameterTypes())) {
172: case MORE_SPECIFIC:
173: /*
174: * This method is more specific than the previously
175: * known maximally specific, so remove the old maximum.
176: */
177: maximal.remove();
178: break;
179:
180: case LESS_SPECIFIC:
181: /*
182: * This method is less specific than some of the
183: * currently known maximally specific methods, so we
184: * won't add it into the set of maximally specific
185: * methods
186: */
187: lessSpecific = true;
188: break;
189: }
190: }
191:
192: if (!lessSpecific) {
193: maximals.addLast(app);
194: }
195: }
196:
197: if (maximals.size() > 1) {
198: // We have more than one maximally specific method
199: throw new AmbiguousException();
200: }
201:
202: return (Method) maximals.getFirst();
203: }
204:
205: /**
206: * Determines which method signature (represented by a class array) is more
207: * specific. This defines a partial ordering on the method signatures.
208: *
209: * @param c1 first signature to compare
210: * @param c2 second signature to compare
211: * @return MORE_SPECIFIC if c1 is more specific than c2, LESS_SPECIFIC if c1
212: * is less specific than c2, INCOMPARABLE if they are incomparable.
213: */
214: private static int moreSpecific(Class[] c1, Class[] c2) {
215: boolean c1MoreSpecific = false;
216: boolean c2MoreSpecific = false;
217:
218: for (int i = 0; i < c1.length; ++i) {
219: if (c1[i] != c2[i]) {
220: c1MoreSpecific = c1MoreSpecific
221: || isStrictMethodInvocationConvertible(c2[i],
222: c1[i]);
223: c2MoreSpecific = c2MoreSpecific
224: || isStrictMethodInvocationConvertible(c1[i],
225: c2[i]);
226: }
227: }
228:
229: if (c1MoreSpecific) {
230: if (c2MoreSpecific) {
231: /*
232: * Incomparable due to cross-assignable arguments (i.e.
233: * foo(String, Object) vs. foo(Object, String))
234: */
235:
236: return INCOMPARABLE;
237: }
238:
239: return MORE_SPECIFIC;
240: }
241:
242: if (c2MoreSpecific) {
243: return LESS_SPECIFIC;
244: }
245:
246: /*
247: * Incomparable due to non-related arguments (i.e. foo(Runnable) vs.
248: * foo(Serializable))
249: */
250:
251: return INCOMPARABLE;
252: }
253:
254: /**
255: * Returns all methods that are applicable to actual argument types.
256: *
257: * @param methods list of all candidate methods
258: * @param classes the actual types of the arguments
259: * @return a list that contains only applicable methods (number of formal
260: * and actual arguments matches, and argument types are assignable
261: * to formal types through a method invocation conversion).
262: */
263: private static LinkedList getApplicables(List methods,
264: Class[] classes) {
265: LinkedList list = new LinkedList();
266:
267: for (Iterator imethod = methods.iterator(); imethod.hasNext();) {
268: Method method = (Method) imethod.next();
269:
270: if (isApplicable(method, classes)) {
271: list.add(method);
272: }
273:
274: }
275: return list;
276: }
277:
278: /**
279: * Returns true if the supplied method is applicable to actual argument
280: * types.
281: * @param method the method to check
282: * @param classes possible argument types
283: * @return true if the arguments are applicable to the method.
284: */
285: private static boolean isApplicable(Method method, Class[] classes) {
286: Class[] methodArgs = method.getParameterTypes();
287:
288: if (methodArgs.length != classes.length) {
289: return false;
290: }
291:
292: for (int i = 0; i < classes.length; ++i) {
293: if (!isMethodInvocationConvertible(methodArgs[i],
294: classes[i])) {
295: return false;
296: }
297: }
298:
299: return true;
300: }
301:
302: /**
303: * Determines whether a type represented by a class object is convertible to
304: * another type represented by a class object using a method invocation
305: * conversion, treating object types of primitive types as if they were
306: * primitive types (that is, a Boolean actual parameter type matches boolean
307: * primitive formal type). This behavior is because this method is used to
308: * determine applicable methods for an actual parameter list, and primitive
309: * types are represented by their object duals in reflective method calls.
310: *
311: * @param formal the formal parameter type to which the actual parameter
312: * type should be convertible
313: * @param actual the actual parameter type.
314: * @return true if either formal type is assignable from actual type, or
315: * formal is a primitive type and actual is its corresponding object
316: * type or an object type of a primitive type that can be converted
317: * to the formal type.
318: */
319: private static boolean isMethodInvocationConvertible(Class formal,
320: Class actual) {
321: /*
322: * if it's a null, it means the arg was null
323: */
324: if (actual == null && !formal.isPrimitive()) {
325: return true;
326: }
327:
328: /*
329: * Check for identity or widening reference conversion
330: */
331:
332: if (actual != null && formal.isAssignableFrom(actual)) {
333: return true;
334: }
335:
336: /*
337: * Check for boxing with widening primitive conversion. Note that actual
338: * parameters are never primitives.
339: */
340:
341: if (formal.isPrimitive()) {
342: if (formal == Boolean.TYPE && actual == Boolean.class) {
343: return true;
344: }
345: if (formal == Character.TYPE && actual == Character.class) {
346: return true;
347: }
348: if (formal == Byte.TYPE && actual == Byte.class) {
349: return true;
350: }
351: if (formal == Short.TYPE
352: && (actual == Short.class || actual == Byte.class)) {
353: return true;
354: }
355: if (formal == Integer.TYPE
356: && (actual == Integer.class
357: || actual == Short.class || actual == Byte.class)) {
358: return true;
359: }
360: if (formal == Long.TYPE
361: && (actual == Long.class || actual == Integer.class
362: || actual == Short.class || actual == Byte.class)) {
363: return true;
364: }
365: if (formal == Float.TYPE
366: && (actual == Float.class || actual == Long.class
367: || actual == Integer.class
368: || actual == Short.class || actual == Byte.class)) {
369: return true;
370: }
371: if (formal == Double.TYPE
372: && (actual == Double.class || actual == Float.class
373: || actual == Long.class
374: || actual == Integer.class
375: || actual == Short.class || actual == Byte.class)) {
376: return true;
377: }
378: }
379:
380: return false;
381: }
382:
383: /**
384: * Determines whether a type represented by a class object is convertible to
385: * another type represented by a class object using a method invocation
386: * conversion, without matching object and primitive types. This method is
387: * used to determine the more specific type when comparing signatures of
388: * methods.
389: *
390: * @param formal the formal parameter type to which the actual parameter
391: * type should be convertible
392: * @param actual the actual parameter type.
393: * @return true if either formal type is assignable from actual type, or
394: * formal and actual are both primitive types and actual can be
395: * subject to widening conversion to formal.
396: */
397: private static boolean isStrictMethodInvocationConvertible(
398: Class formal, Class actual) {
399: /*
400: * we shouldn't get a null into, but if so
401: */
402: if (actual == null && !formal.isPrimitive()) {
403: return true;
404: }
405:
406: /*
407: * Check for identity or widening reference conversion
408: */
409:
410: if (formal.isAssignableFrom(actual)) {
411: return true;
412: }
413:
414: /*
415: * Check for widening primitive conversion.
416: */
417:
418: if (formal.isPrimitive()) {
419: if (formal == Short.TYPE && (actual == Byte.TYPE)) {
420: return true;
421: }
422: if (formal == Integer.TYPE
423: && (actual == Short.TYPE || actual == Byte.TYPE)) {
424: return true;
425: }
426: if (formal == Long.TYPE
427: && (actual == Integer.TYPE || actual == Short.TYPE || actual == Byte.TYPE)) {
428: return true;
429: }
430: if (formal == Float.TYPE
431: && (actual == Long.TYPE || actual == Integer.TYPE
432: || actual == Short.TYPE || actual == Byte.TYPE)) {
433: return true;
434: }
435: if (formal == Double.TYPE
436: && (actual == Float.TYPE || actual == Long.TYPE
437: || actual == Integer.TYPE
438: || actual == Short.TYPE || actual == Byte.TYPE)) {
439: return true;
440: }
441: }
442: return false;
443: }
444: }
|