java.lang.Object
org.seppiko.commons.utils.reflect.ReflectionUtil
Reflection Util.
- Author:
- Leonard Woo
-
Method Summary
Modifier and TypeMethodDescriptionstatic Fieldfind field with type.static <T> MethodfindMethod(Class<T> clazz, String methodName) find method.static <T> MethodfindMethod(Class<T> clazz, String methodName, Class<?>... parameterTypes) find a method with parameter types.static <S> List<S>findServiceProviders(Class<S> serviceClazz) Find service providers.static Method[]getAllDeclaredMethods(Class<?> clazz) get methods.static Field[]getDeclaredFields(Class<?> clazz) get all fields.static Method[]getDeclaredMethods(Class<?> clazz) get methods.static <T> Objectget field value.static Parameter[]getParameters(Method method) get method parameters.static Class<?>[]getParameterTypes(Object[] args) get parameters type.static Field[]getRecordFields(Class<?> clazz) get record class all fields.static <S> ServiceLoader<S>getServiceLoader(Class<S> serviceClazz, ClassLoader classLoader) Get service loader.static ObjectinvokeMethod(Method method, Object target) invoke method without parameter.static ObjectinvokeMethod(Method method, Object target, Object... args) invoke method on target instance.static <T> ObjectinvokeMethod(T t, String methodName, Class<?>[] parameterTypes, Object... args) class instance method caller.static <T> ObjectinvokeMethod(T t, String methodName, Object... args) class instance method caller.static <T> TnewInstance(Class<T> clazz) get class instance without parameter.static <T> TnewInstance(Class<T> clazz, Class<?>[] parameterTypes, Object[] initArgs) get class instance with parameter.static <T,V> void set field value.static <T> TsetRecordAllFields(Class<T> clazz, Class<?>[] parameterTypes, Object... args) set record class all field.
-
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 method.IllegalAccessException- can not access constructor method.InvocationTargetException- can not call constructor method.InstantiationException- 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 class.parameterTypes- class constructor parameter types.initArgs- class constructor parameter objects.- Returns:
- class instance.
- Throws:
NoSuchMethodException- not found constructor method.IllegalAccessException- can not access constructor method.InvocationTargetException- can not call constructor method.InstantiationException- 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 class.methodName- method name.- Returns:
- method object.
- Throws:
NoSuchMethodException- not found method.SecurityException- 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 class.methodName- method name.parameterTypes- parameter types.- Returns:
- method object.
- Throws:
NoSuchMethodException- not found method.SecurityException- 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 instance.methodName- call method name.args- parameter objects (can not use basic data types, cause nonsupport auto unboxing).- Returns:
- method return object.
- Throws:
ReflectiveOperationException- not found method or can not access method.RuntimeException- 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 instance.methodName- call method name.parameterTypes- parameter types.args- parameter objects.- Returns:
- method return object.
- Throws:
ReflectiveOperationException- not found method or can not access method.RuntimeException- 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 method.target- target class instance.args- method parameters.- Returns:
- method return object (if exist).
- Throws:
InvocationTargetException- method throws an exception.IllegalAccessException- method can not access.IllegalArgumentException- 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 method.target- target class instance.- Returns:
- method return object (if exist).
- Throws:
InvocationTargetException- method throws an exception.IllegalAccessException- method can not access.IllegalArgumentException- 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 else.IllegalAccessException- method can not access.
-
findField
public static Field findField(Class<?> clazz, String name, Class<?> type) throws NoSuchFieldException find field with type.- Parameters:
clazz- target class.name- field name.type- 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 found.SecurityException- 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 field.target- field name.- Returns:
- target field value.
- Throws:
IllegalArgumentException- field not exist.IllegalAccessException- field access failed.InaccessibleObjectException- file cannot enable accessible.NullPointerException- field is null.SecurityException- 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 type.V- field value type.- Parameters:
field- target class field.target- field name.value- field value.- Throws:
NullPointerException- field or something is null.IllegalArgumentException- field not exist.IllegalAccessException- field access failed.InaccessibleObjectException- file cannot enable accessible.SecurityException- 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 class.parameterTypes- record parameter types.args- record parameter values.- Returns:
- target record class.
- Throws:
NoSuchMethodException- if a matching method is not found.InstantiationException- if the class that declares the underlying constructor represents an abstract class.SecurityException- field security.IllegalAccessException- if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.IllegalArgumentException- 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 class.InvocationTargetException- if the underlying constructor throws an exception.
-
getParameters
get method parameters.- Parameters:
method- target method object.- Returns:
- parameters.
-
findServiceProviders
Find service providers.- Type Parameters:
S- service provider type.- Parameters:
serviceClazz- service provider interface.- Returns:
- service providers.
-
getServiceLoader
Get service loader.- Type Parameters:
S- service provider type.- Parameters:
serviceClazz- service provider interface.classLoader- The class loader to be used to load provider-configuration files and provider classes, ornullif the system class loader (or, failing that, the bootstrap class loader) is to be used.- Returns:
- A new service loader.
-