Package org.codehaus.gmavenplus.util
Class ReflectionUtils
- java.lang.Object
-
- org.codehaus.gmavenplus.util.ReflectionUtils
-
public class ReflectionUtils extends java.lang.Object
Inspired heavily by Spring's ReflectionUtils.- Since:
- 1.0-beta-1
-
-
Constructor Summary
Constructors Modifier Constructor Description private
ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.util.List<java.lang.reflect.Method>
findConcreteMethodsOnInterfaces(java.lang.Class<?> clazz)
static java.lang.reflect.Constructor<?>
findConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... paramTypes)
Attempt to find aConstructor
on the supplied class with the supplied parameter types.static java.lang.reflect.Field
findField(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type)
Attempt to find afield
on the suppliedClass
with the suppliedname
and/ortype
.static java.lang.reflect.Method
findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... paramTypes)
Attempt to find aMethod
on the supplied class with the supplied name and parameter types.private static java.lang.reflect.Method[]
getDeclaredMethods(java.lang.Class<?> clazz)
This variant retrievesClass.getDeclaredMethods()
from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying.static java.lang.Object
getEnumValue(java.lang.Class<?> clazz, java.lang.String valueName)
Find and return the specified value from the specified enum class.static java.lang.Object
getField(java.lang.reflect.Field field, java.lang.Object target)
Get the field represented by the suppliedfield object
on the specifiedtarget object
.static java.lang.Object
getStaticField(java.lang.reflect.Field field)
Get the field represented by the suppliedfield object
on the specifiedtarget object
.static java.lang.Object
invokeConstructor(java.lang.reflect.Constructor<?> constructor, java.lang.Object... args)
Invoke the specifiedConstructor
with the supplied arguments.static java.lang.Object
invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
Invoke the specifiedMethod
against the supplied target object with the supplied arguments.static java.lang.Object
invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... args)
Invoke the specified staticMethod
with the supplied arguments.
-
-
-
Method Detail
-
findConstructor
public static java.lang.reflect.Constructor<?> findConstructor(java.lang.Class<?> clazz, java.lang.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
-
findField
public static java.lang.reflect.Field findField(java.lang.Class<?> clazz, java.lang.String name, java.lang.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
-
findMethod
public static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.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
-
getEnumValue
public static java.lang.Object getEnumValue(java.lang.Class<?> clazz, java.lang.String valueName)
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
public static java.lang.Object getField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.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:
java.lang.IllegalAccessException
- when unable to access the specified field because access modifiers prevent it
-
getStaticField
public static java.lang.Object getStaticField(java.lang.reflect.Field field) throws java.lang.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:
java.lang.IllegalAccessException
- when unable to access the specified field because access modifiers prevent it
-
invokeConstructor
public static java.lang.Object invokeConstructor(java.lang.reflect.Constructor<?> constructor, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.lang.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:
java.lang.IllegalAccessException
- when unable to access the specified constructor because access modifiers prevent itjava.lang.reflect.InvocationTargetException
- when a reflection invocation failsjava.lang.InstantiationException
- when an instantiation fails
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.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:
java.lang.IllegalAccessException
- when unable to access the specified method because access modifiers prevent itjava.lang.reflect.InvocationTargetException
- when a reflection invocation fails
-
invokeStaticMethod
public static java.lang.Object invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.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:
java.lang.IllegalAccessException
- when unable to access the specified method because access modifiers prevent itjava.lang.reflect.InvocationTargetException
- when a reflection invocation fails
-
getDeclaredMethods
private static java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> clazz)
This variant retrievesClass.getDeclaredMethods()
from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying. In addition, it also includes Java 8 default methods from locally implemented interfaces, since those are effectively to be treated just like declared methods.- Parameters:
clazz
- the class to introspect- Returns:
- the cached array of methods
- See Also:
Class.getDeclaredMethods()
-
findConcreteMethodsOnInterfaces
private static java.util.List<java.lang.reflect.Method> findConcreteMethodsOnInterfaces(java.lang.Class<?> clazz)
-
-