|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.beanutils.MethodUtils
public class MethodUtils
Utility reflection methods focussed on methods in general rather than properties in particular.
There is an issue when invoking public methods contained in a default access superclass.
Reflection locates these methods fine and correctly assigns them as public.
However, an IllegalAccessException
is thrown if the method is invoked.
MethodUtils
contains a workaround for this situation.
It will attempt to call setAccessible
on this method.
If this call succeeds, then the method can be invoked as normal.
This call will only succeed when the application has sufficient security privilages.
If this call fails then a warning will be logged and the method may fail.
Nested Class Summary | |
---|---|
private static class |
MethodUtils.MethodDescriptor
Represents the key to looking up a Method by reflection. |
Field Summary | |
---|---|
private static java.util.Map |
cache
Stores a cache of MethodDescriptor -> Method in a WeakHashMap. |
private static boolean |
CACHE_METHODS
Indicates whether methods should be cached for improved performance. |
private static java.lang.Class[] |
EMPTY_CLASS_PARAMETERS
An empty class array |
private static java.lang.Object[] |
EMPTY_OBJECT_ARRAY
An empty object array |
private static boolean |
loggedAccessibleWarning
Only log warning about accessibility work around once. |
Constructor Summary | |
---|---|
MethodUtils()
|
Method Summary | |
---|---|
private static void |
cacheMethod(MethodUtils.MethodDescriptor md,
java.lang.reflect.Method method)
Add a method to the cache. |
static int |
clearCache()
Clear the method cache. |
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class clazz,
java.lang.reflect.Method method)
Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. |
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class parameterType)
Return an accessible method (that is, one that can be invoked via reflection) with given name and a single parameter. |
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via reflection) with given name and parameters. |
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.reflect.Method method)
Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. |
private static java.lang.reflect.Method |
getAccessibleMethodFromInterfaceNest(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via reflection) that implements the specified method, by scanning through all implemented interfaces and subinterfaces. |
private static java.lang.reflect.Method |
getAccessibleMethodFromSuperclass(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via reflection) by scanning through the superclasses. |
private static java.lang.reflect.Method |
getCachedMethod(MethodUtils.MethodDescriptor md)
Return the method from the cache, if present. |
static java.lang.reflect.Method |
getMatchingAccessibleMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameterTypes)
Find an accessible method that matches the given name and has compatible parameters. |
private static float |
getObjectTransformationCost(java.lang.Class srcClass,
java.lang.Class destClass)
Gets the number of steps required needed to turn the source class into the destination class. |
static java.lang.Class |
getPrimitiveType(java.lang.Class wrapperType)
Gets the class for the primitive type corresponding to the primitive wrapper class given. |
static java.lang.Class |
getPrimitiveWrapper(java.lang.Class primitiveType)
Gets the wrapper object class for the given primitive type class. |
private static float |
getTotalTransformationCost(java.lang.Class[] srcArgs,
java.lang.Class[] destArgs)
Returns the sum of the object transformation cost for each class in the source argument list. |
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object arg)
Invoke a method whose parameter type matches exactly the object type. |
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] args)
Invoke a method whose parameter types match exactly the object types. |
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class[] parameterTypes)
Invoke a method whose parameter types match exactly the parameter types given. |
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class objectClass,
java.lang.String methodName,
java.lang.Object arg)
Invoke a static method whose parameter type matches exactly the object type. |
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class objectClass,
java.lang.String methodName,
java.lang.Object[] args)
Invoke a static method whose parameter types match exactly the object types. |
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class objectClass,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class[] parameterTypes)
Invoke a static method whose parameter types match exactly the parameter types given. |
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object arg)
Invoke a named method whose parameter type matches the object type. |
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] args)
Invoke a named method whose parameter type matches the object type. |
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class[] parameterTypes)
Invoke a named method whose parameter type matches the object type. |
static java.lang.Object |
invokeStaticMethod(java.lang.Class objectClass,
java.lang.String methodName,
java.lang.Object arg)
Invoke a named static method whose parameter type matches the object type. |
static java.lang.Object |
invokeStaticMethod(java.lang.Class objectClass,
java.lang.String methodName,
java.lang.Object[] args)
Invoke a named static method whose parameter type matches the object type. |
static java.lang.Object |
invokeStaticMethod(java.lang.Class objectClass,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class[] parameterTypes)
Invoke a named static method whose parameter type matches the object type. |
static boolean |
isAssignmentCompatible(java.lang.Class parameterType,
java.lang.Class parameterization)
Determine whether a type can be used as a parameter in a method invocation. |
static void |
setCacheMethods(boolean cacheMethods)
Set whether methods should be cached for greater performance or not, default is true . |
private static void |
setMethodAccessible(java.lang.reflect.Method method)
Try to make the method accessible |
static java.lang.Class |
toNonPrimitiveClass(java.lang.Class clazz)
Find a non primitive representation for given primitive class. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static boolean loggedAccessibleWarning
Note that this is broken when this class is deployed via a shared classloader in a container, as the warning message will be emitted only once, not once per webapp. However making the warning appear once per webapp means having a map keyed by context classloader which introduces nasty memory-leak problems. As this warning is really optional we can ignore this problem; only one of the webapps will get the warning in its logs but that should be good enough.
private static boolean CACHE_METHODS
Note that when this class is deployed via a shared classloader in a container, this will affect all webapps. However making this configurable per webapp would mean having a map keyed by context classloader which may introduce memory-leak problems.
private static final java.lang.Class[] EMPTY_CLASS_PARAMETERS
private static final java.lang.Object[] EMPTY_OBJECT_ARRAY
private static final java.util.Map cache
The keys into this map only ever exist as temporary variables within methods of this class, and are never exposed to users of this class. This means that the WeakHashMap is used only as a mechanism for limiting the size of the cache, ie a way to tell the garbage collector that the contents of the cache can be completely garbage-collected whenever it needs the memory. Whether this is a good approach to this problem is doubtful; something like the commons-collections LRUMap may be more appropriate (though of course selecting an appropriate size is an issue).
This static variable is safe even when this code is deployed via a shared classloader because it is keyed via a MethodDescriptor object which has a Class as one of its members and that member is used in the MethodDescriptor.equals method. So two components that load the same class via different classloaders will generate non-equal MethodDescriptor objects and hence end up with different entries in the map.
Constructor Detail |
---|
public MethodUtils()
Method Detail |
---|
public static void setCacheMethods(boolean cacheMethods)
true
.
cacheMethods
- true
if methods should be
cached for greater performance, otherwise false
public static int clearCache()
public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named method whose parameter type matches the object type.
The behaviour of this method is less deterministic
than invokeExactMethod()
.
It loops through all methods with names that match
and then executes the first it finds with compatable parameters.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
class
would match a boolean
primitive.
This is a convenient wrapper for
invokeMethod(Object object,String methodName,Object [] args)
.
object
- invoke method on this objectmethodName
- get method with this namearg
- use this argument
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named method whose parameter type matches the object type.
The behaviour of this method is less deterministic
than invokeExactMethod(Object object,String methodName,Object [] args)
.
It loops through all methods with names that match
and then executes the first it finds with compatable parameters.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
class
would match a boolean
primitive.
This is a convenient wrapper for
invokeMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)
.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named method whose parameter type matches the object type.
The behaviour of this method is less deterministic
than invokeExactMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)
.
It loops through all methods with names that match
and then executes the first it finds with compatable parameters.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
class
would match a boolean
primitive.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a method whose parameter type matches exactly the object type.
This is a convenient wrapper for
invokeExactMethod(Object object,String methodName,Object [] args)
.
object
- invoke method on this objectmethodName
- get method with this namearg
- use this argument
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a method whose parameter types match exactly the object types.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod()
.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a method whose parameter types match exactly the parameter types given.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod()
.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactStaticMethod(java.lang.Class objectClass, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a static method whose parameter types match exactly the parameter types given.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[])
.
objectClass
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeStaticMethod(java.lang.Class objectClass, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named static method whose parameter type matches the object type.
The behaviour of this method is less deterministic
than invokeExactMethod(Object, String, Object[], Class[])
.
It loops through all methods with names that match
and then executes the first it finds with compatable parameters.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
class
would match a boolean
primitive.
This is a convenient wrapper for
invokeStaticMethod(Class objectClass,String methodName,Object [] args)
.
objectClass
- invoke static method on this classmethodName
- get method with this namearg
- use this argument
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeStaticMethod(java.lang.Class objectClass, java.lang.String methodName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named static method whose parameter type matches the object type.
The behaviour of this method is less deterministic
than invokeExactMethod(Object object,String methodName,Object [] args)
.
It loops through all methods with names that match
and then executes the first it finds with compatable parameters.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
class
would match a boolean
primitive.
This is a convenient wrapper for
invokeStaticMethod(Class objectClass,String methodName,Object [] args,Class[] parameterTypes)
.
objectClass
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeStaticMethod(java.lang.Class objectClass, java.lang.String methodName, java.lang.Object[] args, java.lang.Class[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named static method whose parameter type matches the object type.
The behaviour of this method is less deterministic
than invokeExactStaticMethod(Class objectClass,String methodName,Object [] args,Class[] parameterTypes)
.
It loops through all methods with names that match
and then executes the first it finds with compatable parameters.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
class
would match a boolean
primitive.
objectClass
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactStaticMethod(java.lang.Class objectClass, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a static method whose parameter type matches exactly the object type.
This is a convenient wrapper for
invokeExactStaticMethod(Class objectClass,String methodName,Object [] args)
.
objectClass
- invoke static method on this classmethodName
- get method with this namearg
- use this argument
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactStaticMethod(java.lang.Class objectClass, java.lang.String methodName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a static method whose parameter types match exactly the object types.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(Class, String, Class[])
.
objectClass
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
java.lang.NoSuchMethodException
- if there is no such accessible method
java.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invoked
java.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.reflect.Method getAccessibleMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class parameterType)
Return an accessible method (that is, one that can be invoked via
reflection) with given name and a single parameter. If no such method
can be found, return null
.
Basically, a convenience wrapper that constructs a Class
array for you.
clazz
- get method from this classmethodName
- get method with this nameparameterType
- taking this type of parameter
public static java.lang.reflect.Method getAccessibleMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via
reflection) with given name and parameters. If no such method
can be found, return null
.
This is just a convenient wrapper for
getAccessibleMethod(Method method)
.
clazz
- get method from this classmethodName
- get method with this nameparameterTypes
- with these parameters types
public static java.lang.reflect.Method getAccessibleMethod(java.lang.reflect.Method method)
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified Method. If no such method
can be found, return null
.
method
- The method that we wish to call
public static java.lang.reflect.Method getAccessibleMethod(java.lang.Class clazz, java.lang.reflect.Method method)
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified Method. If no such method
can be found, return null
.
clazz
- The class of the objectmethod
- The method that we wish to call
private static java.lang.reflect.Method getAccessibleMethodFromSuperclass(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via
reflection) by scanning through the superclasses. If no such method
can be found, return null
.
clazz
- Class to be checkedmethodName
- Method name of the method we wish to callparameterTypes
- The parameter type signaturesprivate static java.lang.reflect.Method getAccessibleMethodFromInterfaceNest(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified method, by scanning through
all implemented interfaces and subinterfaces. If no such method
can be found, return null
.
There isn't any good reason why this method must be private. It is because there doesn't seem any reason why other classes should call this rather than the higher level methods.
clazz
- Parent class for the interfaces to be checkedmethodName
- Method name of the method we wish to callparameterTypes
- The parameter type signaturespublic static java.lang.reflect.Method getMatchingAccessibleMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes)
Find an accessible method that matches the given name and has compatible parameters. Compatible parameters mean that every method parameter is assignable from the given parameters. In other words, it finds a method with the given name that will take the parameters given.
This method is slightly undeterminstic since it loops through methods names and return the first matching method.
This method is used by
invokeMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)
.
This method can match primitive parameter by passing in wrapper classes.
For example, a Boolean
will match a primitive boolean
parameter.
clazz
- find method in this classmethodName
- find method with this nameparameterTypes
- find method with compatible parameters
private static void setMethodAccessible(java.lang.reflect.Method method)
method
- The source argumentsprivate static float getTotalTransformationCost(java.lang.Class[] srcArgs, java.lang.Class[] destArgs)
srcArgs
- The source argumentsdestArgs
- The destination arguments
private static float getObjectTransformationCost(java.lang.Class srcClass, java.lang.Class destClass)
srcClass
- The source classdestClass
- The destination class
public static final boolean isAssignmentCompatible(java.lang.Class parameterType, java.lang.Class parameterization)
Determine whether a type can be used as a parameter in a method invocation. This method handles primitive conversions correctly.
In order words, it will match a Boolean
to a boolean
,
a Long
to a long
,
a Float
to a float
,
a Integer
to a int
,
and a Double
to a double
.
Now logic widening matches are allowed.
For example, a Long
will not match a int
.
parameterType
- the type of parameter accepted by the methodparameterization
- the type of parameter being tested
public static java.lang.Class getPrimitiveWrapper(java.lang.Class primitiveType)
boolean.class
returns Boolean.class
primitiveType
- the primitive type class for which a match is to be found
public static java.lang.Class getPrimitiveType(java.lang.Class wrapperType)
Boolean.class
returns a boolean.class
.
wrapperType
- the
public static java.lang.Class toNonPrimitiveClass(java.lang.Class clazz)
clazz
- the class to find a representation for, not null
private static java.lang.reflect.Method getCachedMethod(MethodUtils.MethodDescriptor md)
md
- The method descriptor
private static void cacheMethod(MethodUtils.MethodDescriptor md, java.lang.reflect.Method method)
md
- The method descriptormethod
- The method to cache
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |