|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.lang3.reflect.MethodUtils
public class MethodUtils
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.
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 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 the method may fail.
Constructor Summary | |
---|---|
MethodUtils()
MethodUtils instances should NOT be constructed in standard programming. |
Method Summary | |
---|---|
static Method |
getAccessibleMethod(Class<?> cls,
String methodName,
Class<?>... parameterTypes)
Returns an accessible method (that is, one that can be invoked via reflection) with given name and parameters. |
static Method |
getAccessibleMethod(Method method)
Returns an accessible method (that is, one that can be invoked via reflection) that implements the specified Method. |
static Method |
getMatchingAccessibleMethod(Class<?> cls,
String methodName,
Class<?>... parameterTypes)
Finds an accessible method that matches the given name and has compatible parameters. |
static Object |
invokeExactMethod(Object object,
String methodName,
Object... args)
Invokes a method whose parameter types match exactly the object types. |
static Object |
invokeExactMethod(Object object,
String methodName,
Object[] args,
Class<?>[] parameterTypes)
Invokes a method whose parameter types match exactly the parameter types given. |
static Object |
invokeExactStaticMethod(Class<?> cls,
String methodName,
Object... args)
Invokes a static method whose parameter types match exactly the object types. |
static Object |
invokeExactStaticMethod(Class<?> cls,
String methodName,
Object[] args,
Class<?>[] parameterTypes)
Invokes a static method whose parameter types match exactly the parameter types given. |
static Object |
invokeMethod(Object object,
String methodName,
Object... args)
Invokes a named method whose parameter type matches the object type. |
static Object |
invokeMethod(Object object,
String methodName,
Object[] args,
Class<?>[] parameterTypes)
Invokes a named method whose parameter type matches the object type. |
static Object |
invokeStaticMethod(Class<?> cls,
String methodName,
Object... args)
Invokes a named static method whose parameter type matches the object type. |
static Object |
invokeStaticMethod(Class<?> cls,
String methodName,
Object[] args,
Class<?>[] parameterTypes)
Invokes a named static method whose parameter type matches the object type. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MethodUtils()
MethodUtils instances should NOT be constructed in standard programming.
Instead, the class should be used as
MethodUtils.getAccessibleMethod(method)
.
This constructor is public to permit tools that require a JavaBean instance to operate.
Method Detail |
---|
public static Object invokeMethod(Object object, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, 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 a boolean
primitive.
This is a convenient wrapper for
invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)
.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
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 reflectionpublic static Object invokeMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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 a boolean
primitive.
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
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 reflectionpublic static Object invokeExactMethod(Object object, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, 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()
.
object
- invoke method on this objectmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
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 reflectionpublic static Object invokeExactMethod(Object object, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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()
.
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
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 reflectionpublic static Object invokeExactStaticMethod(Class<?> cls, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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[])
.
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty array
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 reflectionpublic static Object invokeStaticMethod(Class<?> cls, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, 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 a boolean
primitive.
This is a convenient wrapper for
invokeStaticMethod(Class objectClass,String methodName,Object [] args,Class[] parameterTypes)
.
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
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 reflectionpublic static Object invokeStaticMethod(Class<?> cls, String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, 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 a boolean
primitive.
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty arrayparameterTypes
- match these parameters - treat null as empty array
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 reflectionpublic static Object invokeExactStaticMethod(Class<?> cls, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, 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[])
.
cls
- invoke static method on this classmethodName
- get method with this nameargs
- use these arguments - treat null as empty array
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 reflectionpublic static Method getAccessibleMethod(Class<?> cls, String methodName, 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 convenient wrapper for
getAccessibleMethod(Method method)
.
cls
- get method from this classmethodName
- get method with this nameparameterTypes
- with these parameters types
public static Method getAccessibleMethod(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
.
method
- The method that we wish to call
public static Method getMatchingAccessibleMethod(Class<?> cls, String methodName, 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 primitive boolean
parameter.
cls
- find method in this classmethodName
- find method with this nameparameterTypes
- find method with most compatible parameters
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |