java.lang.Object
org.seppiko.commons.utils.reflect.ReflectionUtil
Reflection Util
- Author:
- Leonard Woo
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Fieldfind field with typestatic <T> MethodfindMethod(Class<T> clazz, String methodName)find methodstatic <T> MethodfindMethod(Class<T> clazz, String methodName, Class<?>... parameterTypes)find a method with parameter typesstatic Method[]getAllDeclaredMethods(Class<?> clazz)get methodsstatic Field[]getDeclaredFields(Class<?> clazz)get all fieldsstatic Method[]getDeclaredMethods(Class<?> clazz)get methodsstatic <T> Objectget field valuestatic Parameter[]getParameters(Method method)get method parametersstatic Class<?>[]getParameterTypes(Object[] args)get parameters typestatic ObjectinvokeMethod(Method method, Object target)invoke method without parameterstatic ObjectinvokeMethod(Method method, Object target, Object... args)invoke method on target instancestatic <T> ObjectinvokeMethod(T t, String methodName, Class<?>[] parameterTypes, Object... args)class instance method callerstatic <T> ObjectinvokeMethod(T t, String methodName, Object... args)class instance method callerstatic <T> TnewInstance(Class<T> clazz)get class instance without parameterstatic <T> TnewInstance(Class<T> clazz, Class<?>[] parameterTypes, Object[] initArgs)get class instance with parameterstatic <T, V> voidset field value
-
Constructor Details
-
ReflectionUtil
public ReflectionUtil()
-
-
Method Details
-
newInstance
public static <T> T newInstance(Class<T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationExceptionget class instance without parameter- Type Parameters:
T- target class type- Parameters:
clazz- target class- Returns:
- class instance
- Throws:
NoSuchMethodException- not found constructor methodIllegalAccessException- can not access constructor methodInvocationTargetException- can not call constructor methodInstantiationException- can not instance
-
newInstance
public static <T> T newInstance(Class<T> clazz, Class<?>[] parameterTypes, Object[] initArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationExceptionget class instance with parameter- Type Parameters:
T- target class type- Parameters:
clazz- target classparameterTypes- class constructor parameter typesinitArgs- class constructor parameter objects- Returns:
- class instance
- Throws:
NoSuchMethodException- not found constructor methodIllegalAccessException- can not access constructor methodInvocationTargetException- can not call constructor methodInstantiationException- can not instance
-
findMethod
public static <T> Method findMethod(Class<T> clazz, String methodName) throws NoSuchMethodException, SecurityExceptionfind method- Type Parameters:
T- class type- Parameters:
clazz- method classmethodName- method name- Returns:
- method object
- Throws:
NoSuchMethodException- not found methodSecurityException- access failed
-
findMethod
public static <T> Method findMethod(Class<T> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityExceptionfind a method with parameter types- Type Parameters:
T- class type- Parameters:
clazz- method classmethodName- method nameparameterTypes- parameter types- Returns:
- method object
- Throws:
NoSuchMethodException- not found methodSecurityException- access failed
-
invokeMethod
public static <T> Object invokeMethod(T t, String methodName, Object... args) throws ReflectiveOperationException, RuntimeExceptionclass instance method caller- Type Parameters:
T- target class- Parameters:
t- target class instancemethodName- call method nameargs- parameter objects (can NOT use basic data types, cause nonsupport auto unboxing)- Returns:
- method return object
- Throws:
ReflectiveOperationException- can NOT found method or can NOT access methodRuntimeException- method or parameter exception
-
invokeMethod
public static <T> Object invokeMethod(T t, String methodName, Class<?>[] parameterTypes, Object... args) throws ReflectiveOperationException, RuntimeExceptionclass instance method caller- Type Parameters:
T- target class- Parameters:
t- target class instancemethodName- call method nameparameterTypes- parameter typesargs- parameter objects- Returns:
- method return object
- Throws:
ReflectiveOperationException- can NOT found method or can NOT access methodRuntimeException- method or parameter exception
-
getParameterTypes
get parameters type- Parameters:
args- data type objects- Returns:
- get object types
-
invokeMethod
public static Object invokeMethod(Method method, Object target, Object... args) throws InvocationTargetException, IllegalAccessException, IllegalArgumentExceptioninvoke method on target instance- Parameters:
method- target methodtarget- target class instanceargs- method parameters- Returns:
- method return object (if exist)
- Throws:
InvocationTargetException- method throws an exceptionIllegalAccessException- method can not accessIllegalArgumentException- method parameter type or size exception
-
invokeMethod
public static Object invokeMethod(Method method, Object target) throws InvocationTargetException, IllegalAccessException, IllegalArgumentExceptioninvoke method without parameter- Parameters:
method- target methodtarget- target class instance- Returns:
- method return object (if exist)
- Throws:
InvocationTargetException- method throws an exceptionIllegalAccessException- method can not accessIllegalArgumentException- method parameter type or size exception
-
getDeclaredMethods
get methods- Parameters:
clazz- target class- Returns:
- methods
- Throws:
SecurityException- can not access method or some else
-
getAllDeclaredMethods
public static Method[] getAllDeclaredMethods(Class<?> clazz) throws SecurityException, IllegalAccessExceptionget methods- Parameters:
clazz- target class- Returns:
- methods
- Throws:
SecurityException- can not access method or some elseIllegalAccessException- method can not access
-
findField
public static Field findField(Class<?> clazz, String name, Class<?> type) throws NoSuchFieldExceptionfind field with type- Parameters:
clazz- target classname- field nametype- field type- Returns:
- field object
- Throws:
NoSuchFieldException- not found field
-
getDeclaredFields
get all fields- Parameters:
clazz- target class- Returns:
- fields
- Throws:
SecurityException- field security
-
getField
public static <T> Object getField(Field field, T target) throws IllegalArgumentException, IllegalAccessExceptionget field value- Type Parameters:
T- field name type- Parameters:
field- target class fieldtarget- field name- Returns:
- target field value
- Throws:
IllegalArgumentException- field not existIllegalAccessException- field access failed
-
setField
public static <T, V> void setField(Field field, T target, V value) throws IllegalArgumentException, IllegalAccessExceptionset field value- Type Parameters:
T- field name typeV- field value type- Parameters:
field- target class fieldtarget- field namevalue- field value- Throws:
IllegalArgumentException- field not existIllegalAccessException- field access failed
-
getParameters
get method parameters- Parameters:
method- target method object- Returns:
- parameters
-