Package org.codehaus.gmavenplus.util
Class ReflectionUtils
java.lang.Object
org.codehaus.gmavenplus.util.ReflectionUtils
Inspired heavily by Spring's ReflectionUtils.
- Since:
- 1.0-beta-1
- Author:
- Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams, Keegan Witt
-
Method Summary
Modifier and TypeMethodDescriptionstatic Constructor
<?> findConstructor
(Class<?> clazz, Class<?>... paramTypes) Attempt to find aConstructor
on the supplied class with the supplied parameter types.static Field
static Method
findMethod
(Class<?> clazz, String name, Class<?>... paramTypes) Attempt to find aMethod
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
Get the field represented by the suppliedfield object
on the specifiedtarget object
.static Object
getStaticField
(Field field) Get the field represented by the suppliedfield object
on the specifiedtarget object
.static Object
invokeConstructor
(Constructor<?> constructor, Object... args) Invoke the specifiedConstructor
with the supplied arguments.static Object
invokeMethod
(Method method, Object target, Object... args) Invoke the specifiedMethod
against the supplied target object with the supplied arguments.static Object
invokeStaticMethod
(Method method, Object... args) Invoke the specified staticMethod
with the supplied arguments.
-
Method Details
-
findConstructor
Attempt to find aConstructor
on the supplied class with the supplied parameter types. Searches all superclasses up toObject
.- Parameters:
clazz
- The class to introspectparamTypes
- The parameter types of the method (may benull
to indicate any signature)- Returns:
- The Constructor object
-
findField
Attempt to find afield
on the suppliedClass
with the suppliedname
and/ortype
. Searches all superclasses up toObject
.- Parameters:
clazz
- The class to introspectname
- The name of the field (may benull
if type is specified)type
- The type of the field (may benull
if name is specified)- Returns:
- The corresponding Field object
-
findMethod
Attempt to find aMethod
on the supplied class with the supplied name and parameter types. Searches all superclasses up toObject
.- Parameters:
clazz
- The class to introspectname
- The name of the methodparamTypes
- The parameter types of the method (may benull
to indicate any signature)- Returns:
- The Method object
-
getEnumValue
Find and return the specified value from the specified enum class.- Parameters:
clazz
- The enum class to introspectvalueName
- The name of the enum value to get- Returns:
- The enum value
-
getField
Get the field represented by the suppliedfield object
on the specifiedtarget object
. In accordance withField.get(Object)
semantics, the returned value is automatically wrapped if the underlying field has a primitive type.- Parameters:
field
- The field to gettarget
- The target object from which to get the field- Returns:
- The field's current value
- Throws:
IllegalAccessException
- when unable to access the specified field because access modifiers prevent it
-
getStaticField
Get the field represented by the suppliedfield object
on the specifiedtarget object
. In accordance withField.get(Object)
semantics, the returned value is automatically wrapped if the underlying field has a primitive type.- Parameters:
field
- The field to get- Returns:
- The field's current value
- Throws:
IllegalAccessException
- when unable to access the specified field because access modifiers prevent it
-
invokeConstructor
public static Object invokeConstructor(Constructor<?> constructor, Object... args) throws InvocationTargetException, IllegalAccessException, InstantiationException Invoke the specifiedConstructor
with the supplied arguments.- Parameters:
constructor
- The method to invokeargs
- The invocation arguments (may benull
)- Returns:
- The invocation result, if any
- Throws:
IllegalAccessException
- when unable to access the specified constructor because access modifiers prevent itInvocationTargetException
- when a reflection invocation failsInstantiationException
- when an instantiation fails
-
invokeMethod
public static Object invokeMethod(Method method, Object target, Object... args) throws InvocationTargetException, IllegalAccessException Invoke the specifiedMethod
against the supplied target object with the supplied arguments. The target object can benull
when invoking a staticMethod
.- Parameters:
method
- The method to invoketarget
- The target object to invoke the method onargs
- The invocation arguments (may benull
)- Returns:
- The invocation result, if any
- Throws:
IllegalAccessException
- when unable to access the specified method because access modifiers prevent itInvocationTargetException
- when a reflection invocation fails
-
invokeStaticMethod
public static Object invokeStaticMethod(Method method, Object... args) throws InvocationTargetException, IllegalAccessException Invoke the specified staticMethod
with the supplied arguments.- Parameters:
method
- The method to invokeargs
- The invocation arguments (may benull
)- Returns:
- The invocation result, if any
- Throws:
IllegalAccessException
- when unable to access the specified method because access modifiers prevent itInvocationTargetException
- when a reflection invocation fails
-