public abstract class ReflectionUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static Field |
NULL_FIELD
The NULL_FIELD
|
static Method |
NULL_METHOD
The NULL_METHOD
|
| Constructor and Description |
|---|
ReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
annotationToString(Annotation annotation)
annotatio to string
|
static String |
classToString(Class<?> clazz)
class to string
|
static boolean |
containsMethod(Collection<Method> methods,
Method method)
Contains method
|
static boolean |
containsMethod(Map<Method,?> methodMap,
Method method)
Contains method
|
static boolean |
equalsMethod(Method m1,
Method m2)
Equals method
|
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 <T extends Annotation> |
getAnnotation(Method method,
Class<T> annotationClass)
get annotation from the method or super class method
|
static <T> T |
getAnnotationValue(Annotation annotation,
String fieldName)
get annotation value
|
static Map<String,Object> |
getAnnotationValues(Annotation annotation)
get annotation values
|
static Class<?> |
getClassByName(String className)
Gets class by name.
|
static Class<?> |
getClassByName(String className,
ClassLoader loader)
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 <T> T |
getSingleton(Class<T> clazz)
get singleton for the class
|
static boolean |
hasField(Class<?> clazz,
String fieldName)
has Field
|
static boolean |
hasMethod(Class<?> clazz,
Method method)
Has 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 String |
parameterTypesToString(Class<?>[] parameterTypes)
parameter types to string
|
static <T extends AccessibleObject> |
setAccessible(T accessible)
set accessible to true if false
|
static void |
setFieldValue(Object target,
Field field,
Object fieldValue)
set field value
|
static void |
setFieldValue(Object target,
String fieldName,
Object fieldValue)
get field value
|
static void |
setStaticFieldValue(Class<?> targetClass,
String staticFieldName,
Object newValue)
set `static` field value
|
static void |
setStaticFieldValue(Field staticField,
Object newValue)
set `static` field value
|
public static final Field NULL_FIELD
public static final Method NULL_METHOD
@NonNull public static Class<?> getClassByName(String className, ClassLoader loader) throws ClassNotFoundException
className - the class nameloader - the class loaderClassNotFoundException - the class not found exception@NonNull public static Class<?> getClassByName(String className) throws ClassNotFoundException
className - the class nameClassNotFoundException - the class not found exception@NonNull public static Set<Class<?>> getInterfaces(Class<?> clazz)
clazz - the clazzpublic static <T extends AccessibleObject> void setAccessible(T accessible) throws SecurityException
T - the type of the accessibleaccessible - the accessibleSecurityException - if the request is denied.@NonNull public static Field[] getAllFields(Class<?> targetClazz)
targetClazz - the target classpublic static boolean hasField(Class<?> clazz, String fieldName)
clazz - the classfieldName - the field name@NonNull public static Field getField(Class<?> clazz, String fieldName) throws NoSuchFieldException, SecurityException
clazz - the classfieldName - the field nameIllegalArgumentException - if clazz or fieldName is nullNoSuchFieldException - if the field named fieldName does not existSecurityException - the security exception@Nullable public 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 type@Nullable public static <T> T getFieldValue(Object target, String fieldName) throws IllegalArgumentException, NoSuchFieldException, SecurityException
T - the field typetarget - the targetfieldName - the field nameIllegalArgumentException - if target or fieldName 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 or fieldName is nullNoSuchFieldException - if the field named fieldName does not existSecurityException - the security exceptionpublic static void setStaticFieldValue(Field staticField, Object newValue) throws IllegalAccessException
staticField - the static fieldnewValue - the new valueIllegalArgumentException - if staticField is null or not a static fieldIllegalAccessException - the illegal access exceptionpublic static void setStaticFieldValue(Class<?> targetClass, String staticFieldName, Object newValue) throws NoSuchFieldException, IllegalAccessException
targetClass - the target classstaticFieldName - the static field namenewValue - the new valueIllegalArgumentException - if targetClass or staticFieldName is nullNullPointerException - if staticFieldName is nullNoSuchFieldException - if the field named modifyFieldName does not existIllegalAccessException - the illegal access exception@NonNull public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
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 exception@NonNull public static Method getMethod(Class<?> clazz, String methodName) throws NoSuchMethodException, SecurityException
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 boolean equalsMethod(Method m1, Method m2)
m1 - the method 1m2 - the method 2public static boolean containsMethod(Collection<Method> methods, Method method)
methods - the methodsmethod - the methodpublic static boolean containsMethod(Map<Method,?> methodMap, Method method)
methodMap - the method mapmethod - the methodpublic static boolean hasMethod(Class<?> clazz, Method method)
clazz - the classmethod - the method@Nullable public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass)
T - the annotation typemethod - the methodannotationClass - the annotation class@NonNull public static Map<String,Object> getAnnotationValues(Annotation annotation) throws NoSuchFieldException
annotation - the annotationIllegalArgumentException - if annotation is nullNoSuchFieldException - the no such field exception@Nullable public static <T> T getAnnotationValue(Annotation annotation, String fieldName) throws NoSuchFieldException
T - the type of the fieldannotation - the annotationfieldName - the field nameIllegalArgumentException - if annotation or fieldName is nullNoSuchFieldException - the no such field exception@NonNull public static <T> T getSingleton(Class<T> clazz)
T - the typeclazz - the clazzIllegalArgumentException - if clazz is nullpublic 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 © 2021–2022 EasyJ开源社区. All rights reserved.