org.apache.qpid.test.utils
Class ReflectionUtils

java.lang.Object
  extended by org.apache.qpid.test.utils.ReflectionUtils

public class ReflectionUtils
extends Object

Provides helper methods for operating on classes and methods using reflection. Reflection methods tend to return a lot of checked exception so writing code to use them can be tedious and harder to read, especially when such errors are not expected to occur. This class always works with ReflectionUtilsException, which is a runtime exception, to wrap the checked exceptions raised by the standard Java reflection methods. Code using it does not normally expect these errors to occur, usually does not have a recovery mechanism for them when they do, but is cleaner, quicker to write and easier to read in the majority of cases.

CRC Card
Responsibilities Collaborations
Look up Classes by name.
Instantiate Classes by no-arg constructor.


Constructor Summary
ReflectionUtils()
           
 
Method Summary
static Object callMethod(Object o, String method, Object[] params)
          Calls a named method on an object with a specified set of parameters.
static Object callMethodOverridingIllegalAccess(Object o, String method, Object[] params, Class[] paramClasses)
          Calls a named method on an object with a specified set of parameters, any Java access modifier are overridden.
static Class<?> forName(String className)
          Gets the Class object for a named class.
static
<T> Constructor<T>
getConstructor(Class<T> cls, Class[] args)
          Gets the constructor of a class that takes the specified set of arguments if any matches.
static
<T> T
newInstance(Class<? extends T> cls)
          Creates an instance of a Class, instantiated through its no-args constructor.
static
<T> T
newInstance(Constructor<T> constructor, Object[] args)
          Calls a constuctor witht the specified arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

forName

public static Class<?> forName(String className)
Gets the Class object for a named class.

Parameters:
className - The class to get the Class object for.
Returns:
The Class object for the named class.

newInstance

public static <T> T newInstance(Class<? extends T> cls)
Creates an instance of a Class, instantiated through its no-args constructor.

Type Parameters:
T - The Class type.
Parameters:
cls - The Class to instantiate.
Returns:
An instance of the class.

callMethodOverridingIllegalAccess

public static Object callMethodOverridingIllegalAccess(Object o,
                                                       String method,
                                                       Object[] params,
                                                       Class[] paramClasses)
Calls a named method on an object with a specified set of parameters, any Java access modifier are overridden.

Parameters:
o - The object to call.
method - The method name to call.
params - The parameters to pass.
paramClasses - The argument types.
Returns:
The return value from the method call.

callMethod

public static Object callMethod(Object o,
                                String method,
                                Object[] params)
Calls a named method on an object with a specified set of parameters.

Parameters:
o - The object to call.
method - The method name to call.
params - The parameters to pass.
Returns:
The return value from the method call.

newInstance

public static <T> T newInstance(Constructor<T> constructor,
                                Object[] args)
Calls a constuctor witht the specified arguments.

Type Parameters:
T - The Class type.
Parameters:
constructor - The constructor.
args - The arguments.
Returns:
An instance of the class that the constructor is for.

getConstructor

public static <T> Constructor<T> getConstructor(Class<T> cls,
                                                Class[] args)
Gets the constructor of a class that takes the specified set of arguments if any matches. If no matching constructor is found then a runtime exception is raised.

Type Parameters:
T - The class type.
Parameters:
cls - The class to get a constructor from.
args - The arguments to match.
Returns:
The constructor.


Licensed to the Apache Software Foundation