类 ReflectUtil
- java.lang.Object
-
- cc.shacocloud.mirage.utils.reflection.ReflectUtil
-
public class ReflectUtil extends Object
反射工具类- 作者:
- 思追(shaco)
-
-
构造器概要
构造器 构造器 说明 ReflectUtil()
-
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static <T> @Nullable Constructor<T>getConstructor(@NotNull Class<T> clazz, Class<?>... parameterTypes)查找类中的指定参数的构造方法static <T> Constructor<T>[]getConstructors(@NotNull Class<T> beanClass)获得一个类中所有构造列表static FieldgetField(Class<?> beanClass, String name)查找指定类中的指定name的字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回nullstatic Field[]getFields(@NotNull Class<?> beanClass)获得一个类中所有字段列表,包括其父类中的字段
如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。static Field @NotNull []getFieldsDirectly(@NotNull Class<?> beanClass, boolean withSuperClassFields)获得一个类中所有字段列表,直接反射获取,无缓存
如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。static ObjectgetFieldValue(Object obj, Field field)获取字段值static ObjectgetFieldValue(Object obj, String fieldName)获取字段值static @NotNull Member @NotNull []getMembers(@NotNull Class<?> clazz)static @Nullable MethodgetMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes)查找指定方法 如果找不到对应的方法则返回null
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。static @Nullable MethodgetMethod(Class<?> clazz, String methodName, Class<?>... paramTypes)查找指定方法 如果找不到对应的方法则返回nullstatic MethodgetMethodOfObj(Object obj, String methodName, Object... args)查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法static Method[]getMethods(@NotNull Class<?> beanClass)获得一个类中所有方法列表,包括其父类中的方法。static Method[]getMethods(Class<?> clazz, java.util.function.Predicate<Method> filter)获得指定类过滤后的Public方法列表static @NotNull Method[]getMethodsDirectly(@NotNull Class<?> beanClass, boolean withSupers, boolean withMethodFromObject)获得一个类中所有方法列表,直接反射获取,无缓存
接口获取方法和默认方法,获取的方法包括: 本类中的所有方法(包括static方法) 父类中的所有方法(包括static方法) Object中(包括static方法)static @NotNull Method[]getMethodsDirectly(@NotNull Class<?> beanClass, boolean withSupers, boolean withMethodFromObject, @NotNull java.util.function.Function<Method,String> uniqueGenerator)获得一个类中所有方法列表,直接反射获取,无缓存
接口获取方法和默认方法,获取的方法包括: 本类中的所有方法(包括static方法) 父类中的所有方法(包括static方法) Object中(包括static方法)static Method[]getMethodsIncludeOverridden(@NotNull Class<?> beanClass)获得一个类中所有方法列表,包括其父类中的方法,包含被覆盖的方法static booleanhasOverridableAccessModifiers(@NotNull Method method, @NotNull Method candidate)根据方法的修饰符检查该方法是否可以由候选项重写static booleanhasSameName(@NotNull Method method, @NotNull Method candidate)检查方法是否与候选项同名static booleanhasSameParameters(@NotNull Method method, @NotNull Method candidate)检查该方法是否具有与候选项相同的参数数、相同的参数顺序和完全相同的参数类型static @Nullable ObjectinvokeMethod(@NotNull Method method, @Nullable Object target, @Nullable Object... args)当调用一个静态的Method时,目标对象可以是null。static @Nullable ObjectinvokeMethod(Method method, @Nullable Object target)当调用一个静态的Method时,目标对象可以是null。static booleanisEqualsMethod(@Nullable Method method)确定给定的方法是否为 equals 方法static booleanisFinal(@NotNull Member member)检查成员是否为最终成员static booleanisHashCodeMethod(@Nullable Method method)确定给定的方法是否为 hashCode 方法static booleanisOverridden(@NotNull Method method, @NotNull Class<?> clazz)检查该方法是否被指定类中的方法重写static booleanisOverridden(@NotNull Method method, @NotNull Method candidate)检查方法是否已被候选项覆盖static booleanisOverridden(@NotNull Method method, @NotNull Method @NotNull [] candidates)检查该方法是否被候选项列表中的任何方法重写static booleanisPackagePrivate(Member member)检查成员是否为包专用。static booleanisPrivate(@NotNull Member member)检查成员是否为私有成员static booleanisProtected(@NotNull Member member)检查成员是否为保护成员static booleanisPublic(@NotNull Member member)检查成员是否为公共成员static booleanisSamePackage(@NotNull Method method, @NotNull Method candidate)检查该方法是否是与候选项的类位于同一包中的类。static booleanisStatic(@NotNull Member member)检查成员是否为静态成员static booleanisSubClassOf(@NotNull Class<?> potentialSubclass, @NotNull Class<?> potentialSuperclass)检查子类是否是超类的子类之一static booleanisToStringMethod(@Nullable Method method)确定给定的方法是否为 toString 方法static voidmakeAccessible(@NotNull Executable executable)使给定的构造可访问,并在必要时显式设置其可访问性。static <T extends AccessibleObject>
TsetAccessible(@Nullable Object obj, T accessibleObject)设置为可访问(私有可以被外部调用)static voidsetFieldValue(@Nullable Object obj, @NotNull Field field, Object value)设置字段值
-
-
-
方法详细资料
-
getConstructor
@Nullable public static <T> @Nullable Constructor<T> getConstructor(@NotNull @NotNull Class<T> clazz, Class<?>... parameterTypes)
查找类中的指定参数的构造方法- 类型参数:
T- 对象类型- 参数:
clazz- 类parameterTypes- 参数类型,只要任何一个参数是指定参数的父类或接口或相等即可,此参数可以不传- 返回:
- 构造方法,如果未找到返回null
-
getConstructors
public static <T> Constructor<T>[] getConstructors(@NotNull @NotNull Class<T> beanClass)
获得一个类中所有构造列表- 类型参数:
T- 构造的对象类型- 参数:
beanClass- 类- 返回:
- 字段列表
-
getFieldValue
public static Object getFieldValue(Object obj, String fieldName)
获取字段值- 参数:
obj- 对象,如果static字段,此处为类fieldName- 字段名- 返回:
- 字段值
-
getFieldValue
public static Object getFieldValue(Object obj, Field field)
获取字段值- 参数:
obj- 对象,static字段则此字段为nullfield- 字段- 返回:
- 字段值
-
setFieldValue
public static void setFieldValue(@Nullable @Nullable Object obj, @NotNull @NotNull Field field, Object value)设置字段值- 参数:
obj- 对象,如果是static字段,此参数为nullfield- 字段value- 值
-
setAccessible
public static <T extends AccessibleObject> T setAccessible(@Nullable @Nullable Object obj, T accessibleObject)
设置为可访问(私有可以被外部调用)- 类型参数:
T- AccessibleObject的子类,比如Class、Method、Field等- 参数:
accessibleObject- 可设置访问权限的对象,比如Class、Method、Field等- 返回:
- 被设置可访问的对象
-
makeAccessible
public static void makeAccessible(@NotNull @NotNull Executable executable)使给定的构造可访问,并在必要时显式设置其可访问性。
-
getMembers
@NotNull public static @NotNull Member @NotNull [] getMembers(@NotNull @NotNull Class<?> clazz)
-
getField
public static Field getField(Class<?> beanClass, String name) throws SecurityException
查找指定类中的指定name的字段(包括非public字段),也包括父类和Object类的字段, 字段不存在则返回null- 参数:
beanClass- 被查找字段的类,不能为nullname- 字段名- 返回:
- 字段
- 抛出:
SecurityException- 安全异常
-
getFields
public static Field[] getFields(@NotNull @NotNull Class<?> beanClass) throws SecurityException
获得一个类中所有字段列表,包括其父类中的字段
如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。- 参数:
beanClass- 类- 返回:
- 字段列表
- 抛出:
SecurityException- 安全检查异常
-
getFieldsDirectly
public static Field @NotNull [] getFieldsDirectly(@NotNull @NotNull Class<?> beanClass, boolean withSuperClassFields) throws SecurityException
获得一个类中所有字段列表,直接反射获取,无缓存
如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。- 参数:
beanClass- 类withSuperClassFields- 是否包括父类的字段列表- 返回:
- 字段列表
- 抛出:
SecurityException- 安全检查异常
-
getMethods
public static Method[] getMethods(@NotNull @NotNull Class<?> beanClass) throws SecurityException
获得一个类中所有方法列表,包括其父类中的方法。 使用getUniqueKey(Method)来对方法进行去重,即:被覆盖的方法不包含其中- 参数:
beanClass- 类,非null- 返回:
- 方法列表
- 抛出:
SecurityException- 安全检查异常
-
getMethodsIncludeOverridden
public static Method[] getMethodsIncludeOverridden(@NotNull @NotNull Class<?> beanClass) throws SecurityException
获得一个类中所有方法列表,包括其父类中的方法,包含被覆盖的方法- 参数:
beanClass- 类,非null- 返回:
- 方法列表
- 抛出:
SecurityException- 安全检查异常
-
getMethods
public static Method[] getMethods(Class<?> clazz, java.util.function.Predicate<Method> filter) throws SecurityException
获得指定类过滤后的Public方法列表- 参数:
clazz- 查找方法的类filter- 过滤器- 返回:
- 过滤后的方法列表
- 抛出:
SecurityException- 安全异常
-
getMethodsDirectly
@NotNull public static @NotNull Method[] getMethodsDirectly(@NotNull @NotNull Class<?> beanClass, boolean withSupers, boolean withMethodFromObject) throws SecurityException
获得一个类中所有方法列表,直接反射获取,无缓存
接口获取方法和默认方法,获取的方法包括:- 本类中的所有方法(包括static方法)
- 父类中的所有方法(包括static方法)
- Object中(包括static方法)
- 参数:
beanClass- 类或接口withSupers- 是否包括父类或接口的方法列表withMethodFromObject- 是否包括Object中的方法- 返回:
- 方法列表
- 抛出:
SecurityException- 安全检查异常
-
getMethodsDirectly
@NotNull public static @NotNull Method[] getMethodsDirectly(@NotNull @NotNull Class<?> beanClass, boolean withSupers, boolean withMethodFromObject, @NotNull @NotNull java.util.function.Function<Method,String> uniqueGenerator) throws SecurityException
获得一个类中所有方法列表,直接反射获取,无缓存
接口获取方法和默认方法,获取的方法包括:- 本类中的所有方法(包括static方法)
- 父类中的所有方法(包括static方法)
- Object中(包括static方法)
- 参数:
beanClass- 类或接口withSupers- 是否包括父类或接口的方法列表withMethodFromObject- 是否包括Object中的方法uniqueGenerator- 生成方法的唯一键来进行去重- 返回:
- 方法列表
- 抛出:
SecurityException- 安全检查异常
-
getMethodOfObj
public static Method getMethodOfObj(Object obj, String methodName, Object... args) throws SecurityException
查找指定对象中的所有方法(包括非public方法),也包括父对象和Object类的方法此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回
null。- 参数:
obj- 被查找的对象,如果为null返回nullmethodName- 方法名,如果为空字符串返回nullargs- 参数- 返回:
- 方法
- 抛出:
SecurityException- 无访问权限抛出异常
-
getMethod
@Nullable public static @Nullable Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws SecurityException
查找指定方法 如果找不到对应的方法则返回null此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回
null。- 参数:
clazz- 类,如果为null返回nullmethodName- 方法名,如果为空字符串返回nullparamTypes- 参数类型,指定参数类型如果是方法的子类也算- 返回:
- 方法
- 抛出:
SecurityException- 无权访问抛出异常
-
getMethod
@Nullable public static @Nullable Method getMethod(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException
查找指定方法 如果找不到对应的方法则返回null
此方法为精准获取方法名,即方法名和参数数量和类型必须一致,否则返回null。
如果查找的方法有多个同参数类型重载,查找第一个找到的方法- 参数:
clazz- 类,如果为null返回nullignoreCase- 是否忽略大小写methodName- 方法名,如果为空字符串返回nullparamTypes- 参数类型,指定参数类型如果是方法的子类也算- 返回:
- 方法
- 抛出:
SecurityException- 无权访问抛出异常
-
invokeMethod
@Nullable public static @Nullable Object invokeMethod(Method method, @Nullable @Nullable Object target)
当调用一个静态的Method时,目标对象可以是null。
-
invokeMethod
@Nullable public static @Nullable Object invokeMethod(@NotNull @NotNull Method method, @Nullable @Nullable Object target, @Nullable @Nullable Object... args)
当调用一个静态的Method时,目标对象可以是null。
-
isOverridden
public static boolean isOverridden(@NotNull @NotNull Method method, @NotNull @NotNull Class<?> clazz)检查该方法是否被指定类中的方法重写- 参数:
method- 可能被重写的方法clazz- 指定的类- 返回:
- 如果该方法被任何候选项覆盖则返回 true 反之为 false
-
isOverridden
public static boolean isOverridden(@NotNull @NotNull Method method, @NotNull @NotNull Method @NotNull [] candidates)检查该方法是否被候选项列表中的任何方法重写- 参数:
method- 可能被重写的方法candidates- 可能覆盖第一种方法的方法列表。这些方法应与第一种方法(包括继承的方法)来自同一类- 返回:
- 如果该方法被任何候选项覆盖则返回 true 反之为 false
-
isOverridden
public static boolean isOverridden(@NotNull @NotNull Method method, @NotNull @NotNull Method candidate)检查方法是否已被候选项覆盖覆盖规则:
- 候选人必须位于带方法的类的子类中
- 方法名称必须相同
- 参数必须相同。
- 返回类型必须相同或子类
- 访问修饰符必须相同或限制较少
- 方法不能是私有的、最终的或静态的
- 候选人不能是私有或静态的
- 候选项必须与具有默认访问权限的方法位于同一包中。
- 返回:
- 如果该方法被任何候选项覆盖则返回 true 反之为 false
-
hasSameParameters
public static boolean hasSameParameters(@NotNull @NotNull Method method, @NotNull @NotNull Method candidate)检查该方法是否具有与候选项相同的参数数、相同的参数顺序和完全相同的参数类型
-
hasSameName
public static boolean hasSameName(@NotNull @NotNull Method method, @NotNull @NotNull Method candidate)检查方法是否与候选项同名
-
isSubClassOf
public static boolean isSubClassOf(@NotNull @NotNull Class<?> potentialSubclass, @NotNull @NotNull Class<?> potentialSuperclass)检查子类是否是超类的子类之一- 参数:
potentialSubclass- 将检查以确定它是否是潜在超类的子类的类potentialSuperclass- 将检查潜在子类的类
-
hasOverridableAccessModifiers
public static boolean hasOverridableAccessModifiers(@NotNull @NotNull Method method, @NotNull @NotNull Method candidate)根据方法的修饰符检查该方法是否可以由候选项重写私有、静态或最终方法不能被覆盖。仅当两个方法都来自同一包中的类时,才能重写包私有(默认)方法
-
isSamePackage
public static boolean isSamePackage(@NotNull @NotNull Method method, @NotNull @NotNull Method candidate)检查该方法是否是与候选项的类位于同一包中的类。
-
isEqualsMethod
public static boolean isEqualsMethod(@Nullable @Nullable Method method)确定给定的方法是否为 equals 方法- 另请参阅:
Object.equals(Object)
-
isHashCodeMethod
public static boolean isHashCodeMethod(@Nullable @Nullable Method method)确定给定的方法是否为 hashCode 方法- 另请参阅:
Object.hashCode()
-
isToStringMethod
public static boolean isToStringMethod(@Nullable @Nullable Method method)确定给定的方法是否为 toString 方法- 另请参阅:
Object.toString()
-
isStatic
public static boolean isStatic(@NotNull @NotNull Member member)检查成员是否为静态成员
-
isFinal
public static boolean isFinal(@NotNull @NotNull Member member)检查成员是否为最终成员
-
isPrivate
public static boolean isPrivate(@NotNull @NotNull Member member)检查成员是否为私有成员
-
isProtected
public static boolean isProtected(@NotNull @NotNull Member member)检查成员是否为保护成员
-
isPublic
public static boolean isPublic(@NotNull @NotNull Member member)检查成员是否为公共成员
-
isPackagePrivate
public static boolean isPackagePrivate(Member member)
检查成员是否为包专用。也称为默认修饰符,因为不存在修饰符。
-
-