public class DelegateMethod<TARGET_TYPE,RETURN_TYPE> extends Object
//Sample Map
HashMap<String, String> testMap = new HashMap<String, String>();
testMap.put("a", "A");
//Create delegate method that calls the 'clear' method for HashMap.
DelegateMethod<Map, Void> method = new DelegateMethod<Map, Void>(Map.class,
"clear");
//Invokes clear() on the HashMap.
method.invoke(testMap);
Good uses of this object are for lazy invocation of a method and integrating reflection with a vistor pattern.
| Constructor and Description |
|---|
DelegateMethod(Class<?> type,
String methodName,
Class<?>[] paramTypes,
Object... parameters)
Constructs a DelegateMethod object with very specific argument types.
|
DelegateMethod(Class<TARGET_TYPE> type,
String methodName,
Object... parameters)
Constructs a delegate method object that will invoke method
methodName on class type with the parameters
specified.
|
DelegateMethod(Method targetMethod,
Object... parameters)
Constructs a method delegate with an explicit Method object.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj) |
Class<?> |
getReturnType()
Retrieves the expected return type of the delegate method.
|
int |
hashCode() |
RETURN_TYPE |
invoke()
Used for invoking static methods on the type passed into the constructor.
|
<V extends TARGET_TYPE> |
invoke(V target)
Invokes the method specified in the constructor against the target
specified.
|
String |
toString() |
public DelegateMethod(Class<TARGET_TYPE> type, String methodName, Object... parameters) throws NoSuchMethodRuntimeException
Note that this version simply grabs the first method that fits the parameter criteria with the specific name. You may need to be careful if use extensive overloading.
To specify the exact types in the method.
type - the class of the object that should be invoked.methodName - the name of the method that will be invoked.parameters - the parameters to be used.NoSuchMethodRuntimeException - if the method is not found or parameters that match cannot be found.public DelegateMethod(Class<?> type, String methodName, Class<?>[] paramTypes, Object... parameters) throws NoSuchMethodRuntimeException
type - the type of the class to be examined for reflection.methodName - the name of the method to be invoked.paramTypes - specific parameter types for the method to be found.parameters - the parameters for method invocation.NoSuchMethodRuntimeException - if the method is not found.public DelegateMethod(Method targetMethod, Object... parameters)
targetMethod - parameters - public RETURN_TYPE invoke() throws IllegalArgumentException, IllegalAccessRuntimeException, InvocationTargetRuntimeException
IllegalArgumentException - if the method being invoked is not static.IllegalAccessRuntimeException - if the method being invoked is not public.InvocationTargetRuntimeException - if an exception is thrown within the method being invoked.public <V extends TARGET_TYPE> RETURN_TYPE invoke(V target) throws IllegalAccessRuntimeException, InvocationTargetRuntimeException
V - a subclass of the type specified by the object declaration.
This allows Map delegates to operate on HashMaps etc.target - the target object instance to be operated upon. Unless
invoking a static method, this should not be null.IllegalArgumentException - if the method being invoked is not static and parameter
target null.IllegalAccessRuntimeException - if the method being invoked is not public.InvocationTargetRuntimeException - if an exception is thrown within the method being invoked.public Class<?> getReturnType()
Copyright © 2003-2014 Codehaus. All Rights Reserved.