public final class ReflectionUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static Object[] |
EMPTY_ARGS
The constant EMPTY_ARGS
|
static Class<?>[] |
EMPTY_CLASS_ARRAY
The constant EMPTY_CLASS_ARRAY
|
static Field[] |
EMPTY_FIELD_ARRAY
The constant EMPTY_FIELD_ARRAY
|
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
annotationToString(Annotation annotation)
annotatio to string
|
static String |
classToString(Class<?> clazz)
class to string
|
static String |
fieldToString(Class<?> clazz,
String fieldName,
Class<?> fieldType)
field to string
|
static String |
fieldToString(Field field)
field to string
|
static Field[] |
getAllFields(Class<?> targetClazz)
Gets all fields, excluding static or synthetic fields
|
static Map<String,Object> |
getAnnotationValues(Annotation annotation)
get annotation values
|
static Class<?> |
getClassByName(String className)
Gets class by name.
|
static Field |
getField(Class<?> clazz,
String fieldName)
get Field
|
static <T> T |
getFieldValue(Object target,
Field field)
get field value
|
static <T> T |
getFieldValue(Object target,
String fieldName)
get field value
|
static Set<Class<?>> |
getInterfaces(Class<?> clazz)
get all interface of the clazz
|
static Method |
getMethod(Class<?> clazz,
String methodName)
get method.
|
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
get method.
|
static Object |
invokeMethod(Object target,
Method method)
invoke Method
|
static Object |
invokeMethod(Object target,
Method method,
Object... args)
invoke Method
|
static Object |
invokeMethod(Object target,
String methodName)
invoke Method
|
static Object |
invokeMethod(Object target,
String methodName,
Class<?>[] parameterTypes,
Object... args)
invoke Method
|
static Object |
invokeStaticMethod(Class<?> targetClass,
String staticMethodName)
invoke static Method
|
static Object |
invokeStaticMethod(Class<?> targetClass,
String staticMethodName,
Class<?>[] parameterTypes,
Object... args)
invoke static Method
|
static Object |
invokeStaticMethod(Method staticMethod)
invoke static Method
|
static Object |
invokeStaticMethod(Method staticMethod,
Object... args)
invoke static Method
|
static String |
methodToString(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
method to string
|
static String |
methodToString(Method method)
method to string
|
static void |
modifyStaticFinalField(Class<?> targetClass,
String staticFieldName,
Object newValue)
modify `static` or `static final` field value
|
static void |
modifyStaticFinalField(Field staticField,
Object newValue)
modify `static` or `static final` field value
In java17, this method cannot be used for final fields.
|
static String |
parameterTypesToString(Class<?>[] parameterTypes)
parameter types to string
|
static void |
setFieldValue(Object target,
Field field,
Object fieldValue)
set field value
|
static void |
setFieldValue(Object target,
String fieldName,
Object fieldValue)
get field value
|
public static final Field[] EMPTY_FIELD_ARRAY
public static final Class<?>[] EMPTY_CLASS_ARRAY
public static final Object[] EMPTY_ARGS
public static Class<?> getClassByName(String className) throws ClassNotFoundException
className - the class nameClassNotFoundException - the class not found exceptionpublic static Set<Class<?>> getInterfaces(Class<?> clazz)
clazz - the clazzpublic static Field[] getAllFields(Class<?> targetClazz)
targetClazz - the target classpublic static Field getField(Class<?> clazz, String fieldName) throws NoSuchFieldException, SecurityException
clazz - the classfieldName - the field nameNoSuchFieldException - if the field named fieldName does not existSecurityException - the security exceptionpublic static <T> T getFieldValue(Object target, Field field) throws IllegalArgumentException, SecurityException
T - the field typetarget - the targetfield - the field of the targetIllegalArgumentException - if target is nullSecurityException - the security exceptionClassCastException - if the type of the variable receiving the field value is not equals to the field typepublic static <T> T getFieldValue(Object target, String fieldName) throws IllegalArgumentException, NoSuchFieldException, SecurityException
T - the field typetarget - the targetfieldName - the field nameIllegalArgumentException - if target is nullNoSuchFieldException - if the field named fieldName does not existSecurityException - the security exceptionClassCastException - if the type of the variable receiving the field value is not equals to the field typepublic static void setFieldValue(Object target, Field field, Object fieldValue) throws IllegalArgumentException, SecurityException
target - the targetfield - the field of the targetfieldValue - the field valueIllegalArgumentException - if target is nullSecurityException - the security exceptionpublic static void setFieldValue(Object target, String fieldName, Object fieldValue) throws IllegalArgumentException, NoSuchFieldException, SecurityException
target - the targetfieldName - the field namefieldValue - the field valueIllegalArgumentException - if target is nullNoSuchFieldException - if the field named fieldName does not existSecurityException - the security exceptionpublic static void modifyStaticFinalField(Field staticField, Object newValue) throws NoSuchFieldException, IllegalAccessException
In java17, this method cannot be used for final fields.
staticField - the static fieldnewValue - the new valueIllegalArgumentException - if staticField is null or not a static fieldNoSuchFieldException - if the class of the staticField has no `modifiers` fieldIllegalAccessException - the illegal access exceptionpublic static void modifyStaticFinalField(Class<?> targetClass, String staticFieldName, Object newValue) throws NoSuchFieldException, IllegalAccessException
targetClass - the target classstaticFieldName - the static field namenewValue - the new valueIllegalArgumentException - if targetClass is nullNullPointerException - if staticFieldName is nullNoSuchFieldException - if the field named modifyFieldName does not existIllegalAccessException - the illegal access exceptionpublic static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
Class.getMethod(String, Class[]).clazz - the classmethodName - the method nameparameterTypes - the parameter typesIllegalArgumentException - if clazz is nullNullPointerException - if methodName is nullNoSuchMethodException - if the method named methodName does not existSecurityException - the security exceptionpublic static Method getMethod(Class<?> clazz, String methodName) throws NoSuchMethodException, SecurityException
Class.getMethod(String, Class[]).clazz - the classmethodName - the method nameIllegalArgumentException - if clazz is nullNullPointerException - if methodName is nullNoSuchMethodException - if the method named methodName does not existSecurityException - the security exceptionpublic static Object invokeMethod(Object target, Method method, Object... args) throws InvocationTargetException, IllegalArgumentException, SecurityException
target - the targetmethod - the methodargs - the argsInvocationTargetException - if the underlying method throws an exception.IllegalArgumentException - the illegal argument exceptionSecurityException - the security exceptionpublic static Object invokeMethod(Object target, Method method) throws InvocationTargetException, IllegalArgumentException, SecurityException
target - the targetmethod - the methodInvocationTargetException - if the underlying method throws an exception.IllegalArgumentException - the illegal argument exceptionSecurityException - the security exceptionpublic static Object invokeMethod(Object target, String methodName, Class<?>[] parameterTypes, Object... args) throws NoSuchMethodException, InvocationTargetException, IllegalArgumentException, SecurityException
target - the targetmethodName - the method nameparameterTypes - the parameter typesargs - the argsIllegalArgumentException - if target is nullNullPointerException - if methodName is nullNoSuchMethodException - if the method named methodName does not existInvocationTargetException - if the underlying method throws an exception.SecurityException - the security exceptionpublic static Object invokeMethod(Object target, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalArgumentException, SecurityException
target - the targetmethodName - the method nameNoSuchMethodException - the no such method exceptionInvocationTargetException - if the underlying method throws an exception.IllegalArgumentException - the illegal argument exceptionSecurityException - the security exceptionpublic static Object invokeStaticMethod(Method staticMethod, Object... args) throws IllegalArgumentException, InvocationTargetException, SecurityException
staticMethod - the static methodargs - the argsIllegalArgumentException - if staticMethod is null or not a static methodInvocationTargetException - if the underlying method throws an exception.SecurityException - the security exceptionpublic static Object invokeStaticMethod(Method staticMethod) throws InvocationTargetException, IllegalArgumentException, SecurityException
staticMethod - the static methodInvocationTargetException - if the underlying method throws an exception.IllegalArgumentException - the illegal argument exceptionSecurityException - the security exceptionpublic static Object invokeStaticMethod(Class<?> targetClass, String staticMethodName, Class<?>[] parameterTypes, Object... args) throws IllegalArgumentException, NoSuchMethodException, InvocationTargetException, SecurityException
targetClass - the target classstaticMethodName - the static method nameparameterTypes - the parameter typesargs - the argsIllegalArgumentException - if targetClass is nullNullPointerException - if methodName is nullNoSuchMethodException - the no such method exceptionInvocationTargetException - if the underlying method throws an exception.SecurityException - the security exceptionpublic static Object invokeStaticMethod(Class<?> targetClass, String staticMethodName) throws IllegalArgumentException, NoSuchMethodException, SecurityException, InvocationTargetException
targetClass - the target classstaticMethodName - the static method nameIllegalArgumentException - if targetClass is nullNullPointerException - if methodName is nullNoSuchMethodException - the no such method exceptionInvocationTargetException - if the underlying method throws an exception.SecurityException - the security exceptionpublic static Map<String,Object> getAnnotationValues(Annotation annotation) throws NoSuchFieldException
annotation - the annotationNoSuchFieldException - the no such field exceptionpublic static String classToString(Class<?> clazz)
clazz - the classpublic static String fieldToString(Class<?> clazz, String fieldName, Class<?> fieldType)
clazz - the clazzfieldName - the field namefieldType - the field typepublic static String fieldToString(Field field)
field - the fieldpublic static String methodToString(Class<?> clazz, String methodName, Class<?>... parameterTypes)
clazz - the clazzmethodName - the method nameparameterTypes - the parameter typespublic static String methodToString(Method method)
method - the methodpublic static String annotationToString(Annotation annotation)
annotation - the annotationCopyright © 2022 Seata. All rights reserved.