Package org.codehaus.gmavenplus.util
Class ReflectionUtils
- java.lang.Object
-
- org.codehaus.gmavenplus.util.ReflectionUtils
-
public class ReflectionUtils extends Object
Inspired by Spring's ReflectionUtils.- Since:
- 1.0-beta-1
- Author:
- Keegan Witt
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Constructor
findConstructor(Class<?> clazz, Class<?>... paramTypes)
Attempt to find aConstructor
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 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
getField(Field field, Object target)
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 Detail
-
findMethod
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
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
-
findConstructor
public static Constructor findConstructor(Class<?> clazz, Class<?>... paramTypes)
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
-
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
-
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
-
findField
public static Field findField(Class<?> clazz, String name, Class<?> type)
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
-
getField
public static Object getField(Field field, Object target) throws IllegalAccessException
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
public static Object getStaticField(Field field) throws IllegalAccessException
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
-
-