org.picocontainer.gems.util
Class DelegateMethod<TARGET_TYPE,RETURN_TYPE>

java.lang.Object
  extended by org.picocontainer.gems.util.DelegateMethod<TARGET_TYPE,RETURN_TYPE>

public class DelegateMethod<TARGET_TYPE,RETURN_TYPE>
extends Object

The DelegateMethod class has been designed in the hope of providing easier access to methods invoked via reflection. Sample:

 //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.

Author:
Michael Rimov

Constructor Summary
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.
 
Method Summary
 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>
RETURN_TYPE
invoke(V target)
          Invokes the method specified in the constructor against the target specified.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DelegateMethod

public DelegateMethod(Class<TARGET_TYPE> type,
                      String methodName,
                      Object... parameters)
               throws NoSuchMethodRuntimeException
Constructs a delegate method object that will invoke method methodName on class type with the parameters specified. The object automatically searches for a suitable object to be invoked.

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.

Parameters:
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.
Throws:
NoSuchMethodRuntimeException - if the method is not found or parameters that match cannot be found.

DelegateMethod

public DelegateMethod(Class<?> type,
                      String methodName,
                      Class<?>[] paramTypes,
                      Object... parameters)
               throws NoSuchMethodRuntimeException
Constructs a DelegateMethod object with very specific argument types.

Parameters:
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.
Throws:
NoSuchMethodRuntimeException - if the method is not found.

DelegateMethod

public DelegateMethod(Method targetMethod,
                      Object... parameters)
Constructs a method delegate with an explicit Method object.

Parameters:
targetMethod -
parameters -
Method Detail

invoke

public RETURN_TYPE invoke()
                   throws IllegalArgumentException,
                          IllegalAccessRuntimeException,
                          InvocationTargetRuntimeException
Used for invoking static methods on the type passed into the constructor.

Returns:
the result of the invocation. May be null if the return type is void.
Throws:
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.

invoke

public <V extends TARGET_TYPE> RETURN_TYPE invoke(V target)
                   throws IllegalAccessRuntimeException,
                          InvocationTargetRuntimeException
Invokes the method specified in the constructor against the target specified.

Type Parameters:
V - a subclass of the type specified by the object declaration. This allows Map delegates to operate on HashMaps etc.
Parameters:
target - the target object instance to be operated upon. Unless invoking a static method, this should not be null.
Returns:
the result of the invocation. May be null if the return type is void.
Throws:
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.

toString

public String toString()

Overrides:
toString in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

getReturnType

public Class<?> getReturnType()
Retrieves the expected return type of the delegate method.

Returns:


Copyright © 2003-2010 Codehaus. All Rights Reserved.