Top Description Fields Constructors Methods
org.apache.commons.lang3.reflect

public Class MethodUtils

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, java.lang.reflect.Array, .Executable, .InvocationTargetException, .Method, .Type, .TypeVariable, java.util.ArrayList, .Arrays, .Collections, .Comparator, .Iterator, .LinkedHashSet, .List, .Map, .Objects, .Set, .TreeMap, java.util.stream.Collectors, .Stream, org.apache.commons.lang3.ArrayUtils, .ClassUtils, .ClassUtils.Interfaces, .Validate, org.apache.commons.lang3.stream.LangCollectors, .Streams

Utility reflection methods focused on Methods, 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 as public. However, an IllegalAccessException is thrown if the method is invoked.

MethodUtils contains a workaround for this situation. It will attempt to call java.lang.reflect.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.

Since
2.5

Field Summary

Modifier and TypeField and Description
private static final Comparator<Method>

Constructor Summary

AccessConstructor and Description
public
MethodUtils()
Deprecated TODO Make private in 4.0.

MethodUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
private static int

Returns:

the aggregate number of inheritance hops between assignable argument class types.
distance
(final Class<?>[]
the Class array to calculate the distance from.
fromClassArray
,
final Class<?>[]
the Class array to calculate the distance to.
toClassArray
)

Computes the aggregate number of inheritance hops between assignable argument class types.

public static Method

Returns:

The accessible method or null.
getAccessibleMethod
(final Class<?>
The implementing class, may be null.
cls
,
final Method
The method that we wish to call, may be null.
method
)

Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.

public static Method

Returns:

The accessible method.
getAccessibleMethod
(final Class<?>
get method from this class.
cls
,
final String
get method with this name.
methodName
,
final Class<?>...
with these parameters types.
parameterTypes
)

Gets an accessible method (that is, one that can be invoked via reflection) with given name and parameters.

public static Method

Returns:

The accessible method
getAccessibleMethod
(final Method
The method that we wish to call, may be null.
method
)

Gets an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.

private static Method

Returns:

the accessible method or null if not found.
getAccessibleMethodFromInterfaceNest
(Class<?>
Parent class for the interfaces to be checked.
cls
,
final String
Method name of the method we wish to call.
methodName
,
final Class<?>...
The parameter type signatures.
parameterTypes
)

Gets 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 Method

Returns:

the accessible method or null if not found.
getAccessibleMethodFromSuperclass
(final Class<?>
Class to be checked.
cls
,
final String
Method name of the method we wish to call.
methodName
,
final Class<?>...
The parameter type signatures.
parameterTypes
)

Gets an accessible method (that is, one that can be invoked via reflection) by scanning through the superclasses.

private static List<Class<?>>

Returns:

the combined List of superclasses and interfaces in order going up from this one null if null input.
getAllSuperclassesAndInterfaces
(final Class<?>
the class to look up, may be null.
cls
)

Gets a combination of ClassUtils#getAllSuperclasses(Class) and ClassUtils#getAllInterfaces(Class), one from superclasses, one from interfaces, and so on in a breadth first way.

public static <
the annotation type.
A extends Annotation
>
A

Returns:

the first matching annotation, or null if not found.
getAnnotation
(final Method
the Method to query, may be null.
method
,
final Class<A>
the Annotation to check if is present on the method.
annotationCls
,
final boolean
determines if a lookup in the entire inheritance hierarchy of the given class is performed if the annotation was not directly present.
searchSupers
,
final boolean
determines if underlying method has to be accessible.
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.

private static Method
getInvokeMethod(final boolean forceAccess, final String methodName, final Class<?>[] parameterTypes, final Class<? extends Object> cls)

public static Method

Returns:

The accessible method or null.
getMatchingAccessibleMethod
(final Class<?>
find method in this class.
cls
,
final String
find method with this name.
methodName
,
final Class<?>...
find method with most compatible parameters.
requestTypes
)

Gets an accessible method that matches the given name and has compatible parameters.

public static Method

Returns:

The method.
getMatchingMethod
(final Class<?>
The class that will be subjected to the method search.
cls
,
final String
The method that we wish to call.
methodName
,
final Class<?>...
Argument class types.
parameterTypes
)

Gets a method whether or not it's accessible.

public static Method

Returns:

a Method or null.
getMethodObject
(final Class<?> cls, final String
the name of the method.
name
,
final Class<?>...
the list of parameters.
parameterTypes
)

Gets a Method, or null if a checked Class#getMethod(String, Class...) exception is thrown.

public static List<Method>

Returns:

a list of Methods (possibly empty).
getMethodsListWithAnnotation
(final Class<?>
the Class to query.
cls
,
final Class<? extends Annotation>
the Annotation that must be present on a method to be matched.
annotationCls
)

Gets all class level public methods of the given class that are annotated with the given annotation.

public static List<Method>

Returns:

a list of Methods (possibly empty).
getMethodsListWithAnnotation
(final Class<?>
the Class to query.
cls
,
final Class<? extends Annotation>
the Annotation that must be present on a method to be matched.
annotationCls
,
final boolean
determines if a lookup in the entire inheritance hierarchy of the given class should be performed.
searchSupers
,
final boolean
determines if non-public methods should be considered.
ignoreAccess
)

Gets all methods of the given class that are annotated with the given annotation.

public static Method[]

Returns:

an array of Methods (possibly empty).
getMethodsWithAnnotation
(final Class<?>
the Class to query.
cls
,
final Class<? extends Annotation>
the java.lang.annotation.Annotation that must be present on a method to be matched.
annotationCls
)

Gets all class level public methods of the given class that are annotated with the given annotation.

public static Method[]

Returns:

an array of Methods (possibly empty).
getMethodsWithAnnotation
(final Class<?>
the Class to query.
cls
,
final Class<? extends Annotation>
the java.lang.annotation.Annotation that must be present on a method to be matched.
annotationCls
,
final boolean
determines if a lookup in the entire inheritance hierarchy of the given class should be performed.
searchSupers
,
final boolean
determines if non-public methods should be considered.
ignoreAccess
)

Gets all methods of the given class that are annotated with the given annotation.

public static Set<Method>

Returns:

a Set<Method> in ascending order from subclass to superclass.
getOverrideHierarchy
(final Method
lowest to consider.
method
,
final ClassUtils.Interfaces
whether to search interfaces, null implies false.
interfacesBehavior
)

Gets the hierarchy of overridden methods down to result respecting generics.

public static Object

Returns:

The value returned by the invoked method.
invokeExactMethod
(final Object
invoke method on this object.
object
,
final String
get method with this name.
methodName
)

Invokes a method whose parameter types match exactly the object type.

public static Object

Returns:

The value returned by the invoked method.
invokeExactMethod
(final Object
invoke method on this object.
object
,
final String
get method with this name.
methodName
,
final Object...
use these arguments - treat null as empty array.
args
)

Invokes a method whose parameter types match exactly the object types.

public static Object

Returns:

The value returned by the invoked method.
invokeExactMethod
(final Object
Invokes a method on this object.
object
,
final String
Gets a method with this name.
methodName
,
final Object[]
Method arguments - treat null as empty array.
args
,
final Class<?>[]
Match these parameters - treat null as empty array.
parameterTypes
)

Invokes a method whose parameter types match exactly the parameter types given.

public static Object

Returns:

The value returned by the invoked method.
invokeExactStaticMethod
(final Class<?>
invoke static method on this class.
cls
,
final String
get method with this name.
methodName
,
final Object...
use these arguments - treat null as empty array.
args
)

Invokes a static method whose parameter types match exactly the object types.

public static Object

Returns:

The value returned by the invoked method.
invokeExactStaticMethod
(final Class<?>
invoke static method on this class.
cls
,
final String
get method with this name.
methodName
,
final Object[]
use these arguments - treat null as empty array.
args
,
final Class<?>[]
match these parameters - treat null as empty array.
parameterTypes
)

Invokes a static method whose parameter types match exactly the parameter types given.

public static Object

Returns:

The value returned by the invoked method.
invokeMethod
(final Object
invoke method on this object.
object
,
final boolean
force access to invoke method even if it's not accessible.
forceAccess
,
final String
get method with this name.
methodName
)

Invokes a named method without parameters.

public static Object

Returns:

The value returned by the invoked method.
invokeMethod
(final Object
invoke method on this object.
object
,
final boolean
force access to invoke method even if it's not accessible.
forceAccess
,
final String
get method with this name.
methodName
,
final Object...
use these arguments - treat null as empty array.
args
)

Invokes a named method whose parameter type matches the object type.

public static Object

Returns:

The value returned by the invoked method.
invokeMethod
(final Object
invoke method on this object.
object
,
final boolean
force access to invoke method even if it's not accessible.
forceAccess
,
final String
get method with this name.
methodName
,
final Object[]
use these arguments - treat null as empty array.
args
,
final Class<?>[]
match these parameters - treat null as empty array.
parameterTypes
)

Invokes a named method whose parameter type matches the object type.

public static Object

Returns:

The value returned by the invoked method.
invokeMethod
(final Object
invoke method on this object.
object
,
final String
get method with this name.
methodName
)

Invokes a named method without parameters.

public static Object

Returns:

The value returned by the invoked method.
invokeMethod
(final Object
invoke method on this object.
object
,
final String
get method with this name.
methodName
,
final Object...
use these arguments - treat null as empty array.
args
)

Invokes a named method whose parameter type matches the object type.

public static Object

Returns:

The value returned by the invoked method.
invokeMethod
(final Object
invoke method on this object.
object
,
final String
get method with this name.
methodName
,
final Object[]
use these arguments - treat null as empty array.
args
,
final Class<?>[]
match these parameters - treat null as empty array.
parameterTypes
)

Invokes a named method whose parameter type matches the object type.

public static Object

Returns:

The value returned by the invoked method.
invokeStaticMethod
(final Class<?>
invoke static method on this class.
cls
,
final String
get method with this name.
methodName
,
final Object...
use these arguments - treat null as empty array.
args
)

Invokes a named static method whose parameter type matches the object type.

public static Object

Returns:

The value returned by the invoked method.
invokeStaticMethod
(final Class<?>
invoke static method on this class.
cls
,
final String
get method with this name.
methodName
,
final Object[]
use these arguments - treat null as empty array.
args
,
final Class<?>[]
match these parameters - treat null as empty array.
parameterTypes
)

Invokes a named static method whose parameter type matches the object type.

private static Method
requireNonNull(final Method method, final Class<?> cls, final String methodName, final Class<?>[] parameterTypes)

pack-priv static Object[]
toVarArgs(final Executable executable, final Object[] args)

private static Object[]

Returns:

an array of the variadic arguments passed to the method.
toVarArgs
(final Object[]
the array of arguments passed to the varags method.
args
,
final Class<?>[]
the declared array of method parameter types.
methodParameterTypes
)

Gets an array of arguments in the canonical form, given an arguments array passed to a varargs method, for example an array with the declared number of parameters, and whose last parameter is an array of the varargs type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait