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 ConstructorfindConstructor(Class<?> clazz, Class<?>... paramTypes)Attempt to find aConstructoron the supplied class with the supplied parameter types.static FieldfindField(Class<?> clazz, String name, Class<?> type)static 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 ObjectgetField(Field field, Object target)Get 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 Detail
-
findMethod
public static Method findMethod(Class<?> clazz, String name, 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
-
findConstructor
public static Constructor findConstructor(Class<?> clazz, 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
-
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
-
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
-
findField
public static Field findField(Class<?> clazz, String name, 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
-
getField
public static Object getField(Field field, Object target) throws IllegalAccessException
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
public static Object getStaticField(Field field) throws IllegalAccessException
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
-
-