org.apache.commons.jexl.util.introspection
Class ClassMap

java.lang.Object
  extended by org.apache.commons.jexl.util.introspection.ClassMap

public class ClassMap
extends java.lang.Object

Taken from the Velocity tree so we can be self-sufficient A cache of introspection information for a specific class instance. Keys java.lang.Method objects by a concatenation of the method name and the names of classes that make up the parameters.

Since:
1.0
Version:
$Id: ClassMap.java 398459 2006-04-30 23:14:30Z dion $
Author:
Jason van Zyl, Bob McWhirter, Attila Szegedi, Geir Magnusson Jr.

Nested Class Summary
private static class ClassMap.CacheMiss
          represents a miss on the cached data.
private static class ClassMap.MethodInfo
          Used for the iterative discovery process for public methods.
 
Field Summary
private static ClassMap.CacheMiss CACHE_MISS
          constant for a miss on the cached data.
private  java.lang.Class clazz
          Class passed into the constructor used to as the basis for the Method map.
private  java.util.Map methodCache
          Cache of Methods, or CACHE_MISS, keyed by method name and actual arguments used to find it.
private  MethodMap methodMap
          map from method name and args to a Method.
private static java.lang.Object OBJECT
          represents null or missing arguments.
 
Constructor Summary
ClassMap(java.lang.Class aClass)
          Standard constructor.
 
Method Summary
 java.lang.reflect.Method findMethod(java.lang.String name, java.lang.Object[] params)
          Find a Method using the methodKey provided.
private static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz)
          Retrieves public methods for a class.
private static int getAccessibleMethods(java.lang.Class clazz, ClassMap.MethodInfo[] methodInfos, int upcastCount)
          Recursively finds a match for each method, starting with the class, and then searching the superclass and interfaces.
(package private)  java.lang.Class getCachedClass()
           
private static java.lang.reflect.Method getPublicMethod(java.lang.Class clazz, java.lang.String name, java.lang.Class[] paramTypes)
          Looks up the method with specified name and signature in the first public superclass or implemented interface of the class.
static java.lang.reflect.Method getPublicMethod(java.lang.reflect.Method method)
          For a given method, retrieves its publicly accessible counterpart.
private  java.lang.String makeMethodKey(java.lang.reflect.Method method)
          Make a methodKey for the given method using the concatenation of the name and the types of the method parameters.
private static java.lang.String makeMethodKey(java.lang.String method, java.lang.Object[] params)
           
private  void populateMethodCache()
          Populate the Map of direct hits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CACHE_MISS

private static final ClassMap.CacheMiss CACHE_MISS
constant for a miss on the cached data.


OBJECT

private static final java.lang.Object OBJECT
represents null or missing arguments.


clazz

private java.lang.Class clazz
Class passed into the constructor used to as the basis for the Method map.


methodCache

private final java.util.Map methodCache
Cache of Methods, or CACHE_MISS, keyed by method name and actual arguments used to find it.


methodMap

private final MethodMap methodMap
map from method name and args to a Method.

Constructor Detail

ClassMap

public ClassMap(java.lang.Class aClass)
Standard constructor.

Parameters:
aClass - the class to deconstruct.
Method Detail

getCachedClass

java.lang.Class getCachedClass()
Returns:
the class object whose methods are cached by this map.

findMethod

public java.lang.reflect.Method findMethod(java.lang.String name,
                                           java.lang.Object[] params)
                                    throws MethodMap.AmbiguousException
Find a Method using the methodKey provided. Look in the methodMap for an entry. If found, it'll either be a CACHE_MISS, in which case we simply give up, or it'll be a Method, in which case, we return it. If nothing is found, then we must actually go and introspect the method from the MethodMap.

Parameters:
name - method name
params - method parameters
Returns:
CACHE_MISS or a Method
Throws:
MethodMap.AmbiguousException - if the method and parameters are ambiguous.

populateMethodCache

private void populateMethodCache()
Populate the Map of direct hits. These are taken from all the public methods that our class provides.


makeMethodKey

private java.lang.String makeMethodKey(java.lang.reflect.Method method)
Make a methodKey for the given method using the concatenation of the name and the types of the method parameters.


makeMethodKey

private static java.lang.String makeMethodKey(java.lang.String method,
                                              java.lang.Object[] params)

getAccessibleMethods

private static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz)
Retrieves public methods for a class. In case the class is not public, retrieves methods with same signature as its public methods from public superclasses and interfaces (if they exist). Basically upcasts every method to the nearest acccessible method.


getAccessibleMethods

private static int getAccessibleMethods(java.lang.Class clazz,
                                        ClassMap.MethodInfo[] methodInfos,
                                        int upcastCount)
Recursively finds a match for each method, starting with the class, and then searching the superclass and interfaces.

Parameters:
clazz - Class to check
methodInfos - array of methods we are searching to match
upcastCount - current number of methods we have matched
Returns:
count of matched methods

getPublicMethod

public static java.lang.reflect.Method getPublicMethod(java.lang.reflect.Method method)
For a given method, retrieves its publicly accessible counterpart. This method will look for a method with same name and signature declared in a public superclass or implemented interface of this method's declaring class. This counterpart method is publicly callable.

Parameters:
method - a method whose publicly callable counterpart is requested.
Returns:
the publicly callable counterpart method. Note that if the parameter method is itself declared by a public class, this method is an identity function.

getPublicMethod

private static java.lang.reflect.Method getPublicMethod(java.lang.Class clazz,
                                                        java.lang.String name,
                                                        java.lang.Class[] paramTypes)
Looks up the method with specified name and signature in the first public superclass or implemented interface of the class.

Parameters:
class - the class whose method is sought
name - the name of the method
paramTypes - the classes of method parameters