Class MethodUtils
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
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) 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<?> paramType) 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<?>[] paramTypes) 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<?>[] paramTypes) Find an accessible method that matches the given name and has compatible parameters.static Method
getMatchingAccessibleMethod
(Class<?> clazz, String methodName, Object[] args, Class<?>[] paramTypes) Find an accessible method that matches the given name and has compatible parameters.static Object
invokeExactMethod
(Object object, String methodName) Invoke a method whose parameter type matches exactly the object type.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<?>[] paramTypes) Invoke a method whose parameter types match exactly the parameter types given.static Object
invokeExactStaticMethod
(Class<?> objectClass, String methodName) Invoke a static method that has no parameters.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<?>[] paramTypes) Invoke a static method whose parameter types match exactly the parameter types given.static Object
invokeGetter
(Object object, String getterName) Gets an Object property from a bean.static Object
invokeGetter
(Object object, String getterName, Object arg) Gets an Object property from a bean.static Object
invokeGetter
(Object object, String getterName, Object[] args) Gets an Object property from a bean.static Object
invokeMethod
(Object object, String methodName) Invoke a named method whose parameter type matches the object type.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<?>[] paramTypes) Invoke a named method whose parameter type matches the object type.static void
invokeSetter
(Object object, String setterName, Object arg) Sets the value of a bean property to an Object.static void
invokeSetter
(Object object, String setterName, Object[] args) Sets the value of a bean property to an Object.static Object
invokeStaticMethod
(Class<?> objectClass, String methodName) Invoke a named static method that has no parameters.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<?>[] paramTypes) Invoke a named static method whose parameter type matches the object type.
-
Field Details
-
EMPTY_CLASS_PARAMETERS
An empty class array -
EMPTY_OBJECT_ARRAY
An empty object array -
NO_METHODS
-
-
Constructor Details
-
MethodUtils
public MethodUtils()
-
-
Method Details
-
invokeSetter
public static void invokeSetter(Object object, String setterName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Sets the value of a bean property to an Object.- Parameters:
object
- the bean to changesetterName
- the property name or setter method namearg
- use this argument- Throws:
NoSuchMethodException
- the no such method exceptionIllegalAccessException
- the illegal access exceptionInvocationTargetException
- the invocation target exception
-
invokeSetter
public static void invokeSetter(Object object, @NonNull String setterName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Sets the value of a bean property to an Object.- Parameters:
object
- the bean to changesetterName
- the property name or setter method nameargs
- use this arguments- Throws:
NoSuchMethodException
- the no such method exceptionIllegalAccessException
- the illegal access exceptionInvocationTargetException
- the invocation target exception
-
invokeGetter
public static Object invokeGetter(Object object, String getterName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Gets an Object property from a bean.- Parameters:
object
- the beangetterName
- the property name or getter method name- Returns:
- the property value (as an Object)
- Throws:
NoSuchMethodException
- the no such method exceptionIllegalAccessException
- the illegal access exceptionInvocationTargetException
- the invocation target exception
-
invokeGetter
public static Object invokeGetter(Object object, String getterName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Gets an Object property from a bean.- Parameters:
object
- the beangetterName
- the property name or getter method namearg
- use this argument- Returns:
- the property value (as an Object)
- Throws:
NoSuchMethodException
- the no such method exceptionIllegalAccessException
- the illegal access exceptionInvocationTargetException
- the invocation target exception
-
invokeGetter
public static Object invokeGetter(Object object, @NonNull String getterName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Gets an Object property from a bean.- Parameters:
object
- the beangetterName
- the property name or getter method nameargs
- use this arguments- Returns:
- the property value (as an Object)
- Throws:
NoSuchMethodException
- the no such method exceptionIllegalAccessException
- the illegal access exceptionInvocationTargetException
- the invocation target exception
-
invokeMethod
public static Object invokeMethod(Object object, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Invoke a named method whose parameter type matches the object type.
- Parameters:
object
- invoke method on this objectmethodName
- get method with this name- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible
-
invokeMethod
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 compatible parameters.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
invokeMethod(Object object,String methodName,Object[] args)
.- Parameters:
object
- invoke method on this objectmethodName
- get method with this namearg
- use this argument- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeMethod
public 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 compatible parameters.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
invokeMethod(Object object,String methodName,Object[] args,Class[] paramTypes)
.- 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:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeMethod
public static Object invokeMethod(Object object, String methodName, Object[] args, Class<?>[] paramTypes) 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[] paramTypes)
. 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
Boolean
class would match aboolean
primitive.- Parameters:
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparamTypes
- match these parameters - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactMethod
public static Object invokeExactMethod(Object object, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Invoke a method whose parameter type matches exactly the object type.
- Parameters:
object
- invoke method on this objectmethodName
- get method with this name- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactMethod
public 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 objectmethodName
- get method with this namearg
- use this argument- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactMethod
public 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 objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactMethod
public static Object invokeExactMethod(Object object, String methodName, Object[] args, Class<?>[] paramTypes) 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 objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparamTypes
- match these parameters - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactStaticMethod
public static Object invokeExactStaticMethod(Class<?> objectClass, String methodName, Object[] args, Class<?>[] paramTypes) 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 classmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparamTypes
- match these parameters - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> objectClass, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Invoke a named static method that has no parameters.- Parameters:
objectClass
- invoke static method on this classmethodName
- get method with this name- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeStaticMethod
public 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 compatible parameters.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 objectClass,String methodName,Object[] args)
.- Parameters:
objectClass
- invoke static method on this classmethodName
- get method with this namearg
- use this argument- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeStaticMethod
public 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 compatible parameters.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 objectClass,String methodName,Object[] args,Class[] paramTypes)
.- Parameters:
objectClass
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> objectClass, String methodName, Object[] args, Class<?>[] paramTypes) 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[] paramTypes)
. 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
Boolean
class would match aboolean
primitive.- Parameters:
objectClass
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparamTypes
- match these parameters - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactStaticMethod
public static Object invokeExactStaticMethod(Class<?> objectClass, String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException Invoke a static method that has no parameters.- Parameters:
objectClass
- invoke static method on this classmethodName
- get method with this name- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactStaticMethod
public 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 classmethodName
- get method with this namearg
- use this argument- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
invokeExactStaticMethod
public 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 classmethodName
- get method with this nameargs
- use these arguments - treat null as empty array- Returns:
- the value returned by the invoked method
- Throws:
NoSuchMethodException
- if there is no such accessible methodInvocationTargetException
- wraps an exception thrown by the method invokedIllegalAccessException
- if the requested method is not accessible via reflection
-
getAccessibleMethod
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 aClass
array for you.- Parameters:
clazz
- get method from this classmethodName
- get method with this name- Returns:
- the accessible method
-
getAccessibleMethod
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 aClass
array for you.- Parameters:
clazz
- get method from this classmethodName
- get method with this nameparamType
- taking this type of parameter- Returns:
- the accessible method
-
getAccessibleMethod
@Nullable public static Method getAccessibleMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) 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 classmethodName
- get method with this nameparamTypes
- with these parameters types- Returns:
- the accessible method
-
getAccessibleMethod
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
.- Parameters:
method
- the method that we wish to call- Returns:
- the accessible method
-
getAccessibleMethod
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
.- Parameters:
clazz
- The class of the objectmethod
- The method that we wish to call- Returns:
- the accessible method
-
getMatchingAccessibleMethod
@Nullable public static Method getMatchingAccessibleMethod(Class<?> clazz, String methodName, Object[] args, Class<?>[] paramTypes) 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[] paramTypes)
.This method can match primitive parameter by passing in wrapper classes. For example, a
Boolean
will match a primitiveboolean
parameter.- Parameters:
clazz
- find method in this classmethodName
- find method with this nameargs
- find method with given argumentsparamTypes
- find method with compatible parameters- Returns:
- the accessible method
-
getMatchingAccessibleMethod
public static Method getMatchingAccessibleMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) 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 can match primitive parameter by passing in wrapper classes. For example, a
Boolean
will match a primitiveboolean
parameter.- Parameters:
clazz
- find method in this classmethodName
- find method with this nameparamTypes
- find method with compatible parameters- Returns:
- the accessible method
-
clearCache
public static int clearCache()Clear the method cache.- Returns:
- the number of cached methods cleared
-