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 Field[]getRecordFields(Class<?> clazz) get record class all fieldsstatic 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> void set field valuestatic <T> TsetRecordAllFields(Class<T> clazz, Class<?>[] parameterTypes, Object... args) set record class all field
-
Constructor Details
-
ReflectionUtil
public ReflectionUtil()
-
-
Method Details
-
newInstance
public static <T> T newInstance(Class<T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException get 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, InstantiationException get 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, SecurityException find 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, SecurityException find 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, RuntimeException class 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, RuntimeException class 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, IllegalArgumentException invoke 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, IllegalArgumentException invoke 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, IllegalAccessException get 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 NoSuchFieldException find 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
-
getRecordFields
public static Field[] getRecordFields(Class<?> clazz) throws NoSuchFieldException, SecurityException get record class all fields- Parameters:
clazz- target record class- Returns:
- fields
- Throws:
NoSuchFieldException- if a field with the specified name is not foundSecurityException- field security
-
getField
public static <T> Object getField(Field field, T target) throws IllegalArgumentException, IllegalAccessException, InaccessibleObjectException, SecurityException, NullPointerException get 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 failedInaccessibleObjectException- file cannot enable accessibleNullPointerException- field is nullSecurityException- field security
-
setField
public static <T,V> void setField(Field field, T target, V value) throws IllegalArgumentException, IllegalAccessException, InaccessibleObjectException, NullPointerException, SecurityException set field value- Type Parameters:
T- field name typeV- field value type- Parameters:
field- target class fieldtarget- field namevalue- field value- Throws:
NullPointerException- field or something is nullIllegalArgumentException- field not existIllegalAccessException- field access failedInaccessibleObjectException- file cannot enable accessibleSecurityException- field security
-
setRecordAllFields
public static <T> T setRecordAllFields(Class<T> clazz, Class<?>[] parameterTypes, Object... args) throws NoSuchMethodException, InstantiationException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException set record class all field- Type Parameters:
T- record class- Parameters:
clazz- record classparameterTypes- record parameter typesargs- record parameter values- Returns:
- target record class
- Throws:
NoSuchMethodException- if a matching method is not foundInstantiationException- if the class that declares the underlying constructor represents an abstract classSecurityException- field securityIllegalAccessException- if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessibleIllegalArgumentException- if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum classInvocationTargetException- if the underlying constructor throws an exception
-
getParameters
get method parameters- Parameters:
method- target method object- Returns:
- parameters
-