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 [joe at eireneh dot com], DM Smith [dmsmith555 at yahoo dot com]
See Also:
for license details.
The copyright to this program is held by it's authors.

Constructor Summary
private ReflectionUtil()
          Prevent instantiation
 
Method Summary
static Object construct(String className)
          Build an object using its default constructor.
static Object construct(String className, Object[] params)
          Build an object using the supplied parameters.
static Object 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 Method getMethod(Class clazz, String methodName, Class[] calledTypes)
           
static Object invoke(Class clazz, Object obj, String methodName, Object[] params)
          Call a method on an object, or statically, with the supplied parameters.
static Object invoke(Class 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 Object 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.

Parameters:
className - the full class name of the object
Returns:
the constructed object
Throws:
ClassNotFoundException
IllegalAccessException
InstantiationException

construct

public static Object 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.

Parameters:
className - the full class name of the object
params - the constructor's arguments
Returns:
the built object
Throws:
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InvocationTargetException
InstantiationException

construct

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

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
NoSuchMethodException
IllegalAccessException
InvocationTargetException
InstantiationException

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[] { ...}
Throws:
NoSuchMethodException
IllegalAccessException
InvocationTargetException

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[] { ...}
Throws:
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InvocationTargetException

invoke

public static Object invoke(Class 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.

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
IllegalAccessException
InvocationTargetException

invoke

public static Object invoke(Class 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.

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
IllegalAccessException
InvocationTargetException

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 Method getMethod(Class clazz,
                                String methodName,
                                Class[] calledTypes)
                         throws NoSuchMethodException
Throws:
NoSuchMethodException

Copyright ยจ 2003-2007