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
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindConcreteMethodsOnInterfaces(Class<?> clazz) static Constructor<?> findConstructor(Class<?> clazz, Class<?>... paramTypes) Attempt to find aConstructoron the supplied class with the supplied parameter types.static Fieldstatic MethodfindMethod(Class<?> clazz, String name, Class<?>... paramTypes) Attempt to find aMethodon the supplied class with the supplied name and parameter types.private static Method[]getDeclaredMethods(Class<?> clazz) This variant retrievesClass.getDeclaredMethods()from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying.static ObjectgetEnumValue(Class<?> clazz, String valueName) Find and return the specified value from the specified enum class.static ObjectGet the field represented by the suppliedfield objecton the specifiedtarget object.static ObjectgetStaticField(Field field) Get the field represented by the suppliedfield objecton the specifiedtarget object.static ObjectinvokeConstructor(Constructor<?> constructor, Object... args) Invoke the specifiedConstructorwith the supplied arguments.static ObjectinvokeMethod(Method method, Object target, Object... args) Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static ObjectinvokeStaticMethod(Method method, Object... args) Invoke the specified staticMethodwith the supplied arguments.
-
Constructor Details
-
ReflectionUtils
private ReflectionUtils()
-
-
Method Details
-
findConstructor
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
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
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
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 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:
IllegalAccessException- when unable to access the specified field because access modifiers prevent it
-
getStaticField
Get 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:
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 specifiedConstructorwith 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 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:
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 staticMethodwith 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
-
getDeclaredMethods
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:
-
findConcreteMethodsOnInterfaces
-