-
public class MethodUtilsUtility 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 LimitationsAccessing Public Methods In A Default Access SuperclassThere 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
IllegalAccessExceptionis thrown if the method is invoked.MethodUtilscontains a workaround for this situation. It will attempt to callsetAccessibleon 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
Constructors Constructor Description MethodUtils()MethodUtils instances should NOT be constructed in standard programming.
-
Method Summary
Modifier and Type Method Description static ObjectinvokeMethod(Object object, String methodName, Array<Object> args)Invokes a named method whose parameter type matches the object type. static ObjectinvokeMethod(Object object, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)Invokes a named method whose parameter type matches the object type. static ObjectinvokeExactMethod(Object object, String methodName, Array<Object> args)Invokes a method whose parameter types match exactly the objecttypes. static ObjectinvokeExactMethod(Object object, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)Invokes a method whose parameter types match exactly the parametertypes given. static ObjectinvokeExactStaticMethod(Class<out Object> cls, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)Invokes a static method whose parameter types match exactly the parametertypes given. static ObjectinvokeStaticMethod(Class<out Object> cls, String methodName, Array<Object> args)Invokes a named static method whose parameter type matches the object type. static ObjectinvokeStaticMethod(Class<out Object> cls, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)Invokes a named static method whose parameter type matches the object type. static ObjectinvokeExactStaticMethod(Class<out Object> cls, String methodName, Array<Object> args)Invokes a static method whose parameter types match exactly the objecttypes. static MethodgetAccessibleMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> parameterTypes)Returns an accessible method (that is, one that can be invoked viareflection) with given name and parameters. static MethodgetAccessibleMethod(Method method)Returns an accessible method (that is, one that can be invoked viareflection) that implements the specified Method. static MethodgetMatchingAccessibleMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> 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. -
-
Method Detail
-
invokeMethod
static Object invokeMethod(Object object, String methodName, Array<Object> args)
Invokes a named method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod.
This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanobjectwould match abooleanprimitive.This is a convenient wrapper for invokeMethod.
- Parameters:
object- invoke method on this objectmethodName- get method with this nameargs- use these arguments - treat null as empty array
-
invokeMethod
static Object invokeMethod(Object object, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)
Invokes a named method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod.
This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanobjectwould match abooleanprimitive.- Parameters:
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
-
invokeExactMethod
static Object invokeExactMethod(Object object, String methodName, Array<Object> args)
Invokes a method whose parameter types match exactly the objecttypes.
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
-
invokeExactMethod
static Object invokeExactMethod(Object object, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)
Invokes a method whose parameter types match exactly the parametertypes 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 arrayparameterTypes- match these parameters - treat null as empty array
-
invokeExactStaticMethod
static Object invokeExactStaticMethod(Class<out Object> cls, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)
Invokes a static method whose parameter types match exactly the parametertypes given.
This uses reflection to invoke the method obtained from a call to getAccessibleMethod.
- Parameters:
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
-
invokeStaticMethod
static Object invokeStaticMethod(Class<out Object> cls, String methodName, Array<Object> args)
Invokes a named static method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod.
This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanclasswould match abooleanprimitive.This is a convenient wrapper for invokeStaticMethod.
- Parameters:
cls- invoke static method on this classmethodName- get method with this nameargs- use these arguments - treat null as empty array
-
invokeStaticMethod
static Object invokeStaticMethod(Class<out Object> cls, String methodName, Array<Object> args, Array<Class<out Object>> parameterTypes)
Invokes a named static method whose parameter type matches the object type.
This method delegates the method search to getMatchingAccessibleMethod.
This method supports calls to methods taking primitive parameters via passing in wrapping classes. So, for example, a
Booleanclasswould match abooleanprimitive.- Parameters:
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
-
invokeExactStaticMethod
static Object invokeExactStaticMethod(Class<out Object> cls, String methodName, Array<Object> args)
Invokes a static method whose parameter types match exactly the objecttypes.
This uses reflection to invoke the method obtained from a call to getAccessibleMethod.
- Parameters:
cls- invoke static method on this classmethodName- get method with this nameargs- use these arguments - treat null as empty array
-
getAccessibleMethod
static Method getAccessibleMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> parameterTypes)
Returns an accessible method (that is, one that can be invoked viareflection) with given name and parameters. If no such methodcan be found, return
null.This is just a convenient wrapper for getAccessibleMethod.- Parameters:
cls- get method from this classmethodName- get method with this nameparameterTypes- with these parameters types
-
getAccessibleMethod
static Method getAccessibleMethod(Method method)
Returns an accessible method (that is, one that can be invoked viareflection) that implements the specified Method. If no such methodcan be found, return
null.- Parameters:
method- The method that we wish to call
-
getMatchingAccessibleMethod
static Method getMatchingAccessibleMethod(Class<out Object> cls, String methodName, Array<Class<out Object>> 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 can match primitive parameter by passing in wrapper classes.For example, a
Booleanwill match a primitivebooleanparameter.- Parameters:
cls- find method in this classmethodName- find method with this nameparameterTypes- find method with most compatible parameters
-
-
-
-