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 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.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.
-
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
-