public class ReflectionUtils extends Object
Modifier and Type | Method | Description |
---|---|---|
static Constructor |
findConstructor(Class<?> clazz,
Class<?>... paramTypes) |
Attempt to find a
Constructor on the supplied class with the
supplied parameter types. |
static Field |
findField(Class<?> clazz,
String name,
Class<?> type) |
|
static Method |
findMethod(Class<?> clazz,
String name,
Class<?>... paramTypes) |
Attempt to find a
Method on the supplied class with the supplied
name and parameter types. |
static Object |
getEnumValue(Class<?> clazz,
String valueName) |
Find and return the specified value from the specified enum class.
|
static Object |
getField(Field field,
Object target) |
Get the field represented by the supplied
field object on
the specified target object . |
static Object |
getStaticField(Field field) |
Get the field represented by the supplied
field object on
the specified target object . |
static Object |
invokeConstructor(Constructor constructor,
Object... args) |
Invoke the specified
Constructor with the supplied arguments. |
static Object |
invokeMethod(Method method,
Object target,
Object... args) |
Invoke the specified
Method against the supplied target object
with the supplied arguments. |
static Object |
invokeStaticMethod(Method method,
Object... args) |
Invoke the specified static
Method with the supplied arguments. |
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
Method
on the supplied class with the supplied
name and parameter types. Searches all superclasses up to
Object
.clazz
- The class to introspectname
- The name of the methodparamTypes
- The parameter types of the method
(may be null
to indicate any signature)public static Constructor findConstructor(Class<?> clazz, Class<?>... paramTypes)
Constructor
on the supplied class with the
supplied parameter types. Searches all superclasses up to
Object
.clazz
- The class to introspectparamTypes
- The parameter types of the method (may be null
to indicate any signature)public static Object invokeMethod(Method method, Object target, Object... args) throws InvocationTargetException, IllegalAccessException
Method
against the supplied target object
with the supplied arguments. The target object can be null
when invoking a static Method
.method
- The method to invoketarget
- The target object to invoke the method onargs
- The invocation arguments (may be null
)IllegalAccessException
- when unable to access the specified method because access modifiers prevent itInvocationTargetException
- when a reflection invocation failspublic static Object invokeStaticMethod(Method method, Object... args) throws InvocationTargetException, IllegalAccessException
Method
with the supplied arguments.method
- The method to invokeargs
- The invocation arguments (may be null
)IllegalAccessException
- when unable to access the specified method because access modifiers prevent itInvocationTargetException
- when a reflection invocation failspublic static Object invokeConstructor(Constructor constructor, Object... args) throws InvocationTargetException, IllegalAccessException, InstantiationException
Constructor
with the supplied arguments.constructor
- The method to invokeargs
- The invocation arguments (may be null
)IllegalAccessException
- when unable to access the specified constructor because access modifiers prevent itInvocationTargetException
- when a reflection invocation failsInstantiationException
- when an instantiation failspublic static Field findField(Class<?> clazz, String name, Class<?> type)
field
on the supplied Class
with
the supplied name
and/or type
. Searches all
superclasses up to Object
.clazz
- The class to introspectname
- The name of the field (may be null
if type is specified)type
- The type of the field (may be null
if name is specified)public static Object getField(Field field, Object target) throws IllegalAccessException
field object
on
the specified target object
. In accordance with
Field.get(Object)
semantics, the returned value is automatically
wrapped if the underlying field has a primitive type.field
- The field to gettarget
- The target object from which to get the fieldIllegalAccessException
- when unable to access the specified field because access modifiers prevent itpublic static Object getStaticField(Field field) throws IllegalAccessException
field object
on
the specified target object
. In accordance with
Field.get(Object)
semantics, the returned value is automatically
wrapped if the underlying field has a primitive type.field
- The field to getIllegalAccessException
- when unable to access the specified field because access modifiers prevent itCopyright © 2011–2019. All rights reserved.