Class MethodUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.MethodUtils
-
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.Known Limitations
Accessing Public Methods In A Default Access Superclass
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 aspublic
. However, anIllegalAccessException
is thrown if the method is invoked.MethodUtils
contains a workaround for this situation. It will attempt to callAccessibleObject.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.- Since:
- 2.5
-
-
Constructor Summary
Constructors Constructor Description MethodUtils()
MethodUtils
instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.static <A extends java.lang.annotation.Annotation>
AgetAnnotation(java.lang.reflect.Method method, java.lang.Class<A> annotationCls, boolean searchSupers, boolean ignoreAccess)
Gets the annotation object with the given annotation type that is present on the given method or optionally on any equivalent method in super classes and interfaces.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 class level public methods of the given class that are annotated with the given annotation.static java.util.List<java.lang.reflect.Method>
getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)
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 class level public 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, boolean searchSupers, boolean ignoreAccess)
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)
Gets the hierarchy of overridden methods down toresult
respecting generics.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 astatic
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 astatic
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 namedstatic
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 namedstatic
method whose parameter type matches the object type.
-
-
-
Constructor Detail
-
MethodUtils
public MethodUtils()
MethodUtils
instances should NOT be constructed in standard programming. Instead, the class should be used asMethodUtils.getAccessibleMethod(method)
.This constructor is
public
to permit tools that require a JavaBean instance to operate.
-
-
Method Detail
-
invokeMethod
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)
.- Parameters:
object
- invoke method on this objectmethodName
- get method with this name- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection- Since:
- 3.4
-
invokeMethod
public 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)
.- Parameters:
object
- invoke method on this objectforceAccess
- force access to invoke method even if it's not accessiblemethodName
- get method with this name- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection- Since:
- 3.5
-
invokeMethod
public 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 aboolean
primitive.This is a convenient wrapper for
invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
.- Parameters:
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeMethod
public 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 aboolean
primitive.This is a convenient wrapper for
invokeMethod(Object object, boolean forceAccess, String methodName, Object[] args, Class[] parameterTypes)
.- Parameters:
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 array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection- Since:
- 3.5
-
invokeMethod
public 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 aboolean
primitive.- Parameters:
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 array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection- Since:
- 3.5
-
invokeMethod
public 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 aboolean
primitive.- Parameters:
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- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeExactMethod
public 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[])}.- Parameters:
object
- invoke method on this objectmethodName
- get method with this name- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection- Since:
- 3.4
-
invokeExactMethod
public 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[])}.- Parameters:
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeExactMethod
public 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[])
.- Parameters:
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treatnull
as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeExactStaticMethod
public 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[])
.- Parameters:
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treatnull
as empty arrayparameterTypes
- match these parameters - treatnull
as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeStaticMethod
public 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 aboolean
primitive.This is a convenient wrapper for
invokeStaticMethod(Class, String, Object[], Class[])
.- Parameters:
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treatnull
as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeStaticMethod
public 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 aboolean
primitive.- Parameters:
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treatnull
as empty arrayparameterTypes
- match these parameters - treatnull
as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
invokeExactStaticMethod
public 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[])
.- Parameters:
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treatnull
as empty array- Returns:
- The value returned by the invoked method
- Throws:
java.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 reflection
-
getAccessibleMethod
public 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 forgetAccessibleMethod(Method)
.- Parameters:
cls
- get method from this classmethodName
- get method with this nameparameterTypes
- with these parameters types- Returns:
- The accessible method
-
getAccessibleMethod
public 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
.- Parameters:
method
- The method that we wish to call- Returns:
- The accessible method
-
getMatchingAccessibleMethod
public 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 primitiveboolean
parameter.- Parameters:
cls
- find method in this classmethodName
- find method with this nameparameterTypes
- find method with most compatible parameters- Returns:
- The accessible method
-
getMatchingMethod
public 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
.- Parameters:
cls
- The class that will be subjected to the method searchmethodName
- The method that we wish to callparameterTypes
- Argument class types- Returns:
- The method
- Since:
- 3.5
-
getOverrideHierarchy
public static java.util.Set<java.lang.reflect.Method> getOverrideHierarchy(java.lang.reflect.Method method, ClassUtils.Interfaces interfacesBehavior)
Gets the hierarchy of overridden methods down toresult
respecting generics.- Parameters:
method
- lowest to considerinterfacesBehavior
- whether to search interfaces,null
implies
false- Returns:
- Set<Method> in ascending order from sub- to superclass
- Throws:
java.lang.NullPointerException
- if the specified method isnull
- Since:
- 3.2
-
getMethodsWithAnnotation
public static java.lang.reflect.Method[] getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all class level public methods of the given class that are annotated with the given annotation.- Parameters:
cls
- theClass
to queryannotationCls
- theAnnotation
that must be present on a method to be matched- Returns:
- an array of Methods (possibly empty).
- Throws:
java.lang.NullPointerException
- if the class or annotation arenull
- Since:
- 3.4
-
getMethodsListWithAnnotation
public static java.util.List<java.lang.reflect.Method> getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all class level public methods of the given class that are annotated with the given annotation.- Parameters:
cls
- theClass
to queryannotationCls
- theAnnotation
that must be present on a method to be matched- Returns:
- a list of Methods (possibly empty).
- Throws:
java.lang.IllegalArgumentException
- if the class or annotation arenull
- Since:
- 3.4
-
getMethodsWithAnnotation
public static java.lang.reflect.Method[] getMethodsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)
Gets all methods of the given class that are annotated with the given annotation.- Parameters:
cls
- theClass
to queryannotationCls
- theAnnotation
that must be present on a method to be matchedsearchSupers
- determines if a lookup in the entire inheritance hierarchy of the given class should be performedignoreAccess
- determines if non public methods should be considered- Returns:
- an array of Methods (possibly empty).
- Throws:
java.lang.NullPointerException
- if the class or annotation arenull
- Since:
- 3.6
-
getMethodsListWithAnnotation
public static java.util.List<java.lang.reflect.Method> getMethodsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls, boolean searchSupers, boolean ignoreAccess)
Gets all methods of the given class that are annotated with the given annotation.- Parameters:
cls
- theClass
to queryannotationCls
- theAnnotation
that must be present on a method to be matchedsearchSupers
- determines if a lookup in the entire inheritance hierarchy of the given class should be performedignoreAccess
- determines if non public methods should be considered- Returns:
- a list of Methods (possibly empty).
- Throws:
java.lang.NullPointerException
- if either the class or annotation class isnull
- Since:
- 3.6
-
getAnnotation
public static <A extends java.lang.annotation.Annotation> A getAnnotation(java.lang.reflect.Method method, java.lang.Class<A> annotationCls, boolean searchSupers, boolean ignoreAccess)
Gets the annotation object with the given annotation type that is present on the given method or optionally on any equivalent method in super classes and interfaces. Returns null if the annotation type was not present.
Stops searching for an annotation once the first annotation of the specified type has been found. Additional annotations of the specified type will be silently ignored.
- Type Parameters:
A
- the annotation type- Parameters:
method
- theMethod
to queryannotationCls
- theAnnotation
to check if is present on the methodsearchSupers
- determines if a lookup in the entire inheritance hierarchy of the given class is performed if the annotation was not directly presentignoreAccess
- determines if underlying method has to be accessible- Returns:
- the first matching annotation, or
null
if not found - Throws:
java.lang.NullPointerException
- if either the method or annotation class isnull
- Since:
- 3.6
-
-