public final class ReflectionUtil extends Object
The GNU Lesser General Public License for details.
Modifier | Constructor and Description |
---|---|
private |
ReflectionUtil()
Prevent instantiation
|
Modifier and Type | Method and Description |
---|---|
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
|
public static <T> T construct(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException
T
- the type of the object to constructclassName
- the full class name of the objectClassNotFoundException
- if the class is not foundInstantiationException
- 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.public static <T> T construct(String className, Object... params) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
T
- the type of the object to constructclassName
- the full class name of the objectparams
- the constructor's argumentsClassNotFoundException
- if the class is not foundNoSuchMethodException
- the method does not existInstantiationException
- 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.public static <T> T construct(Class<T> clazz, Object... params) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
T
- the type of the object to constructclazz
- the class of the objectparams
- the constructor's argumentsNoSuchMethodException
- the method does not existInstantiationException
- 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.public static <T> T construct(String className, Object[] params, Class<?>[] paramTypes) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
T
- the type of the object to constructclassName
- the full class name of the objectparams
- the constructor's argumentsparamTypes
- the types of the parametersClassNotFoundException
- if the class is not foundNoSuchMethodException
- the method does not existInstantiationException
- 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.public static Object invoke(Object base, String methodName, Object... params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
base
- The object to invoke a method onmethodName
- The text of the invocation, for example "getName"params
- For example new Object[] { ...}NoSuchMethodException
- the method does not existIllegalAccessException
- if this Constructor
object
is enforcing Java language access control and the underlying
constructor is inaccessible.InvocationTargetException
- if the underlying constructor
throws an exception.public static Object invoke(String call, Object... params) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
call
- The text of the invocation, for example
"java.lang.String.getName"params
- For example new Object[] { ...}ClassNotFoundException
- if the class is not foundNoSuchMethodException
- the method does not existIllegalAccessException
- if this Constructor
object
is enforcing Java language access control and the underlying
constructor is inaccessible.InvocationTargetException
- if the underlying constructor
throws an exception.public static <T> Object invoke(Class<T> clazz, Object obj, String methodName, Object... params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
T
- the type of the object to constructclazz
- the class of the objectobj
- the object having the method, or null to call a static methodmethodName
- the method to be calledparams
- the parametersNoSuchMethodException
- the method does not existIllegalAccessException
- if this Constructor
object
is enforcing Java language access control and the underlying
constructor is inaccessible.InvocationTargetException
- if the underlying constructor
throws an exception.public static <T> Object invoke(Class<T> clazz, Object obj, String methodName, Object[] params, Class<?>[] paramTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
T
- the type of the object to constructclazz
- the class of the objectobj
- the object having the method, or null to call a static methodmethodName
- the method to be calledparams
- the parametersparamTypes
- the types of each of the parametersNoSuchMethodException
- the method does not existIllegalAccessException
- if this Constructor
object
is enforcing Java language access control and the underlying
constructor is inaccessible.InvocationTargetException
- if the underlying constructor
throws an exception.private static Class<?>[] describeParameters(Object... params)
params
- the types to describeprivate static <T> Method getMethod(Class<T> clazz, String methodName, Class<?>[] calledTypes) throws NoSuchMethodException
NoSuchMethodException