public class MethodUtils
extends java.lang.Object
Utility reflection methods focused on Method
s, originally from Commons BeanUtils.
Differences from the BeanUtils version may be noted, especially where similar functionality
already existed within Lang.
There is an issue when invoking public
methods contained in a default access superclass on JREs prior to 1.4.
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 AccessibleObject.setAccessible(boolean)
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 privileges.
If this call fails then the method may fail.
Constructor and Description |
---|
MethodUtils()
MethodUtils instances should NOT be constructed in standard programming. |
Modifier and Type | Method and Description |
---|---|
private static int |
distance(java.lang.Class<?>[] classArray,
java.lang.Class<?>[] toClassArray)
Returns the aggregate number of inheritance hops between assignable argument class types.
|
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns 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)
Returns 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<?> cls,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns 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<?> cls,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns an accessible method (that is, one that can be invoked via
reflection) by scanning through the superclasses.
|
static java.lang.reflect.Method |
getMatchingAccessibleMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Finds an accessible method that matches the given name and has compatible parameters.
|
static java.lang.reflect.Method |
getMatchingMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Retrieves a method whether or not it's accessible.
|
static java.util.List<java.lang.reflect.Method> |
getMethodsListWithAnnotation(java.lang.Class<?> cls,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all methods of the given class that are annotated with the given annotation.
|
static java.lang.reflect.Method[] |
getMethodsWithAnnotation(java.lang.Class<?> cls,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all methods of the given class that are annotated with the given annotation.
|
static java.util.Set<java.lang.reflect.Method> |
getOverrideHierarchy(java.lang.reflect.Method method,
ClassUtils.Interfaces interfacesBehavior)
Get the hierarchy of overridden methods down to
result respecting generics. |
(package private) static java.lang.Object[] |
getVarArgs(java.lang.Object[] args,
java.lang.Class<?>[] methodParameterTypes)
Given an arguments array passed to a varargs method, return an array of arguments in the canonical form,
i.e.
|
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName)
Invokes 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)
Invokes a method with no parameters.
|
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] parameterTypes)
Invokes a method whose parameter types match exactly the parameter
types given.
|
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Object... args)
Invokes a
static method whose parameter types match exactly the object
types. |
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] parameterTypes)
Invokes a
static method whose parameter types match exactly the parameter
types given. |
static java.lang.Object |
invokeMethod(java.lang.Object object,
boolean forceAccess,
java.lang.String methodName)
Invokes a named method without parameters.
|
static java.lang.Object |
invokeMethod(java.lang.Object object,
boolean forceAccess,
java.lang.String methodName,
java.lang.Object... args)
Invokes a named method whose parameter type matches the object type.
|
static java.lang.Object |
invokeMethod(java.lang.Object object,
boolean forceAccess,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] parameterTypes)
Invokes a named method whose parameter type matches the object type.
|
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName)
Invokes a named method without parameters.
|
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object... args)
Invokes 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)
Invokes a named method whose parameter type matches the object type.
|
static java.lang.Object |
invokeStaticMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Object... args)
Invokes a named
static method whose parameter type matches the object type. |
static java.lang.Object |
invokeStaticMethod(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] parameterTypes)
Invokes a named
static method whose parameter type matches the object type. |
private static java.lang.Object[] |
toVarArgs(java.lang.reflect.Method method,
java.lang.Object[] args) |
public MethodUtils()
MethodUtils
instances should NOT be constructed in standard programming.
Instead, the class should be used as
MethodUtils.getAccessibleMethod(method)
.
This constructor is public
to permit tools that require a JavaBean
instance to operate.
public static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a named method without parameters.
This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[])
.
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 namejava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible via reflectionpublic static java.lang.Object invokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a named method without parameters.
This is a convenient wrapper for
invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)
.
object
- invoke method on this objectforceAccess
- force access to invoke method even if it's not accessiblemethodName
- get method with this namejava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.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
Invokes a named method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[])
.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
object
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 arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible via reflectionpublic static java.lang.Object invokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a named method whose parameter type matches the object type.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
object
would match a boolean
primitive.
This is a convenient wrapper for
invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)
.
object
- invoke method on this objectforceAccess
- force access to invoke method even if it's not accessiblemethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible via reflectionpublic static java.lang.Object invokeMethod(java.lang.Object object, boolean forceAccess, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a named method whose parameter type matches the object type.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
object
would match a boolean
primitive.
object
- invoke method on this objectforceAccess
- force access to invoke method even if it's not accessiblemethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.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
Invokes a named method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[])
.
This method supports calls to methods taking primitive parameters
via passing in wrapping classes. So, for example, a Boolean
object
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 arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible via reflectionpublic static java.lang.Object invokeExactMethod(java.lang.Object object, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a method whose parameter types match exactly the object types.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...)
(Class,String,Class[])}.
object
- invoke method on this objectmethodName
- get method with this namejava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.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
Invokes a method with no parameters.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>...)
(Class,String,Class[])}.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.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
Invokes a 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[])
.
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 arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeExactStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes 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[])
.
cls
- 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 arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a named static
method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[])
.
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, String, Object[], Class[])
.
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null
as empty arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.Object invokeStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] parameterTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes a named static
method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod(Class, String, Class[])
.
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.
cls
- 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 arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionprivate static java.lang.Object[] toVarArgs(java.lang.reflect.Method method, java.lang.Object[] args)
static java.lang.Object[] getVarArgs(java.lang.Object[] args, java.lang.Class<?>[] methodParameterTypes)
Given an arguments array passed to a varargs method, return an array of arguments in the canonical form, i.e. an array with the declared number of parameters, and whose last parameter is an array of the varargs type.
args
- the array of arguments passed to the varags methodmethodParameterTypes
- the declared array of method parameter typespublic static java.lang.Object invokeExactStaticMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Object... args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invokes 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[])
.
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null
as empty arrayjava.lang.NoSuchMethodException
- if there is no such accessible methodjava.lang.reflect.InvocationTargetException
- wraps an exception thrown by the
method invokedjava.lang.IllegalAccessException
- if the requested method is not accessible
via reflectionpublic static java.lang.reflect.Method getAccessibleMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Returns 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 convenience wrapper for
getAccessibleMethod(Method)
.
cls
- get method from this classmethodName
- get method with this nameparameterTypes
- with these parameters typespublic static java.lang.reflect.Method getAccessibleMethod(java.lang.reflect.Method method)
Returns 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 callprivate static java.lang.reflect.Method getAccessibleMethodFromSuperclass(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Returns 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
.
cls
- Class to be checkedmethodName
- Method name of the method we wish to callparameterTypes
- The parameter type signaturesnull
if not foundprivate static java.lang.reflect.Method getAccessibleMethodFromInterfaceNest(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Returns 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.
cls
- Parent class for the interfaces to be checkedmethodName
- Method name of the method we wish to callparameterTypes
- The parameter type signaturesnull
if not foundpublic static java.lang.reflect.Method getMatchingAccessibleMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Finds 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 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.
cls
- find method in this classmethodName
- find method with this nameparameterTypes
- find method with most compatible parameterspublic static java.lang.reflect.Method getMatchingMethod(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
Retrieves a method whether or not it's accessible. If no such method
can be found, return null
.
cls
- The class that will be subjected to the method searchmethodName
- The method that we wish to callparameterTypes
- Argument class typesprivate static int distance(java.lang.Class<?>[] classArray, java.lang.Class<?>[] toClassArray)
Returns the aggregate number of inheritance hops between assignable argument class types. Returns -1 if the arguments aren't assignable. Fills a specific purpose for getMatchingMethod and is not generalized.
classArray
- toClassArray
- public static java.util.Set<java.lang.reflect.Method> getOverrideHierarchy(java.lang.reflect.Method method, ClassUtils.Interfaces interfacesBehavior)
result
respecting generics.method
- lowest to considerinterfacesBehavior
- whether to search interfaces, null
implies
falsejava.lang.NullPointerException
- if the specified method is null
public static java.lang.reflect.Method[] getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
cls
- the Class
to queryannotationCls
- the Annotation
that must be present on a method to be matchedjava.lang.IllegalArgumentException
- if the class or annotation are null
public static java.util.List<java.lang.reflect.Method> getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
cls
- the Class
to queryannotationCls
- the Annotation
that must be present on a method to be matchedjava.lang.IllegalArgumentException
- if the class or annotation are null