|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.qpid.test.utils.ReflectionUtils
public class ReflectionUtils
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.
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
|
getConstructor(Class<T> cls,
Class[] args)
Gets the constructor of a class that takes the specified set of arguments if any matches. |
|
static
|
newInstance(Class<? extends T> cls)
Creates an instance of a Class, instantiated through its no-args constructor. |
|
static
|
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 |
---|
public ReflectionUtils()
Method Detail |
---|
public static Class<?> forName(String className)
className
- The class to get the Class object for.
public static <T> T newInstance(Class<? extends T> cls)
T
- The Class type.cls
- The Class to instantiate.
public static Object callMethodOverridingIllegalAccess(Object o, String method, Object[] params, Class[] paramClasses)
o
- The object to call.method
- The method name to call.params
- The parameters to pass.paramClasses
- The argument types.
public static Object callMethod(Object o, String method, Object[] params)
o
- The object to call.method
- The method name to call.params
- The parameters to pass.
public static <T> T newInstance(Constructor<T> constructor, Object[] args)
T
- The Class type.constructor
- The constructor.args
- The arguments.
public static <T> Constructor<T> getConstructor(Class<T> cls, Class[] args)
T
- The class type.cls
- The class to get a constructor from.args
- The arguments to match.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |