Class MethodUtils
Utility reflection methods focused on methods in general rather than properties in particular.
Known Limitations
Accessing Public Methods In A Default Access Superclass
 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 privileges. If this call fails then a warning will
 be logged and the method may fail.
 
- 
Method SummaryModifier and TypeMethodDescriptionstatic intClear the method cache.static MethodgetAccessibleMethod(Class<?> clazz, Method method) Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.static MethodgetAccessibleMethod(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 MethodgetAccessibleMethod(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 MethodgetAccessibleMethod(Method method) Return an accessible method (that is, one that can be invoked via reflection) that implements the specified Method.static MethodgetMatchingAccessibleMethod(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 ObjectinvokeExactMethod(Object object, String methodName, Object arg) Invoke a method whose parameter type matches exactly the object type.static ObjectinvokeExactMethod(Object object, String methodName, Object[] args) Invoke a method whose parameter types match exactly the object types.static ObjectinvokeExactMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes) Invoke a method whose parameter types match exactly the parameter types given.static ObjectinvokeExactStaticMethod(Class<?> objectClass, String methodName, Object arg) Invoke a static method whose parameter type matches exactly the object type.static ObjectinvokeExactStaticMethod(Class<?> objectClass, String methodName, Object[] args) Invoke a static method whose parameter types match exactly the object types.static ObjectinvokeExactStaticMethod(Class<?> objectClass, String methodName, Object[] args, Class<?>[] parameterTypes) Invoke a static method whose parameter types match exactly the parameter types given.static ObjectinvokeMethod(Object object, String methodName, Object arg) Invoke a named method whose parameter type matches the object type.static ObjectinvokeMethod(Object object, String methodName, Object[] args) Invoke a named method whose parameter type matches the object type.static ObjectinvokeMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes) Invoke a named method whose parameter type matches the object type.static ObjectinvokeStaticMethod(Class<?> objectClass, String methodName, Object arg) Invoke a named static method whose parameter type matches the object type.static ObjectinvokeStaticMethod(Class<?> objectClass, String methodName, Object[] args) Invoke a named static method whose parameter type matches the object type.static ObjectinvokeStaticMethod(Class<?> objectClass, String methodName, Object[] args, Class<?>[] parameterTypes) Invoke a named static method whose parameter type matches the object type.static booleanisAssignmentCompatible(Class<?> parameterType, Class<?> parameterization) Determine whether a type can be used as a parameter in a method invocation.static voidsetCacheMethods(boolean cacheMethods) Sets whether methods should be cached for greater performance or not, default istrue.static Class<?> toNonPrimitiveClass(Class<?> clazz) Find a non primitive representation for given primitive class.
- 
Method Details- 
clearCacheClear the method cache.- Returns:
- the number of cached methods cleared
- Since:
- 1.8.0
 
- 
getAccessibleMethodReturn 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:
- clazz- The class of the object
- method- The method that we wish to call
- Returns:
- The accessible method
- Since:
- 1.8.0
 
- 
getAccessibleMethodReturn 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 aClassarray for you.- Parameters:
- clazz- get method from this class
- methodName- get method with this name
- parameterType- taking this type of parameter
- Returns:
- The accessible method
 
- 
getAccessibleMethodpublic 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 forgetAccessibleMethod(Method method).- Parameters:
- clazz- get method from this class
- methodName- get method with this name
- parameterTypes- with these parameters types
- Returns:
- The accessible method
 
- 
getAccessibleMethodReturn 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
 
- 
getMatchingAccessibleMethodpublic 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 indeterministic 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</code> will match a primitive <code>booleanparameter.- Parameters:
- clazz- find method in this class
- methodName- find method with this name
- parameterTypes- find method with compatible parameters
- Returns:
- The accessible method
 
- 
getPrimitiveTypeGets the class for the primitive type corresponding to the primitive wrapper class given. For example, an instance ofBoolean.class</code> returns a <code>boolean.class.- Parameters:
- wrapperType- the
- Returns:
- the primitive type class corresponding to the given wrapper class, null if no match is found
 
- 
getPrimitiveWrapperGets the wrapper object class for the given primitive type class. For example, passingboolean.class</code> returns <code>Boolean.class- Parameters:
- primitiveType- the primitive type class for which a match is to be found
- Returns:
- the wrapper type associated with the given primitive or null if no match is found
 
- 
invokeExactMethodpublic 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).- Parameters:
- object- invoke method on this object
- methodName- get method with this name
- arg- use this argument. May be null (this will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
 
- 
invokeExactMethodpublic 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().- Parameters:
- object- invoke method on this object
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
 
- 
invokeExactMethodpublic 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().- Parameters:
- object- invoke method on this object
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- parameterTypes- match these parameters - treat null as empty array
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
 
- 
invokeExactStaticMethodpublic 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).- Parameters:
- objectClass- invoke static method on this class
- methodName- get method with this name
- arg- use this argument. May be null (this will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
- Since:
- 1.8.0
 
- 
invokeExactStaticMethodpublic 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[]).- Parameters:
- objectClass- invoke static method on this class
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
- Since:
- 1.8.0
 
- 
invokeExactStaticMethodpublic 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[]).- Parameters:
- objectClass- invoke static method on this class
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- parameterTypes- match these parameters - treat null as empty array
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
- Since:
- 1.8.0
 
- 
invokeMethodpublic 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 behavior 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 compatible parameters.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Booleanclass would match abooleanprimitive.This is a convenient wrapper for invokeMethod(Object object,String methodName,Object [] args).- Parameters:
- object- invoke method on this object
- methodName- get method with this name
- arg- use this argument. May be null (this will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
 
- 
invokeMethodpublic 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 behavior 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 compatible parameters.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Booleanclass would match abooleanprimitive.This is a convenient wrapper for invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes).- Parameters:
- object- invoke method on this object
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
 
- 
invokeMethodpublic 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 behavior 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 compatible parameters.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Booleanclass would match abooleanprimitive.- Parameters:
- object- invoke method on this object
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- parameterTypes- match these parameters - treat null as empty array
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
 
- 
invokeStaticMethodpublic 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 behavior 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 compatible parameters.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Booleanclass would match abooleanprimitive.This is a convenient wrapper for invokeStaticMethod(Class objectClass,String methodName,Object [] args).- Parameters:
- objectClass- invoke static method on this class
- methodName- get method with this name
- arg- use this argument. May be null (this will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
- Since:
- 1.8.0
 
- 
invokeStaticMethodpublic 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 behavior 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 compatible parameters.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Booleanclass would match abooleanprimitive.This is a convenient wrapper for invokeStaticMethod(Class objectClass, String methodName, Object[] args, Class[] parameterTypes).- Parameters:
- objectClass- invoke static method on this class
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
- Since:
- 1.8.0
 
- 
invokeStaticMethodpublic 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 behavior 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 compatible parameters.This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a Booleanclass would match abooleanprimitive.- Parameters:
- objectClass- invoke static method on this class
- methodName- get method with this name
- args- use these arguments - treat null as empty array (passing null will result in calling the parameterless method with name- methodName).
- parameterTypes- match these parameters - treat null as empty array
- Returns:
- The value returned by the invoked method
- Throws:
- NoSuchMethodException- if there is no such accessible method
- InvocationTargetException- wraps an exception thrown by the method invoked
- IllegalAccessException- if the requested method is not accessible via reflection
- Since:
- 1.8.0
 
- 
isAssignmentCompatibleDetermine 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</code> to a <code>boolean, aLong</code> to a <code>long, aFloat</code> to a <code>float, aInteger</code> to a <code>int, and aDouble</code> to a <code>double. Now logic widening matches are allowed. For example, aLong</code> will not match a <code>int.- Parameters:
- parameterType- the type of parameter accepted by the method
- parameterization- the type of parameter being tested
- Returns:
- true if the assignment is compatible.
 
- 
setCacheMethodsSets whether methods should be cached for greater performance or not, default istrue.- Parameters:
- cacheMethods-- trueif methods should be cached for greater performance, otherwise- false
- Since:
- 1.8.0
 
- 
toNonPrimitiveClassFind a non primitive representation for given primitive class.- Parameters:
- clazz- the class to find a representation for, not null
- Returns:
- the original class if it not a primitive. Otherwise the wrapper class. Not null
 
 
-