Package org.codehaus.gmavenplus.util
Class ReflectionUtils
- java.lang.Object
-
- org.codehaus.gmavenplus.util.ReflectionUtils
-
public class ReflectionUtils extends java.lang.ObjectInspired heavily by Spring's ReflectionUtils.- Since:
- 1.0-beta-1
-
-
Constructor Summary
Constructors Modifier Constructor Description privateReflectionUtils()
-
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 aConstructoron the supplied class with the supplied parameter types.static java.lang.reflect.FieldfindField(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type)Attempt to find afieldon the suppliedClasswith the suppliednameand/ortype.static java.lang.reflect.MethodfindMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... paramTypes)Attempt to find aMethodon 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.ObjectgetEnumValue(java.lang.Class<?> clazz, java.lang.String valueName)Find and return the specified value from the specified enum class.static java.lang.ObjectgetField(java.lang.reflect.Field field, java.lang.Object target)Get the field represented by the suppliedfield objecton the specifiedtarget object.static java.lang.ObjectgetStaticField(java.lang.reflect.Field field)Get the field represented by the suppliedfield objecton the specifiedtarget object.static java.lang.ObjectinvokeConstructor(java.lang.reflect.Constructor<?> constructor, java.lang.Object... args)Invoke the specifiedConstructorwith the supplied arguments.static java.lang.ObjectinvokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static java.lang.ObjectinvokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... args)Invoke the specified staticMethodwith the supplied arguments.
-
-
-
Method Detail
-
findConstructor
public static java.lang.reflect.Constructor<?> findConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... paramTypes)Attempt to find aConstructoron 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 benullto 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 afieldon the suppliedClasswith the suppliednameand/ortype. Searches all superclasses up toObject.- Parameters:
clazz- The class to introspectname- The name of the field (may benullif type is specified)type- The type of the field (may benullif 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 aMethodon 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 benullto 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.IllegalAccessExceptionGet the field represented by the suppliedfield objecton 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.IllegalAccessExceptionGet the field represented by the suppliedfield objecton 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.InstantiationExceptionInvoke the specifiedConstructorwith 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.IllegalAccessExceptionInvoke the specifiedMethodagainst the supplied target object with the supplied arguments. The target object can benullwhen 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.IllegalAccessExceptionInvoke the specified staticMethodwith 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)
-
-