public class MethodUtils extends Object
Utility reflection methods focussed on methods in general rather than properties in particular.
Copied from commons-beanutils, removed dependencies to commons-logging
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.
Constructor and Description |
---|
MethodUtils() |
Modifier and Type | Method and Description |
---|---|
static int |
clearCache()
Clear the method cache.
|
static Method |
getAccessibleMethod(Class clazz,
Method method)
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified Method.
|
static Method |
getAccessibleMethod(Class clazz,
String methodName,
Class parameterType)
Return an accessible method (that is, one that can be invoked via
reflection) with given name and a single parameter.
|
static Method |
getAccessibleMethod(Class clazz,
String methodName,
Class[] parameterTypes)
Return an accessible method (that is, one that can be invoked via
reflection) with given name and parameters.
|
static Method |
getAccessibleMethod(Method method)
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified Method.
|
static Method |
getMatchingAccessibleMethod(Class clazz,
String methodName,
Class[] parameterTypes)
Find an accessible method that matches the given name and has compatible parameters.
|
static Class |
getPrimitiveType(Class wrapperType)
Gets the class for the primitive type corresponding to the primitive wrapper class given.
|
static Class |
getPrimitiveWrapper(Class primitiveType)
Gets the wrapper object class for the given primitive type class.
|
static Object |
invokeExactMethod(Object object,
String methodName,
Object arg)
Invoke a method whose parameter type matches exactly the object
type.
|
static Object |
invokeExactMethod(Object object,
String methodName,
Object[] args)
Invoke a method whose parameter types match exactly the object
types.
|
static Object |
invokeExactMethod(Object object,
String methodName,
Object[] args,
Class[] parameterTypes)
Invoke a method whose parameter types match exactly the parameter
types given.
|
static Object |
invokeExactStaticMethod(Class objectClass,
String methodName,
Object arg)
Invoke a static method whose parameter type matches exactly the object
type.
|
static Object |
invokeExactStaticMethod(Class objectClass,
String methodName,
Object[] args)
Invoke a static method whose parameter types match exactly the object
types.
|
static Object |
invokeExactStaticMethod(Class objectClass,
String methodName,
Object[] args,
Class[] parameterTypes)
Invoke a static method whose parameter types match exactly the parameter
types given.
|
static Object |
invokeMethod(Object object,
String methodName,
Object arg)
Invoke a named method whose parameter type matches the object type.
|
static Object |
invokeMethod(Object object,
String methodName,
Object[] args)
Invoke a named method whose parameter type matches the object type.
|
static Object |
invokeMethod(Object object,
String methodName,
Object[] args,
Class[] parameterTypes)
Invoke a named method whose parameter type matches the object type.
|
static Object |
invokeSafe(Method method,
Object instance,
Object[] args) |
static Object |
invokeStaticMethod(Class objectClass,
String methodName,
Object arg)
Invoke a named static method whose parameter type matches the object type.
|
static Object |
invokeStaticMethod(Class objectClass,
String methodName,
Object[] args)
Invoke a named static method whose parameter type matches the object type.
|
static Object |
invokeStaticMethod(Class objectClass,
String methodName,
Object[] args,
Class[] parameterTypes)
Invoke a named static method whose parameter type matches the object type.
|
static boolean |
isAssignmentCompatible(Class<?> parameterType,
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 . |
static Class |
toNonPrimitiveClass(Class clazz)
Find a non primitive representation for given primitive class.
|
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 Object invokeMethod(Object object, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, 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 argumentNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeMethod(Object object, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeExactMethod(Object object, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, 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 argumentNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeExactMethod(Object object, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeExactStaticMethod(Class objectClass, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeStaticMethod(Class objectClass, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, 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 argumentNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeStaticMethod(Class objectClass, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeStaticMethod(Class objectClass, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeExactStaticMethod(Class objectClass, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, 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 argumentNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Object invokeExactStaticMethod(Class objectClass, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, 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 arrayNoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the
method invokedIllegalAccessException
- if the requested method is not accessible
via reflectionpublic static Method getAccessibleMethod(Class clazz, String methodName, 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 parameterpublic static Method getAccessibleMethod(Class clazz, String methodName, 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 typespublic static Method getAccessibleMethod(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 callpublic static Method getAccessibleMethod(Class clazz, 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 callpublic static Method getMatchingAccessibleMethod(Class clazz, String methodName, 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 parameterspublic static boolean isAssignmentCompatible(Class<?> parameterType, 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 testedpublic static Class getPrimitiveWrapper(Class primitiveType)
boolean.class
returns Boolean.class
primitiveType
- the primitive type class for which a match is to be foundpublic static Class getPrimitiveType(Class wrapperType)
Boolean.class
returns a boolean.class
.wrapperType
- thepublic static Class toNonPrimitiveClass(Class clazz)
clazz
- the class to find a representation for, not null