Class Callback

java.lang.Object
  extended byCallback

public class Callback
extends java.lang.Object

   
 Generic class to facilate use of callbacks
 Also provides a static helper function to get an object of Method that can be passed to the constructor of this class
 Example:
         To get a Method object for the method public foo(String str) in class Test:
         String[] paramTypes = {"java.lang.String"};
         Method method = Callback.getMethod("foo", this, paramTypes);
         Callback cb = new Callback(method, this, "fooTest");

 The above code snippet assumes that it is written inside class Test, hence the use of this.
 The method must have public visibility.
 


Constructor Summary
Callback(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object[] params)
          Initializes member variables
 
Method Summary
static java.lang.reflect.Method getMethod(java.lang.String methodName, java.lang.Object obj, java.lang.String[] parameterTypes)
          Helper function to get a Method object which is needed to pass to the constructor of this class
 void invoke()
          Invokes the callback
 void setParams(java.lang.Object[] params)
          Sets the params to be passed to the method when it is invoked
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Callback

public Callback(java.lang.reflect.Method method,
                java.lang.Object obj,
                java.lang.Object[] params)
Initializes member variables

Parameters:
method - The method to be invoked
obj - The object on which the method is to be invoked
params - An array of objects to be passed to the method as parameters when it is invoked. Can be null if no parameters are to be passed
Method Detail

setParams

public void setParams(java.lang.Object[] params)
Sets the params to be passed to the method when it is invoked

Parameters:
params - The params to be passed to the method when it is invoked

invoke

public void invoke()
            throws java.lang.IllegalAccessException,
                   java.lang.reflect.InvocationTargetException
Invokes the callback

Throws:
java.lang.IllegalAccessException - Thrown by invoke method in class Method
java.lang.reflect.InvocationTargetException - Thrown by invoke method in class Method, if the underlying method throws an exception

getMethod

public static java.lang.reflect.Method getMethod(java.lang.String methodName,
                                                 java.lang.Object obj,
                                                 java.lang.String[] parameterTypes)
                                          throws java.lang.ClassNotFoundException,
                                                 java.lang.NoSuchMethodException,
                                                 java.lang.SecurityException
Helper function to get a Method object which is needed to pass to the constructor of this class

Parameters:
methodName - The name of the method that we want a Method object for
obj - The object that owns the method
parameterTypes - Array of strings of parameter type names. Can be null if there are no parameter types
Returns:
A Method object which can be passed to the constructor of this class
Throws:
java.lang.ClassNotFoundException - This exception is thrown by Class.forName if the given class name does not exist
java.lang.NoSuchMethodException - Thrown by Class.getMethod if a matching method is not found
java.lang.SecurityException - Thrown by Class.getMethod if access to the information is denied