public java.lang.reflect.Method | getMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)Returns the specified method - if any.
String className = clazz.getName();
Map cache = getMethodCache();
Method method = null;
Map methods = null;
// Strategy is as follows:
// construct a MethodKey to represent the name/arguments
// of a method's signature.
//
// use the name of the class to retrieve a map of that
// class' methods
//
// if a map exists, use the MethodKey to find the
// associated value object. if that object is a Method
// instance, return it. if that object is anything
// else, then it's a reference to our NULL_OBJECT
// instance, indicating that we've previously tried
// and failed to find a method meeting our requirements,
// so return null
//
// if the map of methods for the class doesn't exist,
// or if no value is associated with our key in that map,
// then we perform a reflection-based search for the method.
//
// if we find a method in that search, we store it in the
// map using the key; otherwise, we store a reference
// to NULL_OBJECT using the key.
// Check the cache first.
MethodKey key = new MethodKey(methodName, parameterTypes);
methods = (Map) cache.get(clazz);
if (methods != null) {
Object o = methods.get(key);
if (o != null) { // cache hit
if (o instanceof Method) { // good cache hit
return (Method) o;
} else { // bad cache hit
// we hit the NULL_OBJECT, so this is a search
// that previously failed; no point in doing
// it again as it is a worst case search
// through the entire classpath.
return null;
}
} else {
// cache miss: fall through to reflective search
}
} else {
// cache miss: fall through to reflective search
}
try {
method = clazz.getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e1) {
if (!clazz.isPrimitive() && !className.startsWith("java.") && !className.startsWith("javax.")) {
try {
Class helper = ClassUtils.forName(className + "_Helper");
method = helper.getMethod(methodName, parameterTypes);
} catch (ClassNotFoundException e2) {
}
}
}
// first time we've seen this class: set up its method cache
if (methods == null) {
methods = new HashMap();
cache.put(clazz, methods);
}
// when no method is found, cache the NULL_OBJECT
// so that we don't have to repeat worst-case searches
// every time.
if (null == method) {
methods.put(key, NULL_OBJECT);
} else {
methods.put(key, method);
}
return method;
|