org.inria.ns.reflex.util
Class Invoker

java.lang.Object
  extended by org.inria.ns.reflex.util.Invoker

public class Invoker
extends Object

A class that allow to access members with restricted access.

Author:
Philippe Poulard

Method Summary
static Object call(Object target, String method)
          Call a method without arguments.
static Object call(Object target, String method, Object[] args, Class[] paramTypes)
          Call a method.
static Object get(Object target, String field)
          Get the value of a field.
static Field getField(Class clazz, String field)
          Get a field of a class or one of its parent class.
static Method getMethod(Class clazz, String method)
          Get a method of a class or one of its parent class.
static Method getMethod(Class clazz, String method, Class[] paramTypes)
          Get a method of a class or one of its parent class.
static void set(Object target, String field, Object value)
          Set a value to a field of an object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getField

public static Field getField(Class clazz,
                             String field)
                      throws NoSuchFieldException
Get a field of a class or one of its parent class.

Parameters:
clazz - The class.
field - The name of the field.
Returns:
The non-null field of the class.
Throws:
NoSuchFieldException - If the field doesn't exist in the class nor in the ancestor classes.

set

public static void set(Object target,
                       String field,
                       Object value)
                throws NoSuchFieldException,
                       IllegalArgumentException,
                       IllegalAccessException
Set a value to a field of an object.

The operation succeed even if the field is defined by a superclass of the target object.

Parameters:
target - The target object.
field - The name of the field.
value - The value to set.
Throws:
NoSuchFieldException - When the field to set is missing in the target object.
IllegalArgumentException - When the value to set is not of the type expected.
IllegalAccessException - When the field is not visible and a security manager doesn't allow to change its visibility.

get

public static Object get(Object target,
                         String field)
                  throws NoSuchFieldException,
                         IllegalAccessException
Get the value of a field.

The operation succeed even if the field is defined by a superclass of the target object.

Parameters:
target - The target object.
field - The name of the field.
Returns:
The value of the field.
Throws:
NoSuchFieldException - When the field to get is missing in the target object.
IllegalAccessException - When the field is not visible and a security manager doesn't allow to change its visibility.

getMethod

public static Method getMethod(Class clazz,
                               String method,
                               Class[] paramTypes)
                        throws NoSuchMethodException
Get a method of a class or one of its parent class.

Parameters:
clazz - The class.
method - The name of the method.
paramTypes - The types of the parameters of the method, can be null.
Returns:
The non-null method of the class.
Throws:
NoSuchMethodException - If the method doesn't exist in the class nor in the ancestor classes.

getMethod

public static Method getMethod(Class clazz,
                               String method)
                        throws NoSuchMethodException
Get a method of a class or one of its parent class.

Parameters:
clazz - The class.
method - The name of the method.
Returns:
The non-null method of the class that has the name given and accept no parameter.
Throws:
NoSuchMethodException - If the method doesn't exist in the class nor in the ancestor classes.

call

public static Object call(Object target,
                          String method)
                   throws NoSuchMethodException,
                          IllegalAccessException,
                          InvocationTargetException
Call a method without arguments.

The call succeed even if the method is defined by a superclass of the target object.

Parameters:
target - The target object.
method - The name of the method to call.
Returns:
The result of the call.
Throws:
NoSuchMethodException - When the method to call is missing in the target object.
IllegalAccessException - When the method is not visible and a security manager doesn't allow to change its visibility.
InvocationTargetException - If the underlying method throws an exception.

call

public static Object call(Object target,
                          String method,
                          Object[] args,
                          Class[] paramTypes)
                   throws NoSuchMethodException,
                          IllegalArgumentException,
                          IllegalAccessException,
                          InvocationTargetException
Call a method.

The call succeed even if the method is defined by a superclass of the target object.

Parameters:
target - The target object.
method - The name of the method to call.
args - An array of arguments.
paramTypes - An array of types.
Returns:
The result of the call.
Throws:
NoSuchMethodException - When the method to call is missing in the target object.
IllegalArgumentException - When one of the arguments is not of the type expected.
IllegalAccessException - When the method is not visible and a security manager doesn't allow to change the visibility.
InvocationTargetException - If the underlying method throws an exception.