public class MethodUtils
extends java.lang.Object
Modifier and Type | Field | Description |
---|---|---|
static java.lang.Class<?>[] |
EMPTY_CLASS_PARAMETERS |
An empty class array
|
static java.lang.Object[] |
EMPTY_OBJECT_ARRAY |
An empty object array
|
Constructor | Description |
---|---|
MethodUtils() |
Modifier and Type | Method | Description |
---|---|---|
static int |
clearCache() |
Clear the method cache.
|
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class<?> clazz,
java.lang.reflect.Method method) |
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified Method.
|
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class<?> clazz,
java.lang.String methodName) |
Return an accessible method (that is, one that can be invoked via
reflection) with given name and a single parameter.
|
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?> paramType) |
Return an accessible method (that is, one that can be invoked via
reflection) with given name and a single parameter.
|
static java.lang.reflect.Method |
getAccessibleMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>[] paramTypes) |
Return 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) |
Return an accessible method (that is, one that can be invoked via
reflection) that implements the specified Method.
|
static java.lang.reflect.Method |
getMatchingAccessibleMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>[] paramTypes) |
Find an accessible method that matches the given name and has compatible parameters.
|
static java.lang.reflect.Method |
getMatchingAccessibleMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] paramTypes) |
Find an accessible method that matches the given name and has compatible parameters.
|
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object arg) |
Invoke a method whose parameter type matches exactly the object type.
|
static java.lang.Object |
invokeExactMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Object[] args) |
Invoke 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,
java.lang.Class<?>[] paramTypes) |
Invoke a method whose parameter types match exactly the parameter types given.
|
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName) |
Invoke a static method that has no parameters.
|
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName,
java.lang.Object arg) |
Invoke a static method whose parameter type matches exactly the object type.
|
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName,
java.lang.Object[] args) |
Invoke a static method whose parameter types match exactly the object types.
|
static java.lang.Object |
invokeExactStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] paramTypes) |
Invoke a static method whose parameter types match exactly the parameter types given.
|
static java.lang.Object |
invokeGetter(java.lang.Object object,
java.lang.String getterName) |
Gets an Object property from a bean.
|
static java.lang.Object |
invokeGetter(java.lang.Object object,
java.lang.String getterName,
java.lang.Object arg) |
Gets an Object property from a bean.
|
static java.lang.Object |
invokeGetter(java.lang.Object object,
java.lang.String getterName,
java.lang.Object[] args) |
Gets an Object property from a bean.
|
static java.lang.Object |
invokeMethod(java.lang.Object object,
java.lang.String methodName) |
Invoke 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 arg) |
Invoke 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) |
Invoke 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<?>[] paramTypes) |
Invoke a named method whose parameter type matches the object type.
|
static void |
invokeSetter(java.lang.Object object,
java.lang.String setterName,
java.lang.Object arg) |
Sets the value of a bean property to an Object.
|
static void |
invokeSetter(java.lang.Object object,
java.lang.String setterName,
java.lang.Object[] args) |
Sets the value of a bean property to an Object.
|
static java.lang.Object |
invokeStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName) |
Invoke a named static method that has no parameters.
|
static java.lang.Object |
invokeStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName,
java.lang.Object arg) |
Invoke a named static method whose parameter type matches the object type.
|
static java.lang.Object |
invokeStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName,
java.lang.Object[] args) |
Invoke a named static method whose parameter type matches the object type.
|
static java.lang.Object |
invokeStaticMethod(java.lang.Class<?> objectClass,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<?>[] paramTypes) |
Invoke a named static method whose parameter type matches the object type.
|
static void |
setCacheEnabled(boolean cacheEnabling) |
Set whether methods should be cached for greater performance or not,
default is
true . |
public static final java.lang.Class<?>[] EMPTY_CLASS_PARAMETERS
public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
public static void invokeSetter(java.lang.Object object, java.lang.String setterName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
object
- the bean to changesetterName
- the property name or setter method namearg
- use this argumentjava.lang.NoSuchMethodException
- the no such method exceptionjava.lang.IllegalAccessException
- the illegal access exceptionjava.lang.reflect.InvocationTargetException
- the invocation target exceptionpublic static void invokeSetter(java.lang.Object object, java.lang.String setterName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
object
- the bean to changesetterName
- the property name or setter method nameargs
- use this argumentsjava.lang.NoSuchMethodException
- the no such method exceptionjava.lang.IllegalAccessException
- the illegal access exceptionjava.lang.reflect.InvocationTargetException
- the invocation target exceptionpublic static java.lang.Object invokeGetter(java.lang.Object object, java.lang.String getterName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
object
- the beangetterName
- the property name or getter method namejava.lang.NoSuchMethodException
- the no such method exceptionjava.lang.IllegalAccessException
- the illegal access exceptionjava.lang.reflect.InvocationTargetException
- the invocation target exceptionpublic static java.lang.Object invokeGetter(java.lang.Object object, java.lang.String getterName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
object
- the beangetterName
- the property name or getter method namearg
- use this argumentjava.lang.NoSuchMethodException
- the no such method exceptionjava.lang.IllegalAccessException
- the illegal access exceptionjava.lang.reflect.InvocationTargetException
- the invocation target exceptionpublic static java.lang.Object invokeGetter(java.lang.Object object, java.lang.String getterName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
object
- the beangetterName
- the property name or getter method nameargs
- use this argumentsjava.lang.NoSuchMethodException
- the no such method exceptionjava.lang.IllegalAccessException
- the illegal access exceptionjava.lang.reflect.InvocationTargetException
- the invocation target exceptionpublic static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Invoke a named method whose parameter type matches the object type.
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 accessiblepublic static java.lang.Object invokeMethod(java.lang.Object object, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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 a boolean
primitive.
This is a convenient wrapper for
invokeMethod(Object object,String methodName,Object[] args)
.
object
- invoke method on this objectmethodName
- get method with this namearg
- use this argumentjava.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
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 a boolean
primitive.
This is a convenient wrapper for
invokeMethod(Object object,String methodName,Object[] args,Class[] paramTypes)
.
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, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] paramTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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 a boolean
primitive.
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 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 arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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)
.
object
- invoke method on this objectmethodName
- get method with this namearg
- use this argumentjava.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
Invoke a method whose parameter types match exactly the object types.
This uses reflection to invoke the method obtained from a call to
getAccessibleMethod()
.
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<?>[] paramTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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()
.
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 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<?> objectClass, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] paramTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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[])
.
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 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<?> objectClass, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
objectClass
- invoke static method on this classmethodName
- 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 invokeStaticMethod(java.lang.Class<?> objectClass, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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 a boolean
primitive.
This is a convenient wrapper for
invokeStaticMethod(Class objectClass,String methodName,Object[] args)
.
objectClass
- invoke static method on this classmethodName
- get method with this namearg
- use this argumentjava.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<?> objectClass, java.lang.String methodName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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 a boolean
primitive.
This is a convenient wrapper for
invokeStaticMethod(Class objectClass,String methodName,Object[] args,Class[] paramTypes)
.
objectClass
- 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<?> objectClass, java.lang.String methodName, java.lang.Object[] args, java.lang.Class<?>[] paramTypes) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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 a boolean
primitive.
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 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<?> objectClass, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
objectClass
- invoke static method on this classmethodName
- 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 invokeExactStaticMethod(java.lang.Class<?> objectClass, java.lang.String methodName, java.lang.Object arg) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
This is a convenient wrapper for
invokeExactStaticMethod(Class objectClass,String methodName,Object[] args)
.
objectClass
- invoke static method on this classmethodName
- get method with this namearg
- use this argumentjava.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<?> objectClass, java.lang.String methodName, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.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[])
.
objectClass
- 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<?> clazz, java.lang.String methodName)
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 a Class
array for you.
clazz
- get method from this classmethodName
- get method with this namepublic static java.lang.reflect.Method getAccessibleMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?> paramType)
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 a Class
array for you.
clazz
- get method from this classmethodName
- get method with this nameparamType
- taking this type of parameterpublic static java.lang.reflect.Method getAccessibleMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 for
getAccessibleMethod(Method method)
.
clazz
- get method from this classmethodName
- get method with this nameparamTypes
- with these parameters typespublic static java.lang.reflect.Method getAccessibleMethod(java.lang.reflect.Method method)
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
.
method
- the method that we wish to callpublic static java.lang.reflect.Method getAccessibleMethod(java.lang.Class<?> clazz, java.lang.reflect.Method method)
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
.
clazz
- The class of the objectmethod
- The method that we wish to callpublic static java.lang.reflect.Method getMatchingAccessibleMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object[] args, java.lang.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 primitive boolean
parameter.
clazz
- find method in this classmethodName
- find method with this nameargs
- find method with given argumentsparamTypes
- find method with compatible parameterspublic static java.lang.reflect.Method getMatchingAccessibleMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 primitive boolean
parameter.
clazz
- find method in this classmethodName
- find method with this nameparamTypes
- find method with compatible parameterspublic static void setCacheEnabled(boolean cacheEnabling)
true
.cacheEnabling
- true
if methods should be
cached for greater performance, otherwise false
public static int clearCache()
Copyright © 2008–2018 The Aspectran Project. All rights reserved.