Package icu.easyj.core.util
Class ReflectionUtils
java.lang.Object
icu.easyj.core.util.ReflectionUtils
反射工具类
- Author:
- wangliang181230
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringannotationToString(Annotation annotation) annotatio to stringstatic StringclassToString(Class<?> clazz) class to stringstatic booleancontainsMethod(Collection<Method> methods, Method method) Contains methodstatic booleancontainsMethod(Map<Method, ?> methodMap, Method method) Contains methodstatic booleanequalsMethod(Method m1, Method m2) Equals methodstatic StringfieldToString(Class<?> clazz, String fieldName, Class<?> fieldType) field to stringstatic StringfieldToString(Field field) field to stringstatic Field[]getAllFields(Class<?> targetClazz) Gets all fields, excluding static or synthetic fieldsstatic <T extends Annotation>
TgetAnnotation(Method method, Class<T> annotationClass) get annotation from the method or super class methodstatic <T> TgetAnnotationValue(Annotation annotation, String fieldName) get annotation valuegetAnnotationValues(Annotation annotation) get annotation valuesstatic Class<?>getClassByName(String className) Gets class by name.static Class<?>getClassByName(String className, ClassLoader loader) Gets class by name.static Fieldget Fieldstatic <T> TgetFieldValue(Object target, Field field) get field valuestatic <T> TgetFieldValue(Object target, String fieldName) get field valuegetInterfaces(Class<?> clazz) get all interface of the clazzstatic Methodget methodstatic Methodget methodstatic <T> TgetSingleton(Class<T> clazz) get singleton for the classstatic booleanhas Fieldstatic booleanHas methodstatic ObjectinvokeMethod(Object target, Method method) invoke Methodstatic ObjectinvokeMethod(Object target, Method method, Object... args) invoke Methodstatic ObjectinvokeMethod(Object target, String methodName) invoke Methodstatic ObjectinvokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object... args) invoke Methodstatic ObjectinvokeStaticMethod(Class<?> targetClass, String staticMethodName) invoke static Methodstatic ObjectinvokeStaticMethod(Class<?> targetClass, String staticMethodName, Class<?>[] parameterTypes, Object... args) invoke static Methodstatic ObjectinvokeStaticMethod(Method staticMethod) invoke static Methodstatic ObjectinvokeStaticMethod(Method staticMethod, Object... args) invoke static Methodstatic StringmethodToString(Class<?> clazz, String methodName, Class<?>... parameterTypes) method to stringstatic StringmethodToString(Method method) method to stringstatic StringparameterTypesToString(Class<?>[] parameterTypes) parameter types to stringstatic <T extends AccessibleObject>
voidsetAccessible(T accessible) set accessible to true if falsestatic voidsetFieldValue(Object target, Field field, Object fieldValue) set field valuestatic voidsetFieldValue(Object target, String fieldName, Object fieldValue) get field valuestatic voidsetStaticFieldValue(Class<?> targetClass, String staticFieldName, Object newValue) set `static` field valuestatic voidsetStaticFieldValue(Field staticField, Object newValue) set `static` field value
-
Field Details
-
NULL_FIELD
The NULL_FIELD -
NULL_METHOD
The NULL_METHOD
-
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getClassByName
@NonNull public static Class<?> getClassByName(String className, ClassLoader loader) throws ClassNotFoundException Gets class by name.- Parameters:
className- the class nameloader- the class loader- Returns:
- the class by name
- Throws:
ClassNotFoundException- the class not found exception
-
getClassByName
Gets class by name.- Parameters:
className- the class name- Returns:
- the class by name
- Throws:
ClassNotFoundException- the class not found exception
-
getInterfaces
get all interface of the clazz- Parameters:
clazz- the clazz- Returns:
- set
-
setAccessible
public static <T extends AccessibleObject> void setAccessible(T accessible) throws SecurityException set accessible to true if false- Type Parameters:
T- the type of the accessible- Parameters:
accessible- the accessible- Throws:
SecurityException- if the request is denied.
-
getAllFields
Gets all fields, excluding static or synthetic fields- Parameters:
targetClazz- the target class- Returns:
- allFields the all fields
-
hasField
has Field- Parameters:
clazz- the classfieldName- the field name- Returns:
- the boolean
-
getField
@NonNull public static Field getField(Class<?> clazz, String fieldName) throws NoSuchFieldException, SecurityException get Field- Parameters:
clazz- the classfieldName- the field name- Returns:
- the field
- Throws:
IllegalArgumentException- ifclazzorfieldNameis nullNoSuchFieldException- if the field namedfieldNamedoes not existSecurityException- the security exception
-
getFieldValue
@Nullable public static <T> T getFieldValue(Object target, Field field) throws IllegalArgumentException, SecurityException get field value- Type Parameters:
T- the field type- Parameters:
target- the targetfield- the field of the target- Returns:
- field value
- Throws:
IllegalArgumentException- iftargetisnullSecurityException- the security exceptionClassCastException- if the type of the variable receiving the field value is not equals to the field type
-
getFieldValue
@Nullable public static <T> T getFieldValue(Object target, String fieldName) throws IllegalArgumentException, NoSuchFieldException, SecurityException get field value- Type Parameters:
T- the field type- Parameters:
target- the targetfieldName- the field name- Returns:
- field value
- Throws:
IllegalArgumentException- iftargetorfieldNameisnullNoSuchFieldException- if the field namedfieldNamedoes not existSecurityException- the security exceptionClassCastException- if the type of the variable receiving the field value is not equals to the field type
-
setFieldValue
public static void setFieldValue(Object target, Field field, Object fieldValue) throws IllegalArgumentException, SecurityException set field value- Parameters:
target- the targetfield- the field of the targetfieldValue- the field value- Throws:
IllegalArgumentException- iftargetisnullSecurityException- the security exception
-
setFieldValue
public static void setFieldValue(Object target, String fieldName, Object fieldValue) throws IllegalArgumentException, NoSuchFieldException, SecurityException get field value- Parameters:
target- the targetfieldName- the field namefieldValue- the field value- Throws:
IllegalArgumentException- iftargetorfieldNameisnullNoSuchFieldException- if the field namedfieldNamedoes not existSecurityException- the security exception
-
setStaticFieldValue
public static void setStaticFieldValue(Field staticField, Object newValue) throws IllegalAccessException set `static` field value- Parameters:
staticField- the static fieldnewValue- the new value- Throws:
IllegalArgumentException- ifstaticFieldisnullor not a static fieldIllegalAccessException- the illegal access exception
-
setStaticFieldValue
public static void setStaticFieldValue(Class<?> targetClass, String staticFieldName, Object newValue) throws NoSuchFieldException, IllegalAccessException set `static` field value- Parameters:
targetClass- the target classstaticFieldName- the static field namenewValue- the new value- Throws:
IllegalArgumentException- iftargetClassorstaticFieldNameisnullNullPointerException- ifstaticFieldNameisnullNoSuchFieldException- if the field namedmodifyFieldNamedoes not existIllegalAccessException- the illegal access exception
-
getMethod
@NonNull public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException get method- Parameters:
clazz- the classmethodName- the method nameparameterTypes- the parameter types- Returns:
- the method
- Throws:
IllegalArgumentException- ifclazzisnullNullPointerException- ifmethodNameisnullNoSuchMethodException- if the method namedmethodNamedoes not existSecurityException- the security exception
-
getMethod
@NonNull public static Method getMethod(Class<?> clazz, String methodName) throws NoSuchMethodException, SecurityException get method- Parameters:
clazz- the classmethodName- the method name- Returns:
- the method
- Throws:
IllegalArgumentException- ifclazzisnullNullPointerException- ifmethodNameisnullNoSuchMethodException- if the method namedmethodNamedoes not existSecurityException- the security exception
-
invokeMethod
public static Object invokeMethod(Object target, Method method, Object... args) throws InvocationTargetException, IllegalArgumentException, SecurityException invoke Method- Parameters:
target- the targetmethod- the methodargs- the args- Returns:
- the result of the underlying method
- Throws:
InvocationTargetException- if the underlying method throws an exception.IllegalArgumentException- the illegal argument exceptionSecurityException- the security exception
-
invokeMethod
public static Object invokeMethod(Object target, Method method) throws InvocationTargetException, IllegalArgumentException, SecurityException invoke Method- Parameters:
target- the targetmethod- the method- Returns:
- the result of the underlying method
- Throws:
InvocationTargetException- if the underlying method throws an exception.IllegalArgumentException- the illegal argument exceptionSecurityException- the security exception
-
invokeMethod
public static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object... args) throws NoSuchMethodException, InvocationTargetException, IllegalArgumentException, SecurityException invoke Method- Parameters:
target- the targetmethodName- the method nameparameterTypes- the parameter typesargs- the args- Returns:
- the result of the underlying method
- Throws:
IllegalArgumentException- iftargetisnullNullPointerException- ifmethodNameisnullNoSuchMethodException- if the method namedmethodNamedoes not existInvocationTargetException- if the underlying method throws an exception.SecurityException- the security exception
-
invokeMethod
public static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalArgumentException, SecurityException invoke Method- Parameters:
target- the targetmethodName- the method name- Returns:
- the result of the underlying method
- Throws:
NoSuchMethodException- the no such method exceptionInvocationTargetException- if the underlying method throws an exception.IllegalArgumentException- the illegal argument exceptionSecurityException- the security exception
-
invokeStaticMethod
public static Object invokeStaticMethod(Method staticMethod, Object... args) throws IllegalArgumentException, InvocationTargetException, SecurityException invoke static Method- Parameters:
staticMethod- the static methodargs- the args- Returns:
- the result of the static method
- Throws:
IllegalArgumentException- ifstaticMethodisnullor not a static methodInvocationTargetException- if the underlying method throws an exception.SecurityException- the security exception
-
invokeStaticMethod
public static Object invokeStaticMethod(Method staticMethod) throws InvocationTargetException, IllegalArgumentException, SecurityException invoke static Method- Parameters:
staticMethod- the static method- Returns:
- the result of the static method
- Throws:
InvocationTargetException- if the underlying method throws an exception.IllegalArgumentException- the illegal argument exceptionSecurityException- the security exception
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> targetClass, String staticMethodName, Class<?>[] parameterTypes, Object... args) throws IllegalArgumentException, NoSuchMethodException, InvocationTargetException, SecurityException invoke static Method- Parameters:
targetClass- the target classstaticMethodName- the static method nameparameterTypes- the parameter typesargs- the args- Returns:
- the result of the static method
- Throws:
IllegalArgumentException- iftargetClassisnullNullPointerException- ifmethodNameisnullNoSuchMethodException- the no such method exceptionInvocationTargetException- if the underlying method throws an exception.SecurityException- the security exception
-
invokeStaticMethod
public static Object invokeStaticMethod(Class<?> targetClass, String staticMethodName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, InvocationTargetException invoke static Method- Parameters:
targetClass- the target classstaticMethodName- the static method name- Returns:
- the result of the static method
- Throws:
IllegalArgumentException- iftargetClassisnullNullPointerException- ifmethodNameisnullNoSuchMethodException- the no such method exceptionInvocationTargetException- if the underlying method throws an exception.SecurityException- the security exception
-
equalsMethod
Equals method- Parameters:
m1- the method 1m2- the method 2- Returns:
- the boolean
-
containsMethod
Contains method- Parameters:
methods- the methodsmethod- the method- Returns:
- the boolean
-
containsMethod
Contains method- Parameters:
methodMap- the method mapmethod- the method- Returns:
- the boolean
-
hasMethod
Has method- Parameters:
clazz- the classmethod- the method- Returns:
- the boolean
-
getAnnotation
@Nullable public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) get annotation from the method or super class method- Type Parameters:
T- the annotation type- Parameters:
method- the methodannotationClass- the annotation class- Returns:
- the annotation
-
getAnnotationValues
@NonNull public static Map<String,Object> getAnnotationValues(Annotation annotation) throws NoSuchFieldException get annotation values- Parameters:
annotation- the annotation- Returns:
- annotationValues the annotation values
- Throws:
IllegalArgumentException- ifannotationisnullNoSuchFieldException- the no such field exception
-
getAnnotationValue
@Nullable public static <T> T getAnnotationValue(Annotation annotation, String fieldName) throws NoSuchFieldException get annotation value- Type Parameters:
T- the type of the field- Parameters:
annotation- the annotationfieldName- the field name- Returns:
- annotationValue the field value
- Throws:
IllegalArgumentException- ifannotationorfieldNameisnullNoSuchFieldException- the no such field exception
-
getSingleton
get singleton for the class- Type Parameters:
T- the type- Parameters:
clazz- the clazz- Returns:
- the singleton
- Throws:
IllegalArgumentException- ifclazzis null
-
classToString
class to string- Parameters:
clazz- the class- Returns:
- the string
-
fieldToString
field to string- Parameters:
clazz- the clazzfieldName- the field namefieldType- the field type- Returns:
- the string
-
fieldToString
field to string- Parameters:
field- the field- Returns:
- the string
-
methodToString
method to string- Parameters:
clazz- the clazzmethodName- the method nameparameterTypes- the parameter types- Returns:
- the string
-
methodToString
method to string- Parameters:
method- the method- Returns:
- the string
-
annotationToString
annotatio to string- Parameters:
annotation- the annotation- Returns:
- the string
-
parameterTypesToString
parameter types to string- Parameters:
parameterTypes- the parameter types- Returns:
- the string
-