org.crosswire.common.util
Class ReflectionUtil

java.lang.Object
  extended by org.crosswire.common.util.ReflectionUtil

public final class ReflectionUtil
extends Object

Various utilities for calling constructors and methods via introspection.

Author:
Joe Walker, DM Smith
See Also:
The GNU Lesser General Public License for details.

Constructor Summary
private ReflectionUtil()
          Prevent instantiation
 
Method Summary
static
<T> T
construct(Class<T> clazz, Object... params)
          Build an object using the supplied parameters.
static
<T> T
construct(String className)
          Build an object using its default constructor.
static
<T> T
construct(String className, Object... params)
          Build an object using the supplied parameters.
static
<T> T
construct(String className, Object[] params, Class<?>[] paramTypes)
          Build an object using the supplied parameters.
private static Class<?>[] describeParameters(Object... params)
          Construct a parallel array of class objects for each element in params.
private static
<T> Method
getMethod(Class<T> clazz, String methodName, Class<?>[] calledTypes)
           
static
<T> Object
invoke(Class<T> clazz, Object obj, String methodName, Object... params)
          Call a method on an object, or statically, with the supplied parameters.
static
<T> Object
invoke(Class<T> clazz, Object obj, String methodName, Object[] params, Class<?>[] paramTypes)
          Call a method on an object, or statically, with the supplied parameters.
static Object invoke(Object base, String methodName, Object... params)
          Call a method on a class given a sting
static Object invoke(String call, Object... params)
          Call a static method on a class given a string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtil

private ReflectionUtil()
Prevent instantiation

Method Detail

construct

public static <T> T construct(String className)
                   throws ClassNotFoundException,
                          InstantiationException,
                          IllegalAccessException
Build an object using its default constructor. Note: a constructor that takes a boolean needs a type of boolean.class, but a parameter of type Boolean. Likewise for other primitives. If this is needed, do not call this method.

Type Parameters:
T - the type of the object to construct
Parameters:
className - the full class name of the object
Returns:
the constructed object
Throws:
ClassNotFoundException - if the class is not found
InstantiationException - if this data represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
IllegalAccessException - if the class or its nullary constructor is not accessible.

construct

public static <T> T construct(String className,
                              Object... params)
                   throws ClassNotFoundException,
                          NoSuchMethodException,
                          IllegalAccessException,
                          InvocationTargetException,
                          InstantiationException
Build an object using the supplied parameters. Note: a constructor that takes a boolean needs a type of boolean.class, but a parameter of type Boolean. Likewise for other primitives.

Type Parameters:
T - the type of the object to construct
Parameters:
className - the full class name of the object
params - the constructor's arguments
Returns:
the built object
Throws:
ClassNotFoundException - if the class is not found
NoSuchMethodException - the method does not exist
InstantiationException - if this data represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
IllegalAccessException - if the class or its nullary constructor is not accessible.
InvocationTargetException - if the underlying constructor throws an exception.
InstantiationException - if the class that declares the underlying constructor represents an abstract class.

construct

public static <T> T construct(Class<T> clazz,
                              Object... params)
                   throws NoSuchMethodException,
                          InstantiationException,
                          IllegalAccessException,
                          InvocationTargetException
Build an object using the supplied parameters. Note: a constructor that takes a boolean needs a type of boolean.class, but a parameter of type Boolean. Likewise for other primitives.

Type Parameters:
T - the type of the object to construct
Parameters:
clazz - the class of the object
params - the constructor's arguments
Returns:
the built object
Throws:
NoSuchMethodException - the method does not exist
InstantiationException - if the class that declares the underlying constructor represents an abstract class.
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
InvocationTargetException - if the underlying constructor throws an exception.

construct

public static <T> T construct(String className,
                              Object[] params,
                              Class<?>[] paramTypes)
                   throws ClassNotFoundException,
                          NoSuchMethodException,
                          IllegalAccessException,
                          InvocationTargetException,
                          InstantiationException
Build an object using the supplied parameters.

Type Parameters:
T - the type of the object to construct
Parameters:
className - the full class name of the object
params - the constructor's arguments
paramTypes - the types of the parameters
Returns:
the built object
Throws:
ClassNotFoundException - if the class is not found
NoSuchMethodException - the method does not exist
InstantiationException - if the class that declares the underlying constructor represents an abstract class.
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
InvocationTargetException - if the underlying constructor throws an exception.

invoke

public static Object invoke(Object base,
                            String methodName,
                            Object... params)
                     throws NoSuchMethodException,
                            IllegalAccessException,
                            InvocationTargetException
Call a method on a class given a sting

Parameters:
base - The object to invoke a method on
methodName - The text of the invocation, for example "getName"
params - For example new Object[] { ...}
Returns:
whatever the method returs
Throws:
NoSuchMethodException - the method does not exist
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
InvocationTargetException - if the underlying constructor throws an exception.

invoke

public static Object invoke(String call,
                            Object... params)
                     throws ClassNotFoundException,
                            NoSuchMethodException,
                            IllegalAccessException,
                            InvocationTargetException
Call a static method on a class given a string

Parameters:
call - The text of the invocation, for example "java.lang.String.getName"
params - For example new Object[] { ...}
Returns:
whatever the method returs
Throws:
ClassNotFoundException - if the class is not found
NoSuchMethodException - the method does not exist
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
InvocationTargetException - if the underlying constructor throws an exception.

invoke

public static <T> Object invoke(Class<T> clazz,
                                Object obj,
                                String methodName,
                                Object... params)
                     throws NoSuchMethodException,
                            IllegalAccessException,
                            InvocationTargetException
Call a method on an object, or statically, with the supplied parameters. Note: a method that takes a boolean needs a type of boolean.class, but a parameter of type Boolean. Likewise for other primitives. If this is needed, do not call this method.

Type Parameters:
T - the type of the object to construct
Parameters:
clazz - the class of the object
obj - the object having the method, or null to call a static method
methodName - the method to be called
params - the parameters
Returns:
whatever the method returns
Throws:
NoSuchMethodException - the method does not exist
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
InvocationTargetException - if the underlying constructor throws an exception.

invoke

public static <T> Object invoke(Class<T> clazz,
                                Object obj,
                                String methodName,
                                Object[] params,
                                Class<?>[] paramTypes)
                     throws NoSuchMethodException,
                            IllegalAccessException,
                            InvocationTargetException
Call a method on an object, or statically, with the supplied parameters. Note: a method that takes a boolean needs a type of boolean.class, but a parameter of type Boolean. Likewise for other primitives.

Type Parameters:
T - the type of the object to construct
Parameters:
clazz - the class of the object
obj - the object having the method, or null to call a static method
methodName - the method to be called
params - the parameters
paramTypes - the types of each of the parameters
Returns:
whatever the method returns
Throws:
NoSuchMethodException - the method does not exist
IllegalAccessException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.
InvocationTargetException - if the underlying constructor throws an exception.

describeParameters

private static Class<?>[] describeParameters(Object... params)
Construct a parallel array of class objects for each element in params.

Parameters:
params - the types to describe
Returns:
the parallel array of class objects

getMethod

private static <T> Method getMethod(Class<T> clazz,
                                    String methodName,
                                    Class<?>[] calledTypes)
                         throws NoSuchMethodException
Throws:
NoSuchMethodException

Copyright ยจ 2003-2015