Class MethodMap
- java.lang.Object
-
- org.apache.velocity.util.introspection.MethodMap
-
public class MethodMap extends java.lang.Object- Version:
- $Id$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMethodMap.AmbiguousExceptionSimple distinguishable exception, used when we run across ambiguous overloading.private classMethodMap.Match
-
Field Summary
Fields Modifier and Type Field Description (package private) TypeConversionHandlerconversionHandlerprivate static intEQUIVALENTprivate static intEXPLICITLY_CONVERTIBLEprivate static intIMPLCITLY_CONVERTIBLEprivate static intINCOMPARABLEprivate static intLESS_SPECIFIC(package private) java.util.Map<java.lang.String,java.util.List<java.lang.reflect.Method>>methodByNameMapKeep track of all methods with the same name.private static intMORE_SPECIFICprivate static intNOT_CONVERTIBLEprivate static intSTRICTLY_CONVERTIBLEprivate static java.lang.reflect.MethodTRY_SET_ACCESSIBLE
-
Constructor Summary
Constructors Constructor Description MethodMap()Default constructorMethodMap(TypeConversionHandler conversionHandler)Constructor with provided conversion handler
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(java.lang.reflect.Method method)Add a method to a list of methods by name.private static booleancanAccess(java.lang.reflect.Method method)private intcompare(java.lang.reflect.Type[] t1, java.lang.reflect.Type[] t2)Determines which method signature (represented by a class array) is more specific.java.lang.reflect.Methodfind(java.lang.String methodName, java.lang.Object[] args)Find a method.java.util.List<java.lang.reflect.Method>get(java.lang.String key)Return a list of methods with the same name.static java.lang.reflect.MethodgetAccessibleMethodDeclaration(java.lang.reflect.Method method)Once we identified a best match of a specific call, walk up the chain of inheritance to find the first method which we are allowed to call through reflection.private intgetApplicability(java.lang.reflect.Method method, java.lang.Class<?>[] classes)Returns the applicability of the supplied method against actual argument types.private java.lang.reflect.MethodgetBestMatch(java.util.List<java.lang.reflect.Method> methods, java.lang.Class<?>[] args)private booleanisConvertible(java.lang.reflect.Type formal, java.lang.Class<?> actual, boolean possibleVarArg)Returns true ifactualis convertible toformalby implicit Java method call conversionsprivate booleanisExplicitlyConvertible(java.lang.reflect.Type formal, java.lang.Class<?> actual, boolean possibleVarArg)Returns true ifactualis convertible toformalusing an explicit converterprivate static booleanisStrictConvertible(java.lang.reflect.Type formal, java.lang.Class<?> actual, boolean possibleVarArg)Returns true ifactualis strictly convertible toformal(aka without implicit boxing/unboxing)private static booleanonlyNullOrObjects(java.lang.Class<?>[] args)private static booleantrySetAccessible(java.lang.reflect.Method method)
-
-
-
Field Detail
-
INCOMPARABLE
private static final int INCOMPARABLE
- See Also:
- Constant Field Values
-
MORE_SPECIFIC
private static final int MORE_SPECIFIC
- See Also:
- Constant Field Values
-
EQUIVALENT
private static final int EQUIVALENT
- See Also:
- Constant Field Values
-
LESS_SPECIFIC
private static final int LESS_SPECIFIC
- See Also:
- Constant Field Values
-
NOT_CONVERTIBLE
private static final int NOT_CONVERTIBLE
- See Also:
- Constant Field Values
-
EXPLICITLY_CONVERTIBLE
private static final int EXPLICITLY_CONVERTIBLE
- See Also:
- Constant Field Values
-
IMPLCITLY_CONVERTIBLE
private static final int IMPLCITLY_CONVERTIBLE
- See Also:
- Constant Field Values
-
STRICTLY_CONVERTIBLE
private static final int STRICTLY_CONVERTIBLE
- See Also:
- Constant Field Values
-
TRY_SET_ACCESSIBLE
private static final java.lang.reflect.Method TRY_SET_ACCESSIBLE
-
conversionHandler
TypeConversionHandler conversionHandler
-
methodByNameMap
java.util.Map<java.lang.String,java.util.List<java.lang.reflect.Method>> methodByNameMap
Keep track of all methods with the same name.
-
-
Constructor Detail
-
MethodMap
public MethodMap()
Default constructor
-
MethodMap
public MethodMap(TypeConversionHandler conversionHandler)
Constructor with provided conversion handler- Parameters:
conversionHandler- conversion handler- Since:
- 2.0
-
-
Method Detail
-
add
public void add(java.lang.reflect.Method method)
Add a method to a list of methods by name. For a particular class we are keeping track of all the methods with the same name.- Parameters:
method-
-
get
public java.util.List<java.lang.reflect.Method> get(java.lang.String key)
Return a list of methods with the same name.- Parameters:
key-- Returns:
- List list of methods
-
find
public java.lang.reflect.Method find(java.lang.String methodName, java.lang.Object[] args) throws MethodMap.AmbiguousExceptionFind a method. Attempts to find the most specific applicable method using the algorithm described in the JLS section 15.12.2 (with the exception that it can't distinguish a primitive type argument from an object type argument, since in reflection primitive type arguments are represented by their object counterparts, so for an argument of type (say) java.lang.Integer, it will not be able to decide between a method that takes int and a method that takes java.lang.Integer as a parameter.
This turns out to be a relatively rare case where this is needed - however, functionality like this is needed.
- Parameters:
methodName- name of methodargs- the actual arguments with which the method is called- Returns:
- the most specific applicable method, or null if no method is applicable.
- Throws:
MethodMap.AmbiguousException- if there is more than one maximally specific applicable method
-
onlyNullOrObjects
private static boolean onlyNullOrObjects(java.lang.Class<?>[] args)
-
getBestMatch
private java.lang.reflect.Method getBestMatch(java.util.List<java.lang.reflect.Method> methods, java.lang.Class<?>[] args)
-
getAccessibleMethodDeclaration
public static java.lang.reflect.Method getAccessibleMethodDeclaration(java.lang.reflect.Method method)
Once we identified a best match of a specific call, walk up the chain of inheritance to find the first method which we are allowed to call through reflection. This is needed to avoid IllegalAccessException, when a public API method is implemented by a class which is not exported.- Parameters:
method-- Returns:
-
canAccess
private static boolean canAccess(java.lang.reflect.Method method)
-
trySetAccessible
private static boolean trySetAccessible(java.lang.reflect.Method method)
-
compare
private int compare(java.lang.reflect.Type[] t1, java.lang.reflect.Type[] t2)Determines which method signature (represented by a class array) is more specific. This defines a partial ordering on the method signatures.- Parameters:
t1- first signature to comparet2- second signature to compare- Returns:
- MORE_SPECIFIC if c1 is more specific than c2, LESS_SPECIFIC if c1 is less specific than c2, INCOMPARABLE if they are incomparable.
-
getApplicability
private int getApplicability(java.lang.reflect.Method method, java.lang.Class<?>[] classes)Returns the applicability of the supplied method against actual argument types.- Parameters:
method- method that will be calledclasses- arguments to method- Returns:
- the level of applicability: 0 = not applicable 1 = explicitly applicable (i.e. using stock or custom conversion handlers) 2 = implicitly applicable (i.e. using JAva implicit boxing/unboxing and primitive types widening) 3 = strictly applicable
-
isConvertible
private boolean isConvertible(java.lang.reflect.Type formal, java.lang.Class<?> actual, boolean possibleVarArg)Returns true ifactualis convertible toformalby implicit Java method call conversions- Parameters:
formal-actual-possibleVarArg-- Returns:
- convertible
-
isStrictConvertible
private static boolean isStrictConvertible(java.lang.reflect.Type formal, java.lang.Class<?> actual, boolean possibleVarArg)Returns true ifactualis strictly convertible toformal(aka without implicit boxing/unboxing)- Parameters:
formal-actual-possibleVarArg-- Returns:
- convertible
-
isExplicitlyConvertible
private boolean isExplicitlyConvertible(java.lang.reflect.Type formal, java.lang.Class<?> actual, boolean possibleVarArg)Returns true ifactualis convertible toformalusing an explicit converter- Parameters:
formal-actual-possibleVarArg-- Returns:
-
-