Package org.seppiko.commons.utils
Class ReflectionUtil
java.lang.Object
org.seppiko.commons.utils.ReflectionUtil
public class ReflectionUtil
extends java.lang.Object
Reflection Util
- Author:
- Leonard Woo
-
Constructor Summary
Constructors Constructor Description ReflectionUtil() -
Method Summary
Modifier and Type Method Description static java.lang.reflect.FieldfindField(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type)find field with typestatic <T> java.lang.reflect.MethodfindMethod(java.lang.Class<T> clazz, java.lang.String methodName)find methodstatic <T> java.lang.reflect.MethodfindMethod(java.lang.Class<T> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes)find a method with parameter typesstatic java.lang.reflect.Method[]getAllDeclaredMethods(java.lang.Class<?> clazz)get methodsstatic java.lang.reflect.Field[]getDeclaredFields(java.lang.Class<?> clazz)get all fieldsstatic java.lang.reflect.Method[]getDeclaredMethods(java.lang.Class<?> clazz)get methodsstatic <T> java.lang.ObjectgetField(java.lang.reflect.Field field, T target)get field valuestatic java.lang.reflect.Parameter[]getParameters(java.lang.reflect.Method method)get method parametersstatic java.lang.Class<?>[]getParameterTypes(java.lang.Object[] args)get parameters typestatic java.lang.ObjectinvokeMethod(java.lang.reflect.Method method, java.lang.Object target)invoke method without parameterstatic java.lang.ObjectinvokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)invoke method on target instancestatic <T> java.lang.ObjectinvokeMethod(T t, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object... args)class instance method callerstatic <T> java.lang.ObjectinvokeMethod(T t, java.lang.String methodName, java.lang.Object... args)class instance method callerstatic <T> TnewInstance(java.lang.Class<T> clazz)get class instance without parameterstatic <T> TnewInstance(java.lang.Class<T> clazz, java.lang.Class<?>[] parameterTypes, java.lang.Object[] initArgs)get class instance with parameterstatic <T, V> voidsetField(java.lang.reflect.Field field, T target, V value)set field valueMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
ReflectionUtil
public ReflectionUtil()
-
-
Method Details
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationExceptionget class instance without parameter- Type Parameters:
T- target class type- Parameters:
clazz- target class- Returns:
- class instance
- Throws:
java.lang.NoSuchMethodException- not found constructor methodjava.lang.IllegalAccessException- can not access constructor methodjava.lang.reflect.InvocationTargetException- can not call constructor methodjava.lang.InstantiationException- can not instance
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz, java.lang.Class<?>[] parameterTypes, java.lang.Object[] initArgs) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.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:
java.lang.NoSuchMethodException- not found constructor methodjava.lang.IllegalAccessException- can not access constructor methodjava.lang.reflect.InvocationTargetException- can not call constructor methodjava.lang.InstantiationException- can not instance
-
findMethod
public static <T> java.lang.reflect.Method findMethod(java.lang.Class<T> clazz, java.lang.String methodName) throws java.lang.NoSuchMethodException, java.lang.SecurityExceptionfind method- Type Parameters:
T- class type- Parameters:
clazz- method classmethodName- method name- Returns:
- method object
- Throws:
java.lang.NoSuchMethodException- not found methodjava.lang.SecurityException- access failed
-
findMethod
public static <T> java.lang.reflect.Method findMethod(java.lang.Class<T> clazz, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException, java.lang.SecurityExceptionfind a method with parameter types- Type Parameters:
T- class type- Parameters:
clazz- method classmethodName- method nameparameterTypes- parameter types- Returns:
- method object
- Throws:
java.lang.NoSuchMethodException- not found methodjava.lang.SecurityException- access failed
-
invokeMethod
public static <T> java.lang.Object invokeMethod(T t, java.lang.String methodName, java.lang.Object... args) throws java.lang.ReflectiveOperationException, java.lang.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:
java.lang.ReflectiveOperationException- can NOT found method or can NOT access methodjava.lang.RuntimeException- method or parameter exception
-
invokeMethod
public static <T> java.lang.Object invokeMethod(T t, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object... args) throws java.lang.ReflectiveOperationException, java.lang.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:
java.lang.ReflectiveOperationException- can NOT found method or can NOT access methodjava.lang.RuntimeException- method or parameter exception
-
getParameterTypes
public static java.lang.Class<?>[] getParameterTypes(java.lang.Object[] args)get parameters type- Parameters:
args- data type objects- Returns:
- get object types
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.lang.IllegalArgumentExceptioninvoke method on target instance- Parameters:
method- target methodtarget- target class instanceargs- method parameters- Returns:
- method return object (if exist)
- Throws:
java.lang.reflect.InvocationTargetException- method throws an exceptionjava.lang.IllegalAccessException- method can not accessjava.lang.IllegalArgumentException- method parameter type or size exception
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target) throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException, java.lang.IllegalArgumentExceptioninvoke method without parameter- Parameters:
method- target methodtarget- target class instance- Returns:
- method return object (if exist)
- Throws:
java.lang.reflect.InvocationTargetException- method throws an exceptionjava.lang.IllegalAccessException- method can not accessjava.lang.IllegalArgumentException- method parameter type or size exception
-
getDeclaredMethods
public static java.lang.reflect.Method[] getDeclaredMethods(java.lang.Class<?> clazz) throws java.lang.SecurityExceptionget methods- Parameters:
clazz- target class- Returns:
- methods
- Throws:
java.lang.SecurityException- can not access method or some else
-
getAllDeclaredMethods
public static java.lang.reflect.Method[] getAllDeclaredMethods(java.lang.Class<?> clazz) throws java.lang.SecurityException, java.lang.IllegalAccessExceptionget methods- Parameters:
clazz- target class- Returns:
- methods
- Throws:
java.lang.SecurityException- can not access method or some elsejava.lang.IllegalAccessException- method can not access
-
findField
public static java.lang.reflect.Field findField(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?> type) throws java.lang.NoSuchFieldExceptionfind field with type- Parameters:
clazz- target classname- field nametype- field type- Returns:
- field object
- Throws:
java.lang.NoSuchFieldException- not found field
-
getDeclaredFields
public static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class<?> clazz) throws java.lang.SecurityExceptionget all fields- Parameters:
clazz- target class- Returns:
- fields
- Throws:
java.lang.SecurityException- field security
-
getField
public static <T> java.lang.Object getField(java.lang.reflect.Field field, T target) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessExceptionget field value- Type Parameters:
T- field name type- Parameters:
field- target class fieldtarget- field name- Returns:
- target field value
- Throws:
java.lang.IllegalArgumentException- field not existjava.lang.IllegalAccessException- field access failed
-
setField
public static <T, V> void setField(java.lang.reflect.Field field, T target, V value) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessExceptionset field value- Type Parameters:
T- field name typeV- field value type- Parameters:
field- target class fieldtarget- field namevalue- field value- Throws:
java.lang.IllegalArgumentException- field not existjava.lang.IllegalAccessException- field access failed
-
getParameters
public static java.lang.reflect.Parameter[] getParameters(java.lang.reflect.Method method)get method parameters- Parameters:
method- target method object- Returns:
- parameters
-