public class ReflectUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
private static Method |
_getMethod0 |
static String |
METHOD_GET_PREFIX |
static String |
METHOD_IS_PREFIX |
static String |
METHOD_SET_PREFIX |
static Object[] |
NO_ARGUMENTS
an empty object array
|
static Class<?>[] |
NO_PARAMETERS
an empty class array
|
static Type[] |
NO_TYPES
an empty object array
|
| 构造器和说明 |
|---|
ReflectUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
private static void |
addFieldIfNotExist(List<Field> allFields,
Field newField) |
private static void |
addMethodIfNotExist(List<Method> allMethods,
Method newMethod) |
static boolean |
compareDeclarations(Method first,
Method second)
判断两个方法是否完全一样,包括方法名字、参数列表和返回值类型
|
static boolean |
compareParameters(Class<?>[] first,
Class<?>[] second)
判断方法或者构造方法的参数类型数组是否相同
|
static boolean |
compareSignatures(Constructor<?> first,
Constructor<?> second)
判断两个构造函数是否相同,包括名字和参数列表
|
static boolean |
compareSignatures(Field first,
Field second)
判断两个属性的名字是否相同
|
static boolean |
compareSignatures(Method first,
Method second)
判断两个方法的签名是否相同,包括方法的名字和参数列表
|
static Method |
findDeclaredMethod(Class<?> c,
String methodName)
根据给定的方法名字,从所有方法中查找。
|
private static Method |
findDeclaredMethod(Class<?> c,
String methodName,
boolean publicOnly)
根据给定的方法名字,从public方法或者所有方法中查找。
|
static Method |
findMethod(Class<?> c,
String methodName)
根据给定的方法名字,从public方法中查找。
|
static void |
forceAccess(AccessibleObject accObject)
检查对象是否可以访问,如果不能,设置为可以访问。
|
static Field[] |
getAccessibleFields(Class<?> clazz)
返回给定类的所有可以访问的属性,直到
Object.class |
static Field[] |
getAccessibleFields(Class<?> clazz,
Class<?> limit)
返回给定类的所有可以访问的属性,直到给定的超类limit
|
static Method[] |
getAccessibleMethods(Class<?> clazz)
返回给定类的所有方法数组
|
static Method[] |
getAccessibleMethods(Class<?> clazz,
Class<?> limit)
返回给定类的所有可以访问的方法,直到
Object.class类。 |
static Map<String,Object> |
getBeanKVMap(Object bean)
反射获取bean的key和value的map
|
static String |
getBeanPropertyGetterName(Method method)
返回bean中getter方法所对应的名字。
|
private static int |
getBeanPropertyGetterPrefixLength(Method method) |
static String |
getBeanPropertySetterName(Method method)
返回bean属性setter方法所对应的属性的名字。
|
private static int |
getBeanPropertySetterPrefixLength(Method method) |
static Class<?>[] |
getClasses(Object... objects)
返回对象的Class数组。
|
static Class<?> |
getComponentType(Type type)
Returns component type of the given
type. |
static Class<?> |
getComponentType(Type type,
int index)
Returns the component type of the given
type. |
static Class<?> |
getGenericSupertype(Class<?> type) |
static Class<?> |
getGenericSupertype(Class<?> type,
int index) |
static Method |
getMethod0(Class<?> c,
String name,
Class<?>... parameterTypes)
调用
Class.getMethod0()私有方法,获取类中的方法。 |
static Class<?>[] |
getSuperclasses(Class<?> type)
返回当前类的所有超类Class数组
|
static Field[] |
getSupportedFields(Class<?> clazz)
返回给定类的所有的属性,直到
Object.class类 |
static Field[] |
getSupportedFields(Class<?> clazz,
Class<?> limit)
返回给定类的所有的属性,直到给定的超类limit
|
static Method[] |
getSupportedMethods(Class<?> clazz)
返回给定类的所有的方法,直到
Object.class |
static Method[] |
getSupportedMethods(Class<?> clazz,
Class<?> limit)
返回从clazz到超类limit所有的方法数组。
|
static Object |
invoke(Class<?> c,
Object obj,
String method,
Class<?>[] paramClasses,
Object[] params)
调用对象中的可访问方法
|
static Object |
invoke(Class<?> c,
Object obj,
String method,
Object[] params)
调用对象中指定的方法,不需要给定参数类型
|
static Object |
invoke(Object obj,
String method,
Class<?>[] paramClasses,
Object[] params)
调用对象中的可访问方法
|
static Object |
invoke(Object obj,
String method,
Object[] params)
调用对象中指定的方法,不需要给定参数类型
|
static Object |
invokeDeclared(Class<?> c,
Object obj,
String method,
Class<?>[] paramClasses,
Object[] params)
调用对象中的任意方法,包括private方法
|
static Object |
invokeDeclared(Class<?> c,
Object obj,
String method,
Object[] params)
忽略Java访问控制,调用类中的任意方法
|
static Object |
invokeDeclared(Object obj,
String method,
Class<?>[] paramClasses,
Object[] params)
忽略Java访问控制,调用类中的任意方法
|
static Object |
invokeDeclared(Object obj,
String method,
Object[] params)
忽略Java访问控制,调用类中的任意方法
|
static boolean |
isAssignableFrom(Member member1,
Member member2)
表示第一个Memeber是否从第二个Member中访问。
|
static boolean |
isBeanProperty(Method method)
判断方法是否是bean的属性访问器,即是否是getter和setter方法
如果是返回
true |
static boolean |
isBeanPropertyGetter(Method method)
判断方法是否是bean的getter方法
如果是bean的getter方法返回
true |
static boolean |
isBeanPropertySetter(Method method)
判断给定的方法是否是bean的setter方法,如果是返回
true |
static boolean |
isInstanceOf(Object o,
Class<?> target)
instanceof的动态实现 |
static boolean |
isInterfaceImpl(Class<?> thisClass,
Class<?> targetInterface)
如果thisClass是targetInterface接口的实现类,返回
true |
static boolean |
isObjectMethod(Method method)
判断方法是否是用户定义的方法,而不是在
Object中定义的方法。 |
static boolean |
isPublic(Class<?> c)
如果当前Class是共有的,那么返回
true |
static boolean |
isPublic(Member member)
判断反射中用来描述成员变量、方法和构造方法访问特性的Member对象中描述的是否是共有。
|
static boolean |
isPublicPublic(Member member)
判断反射中用来描述成员变量、方法和构造方法访问特性的Member对象中描述的是否是共有。
|
static boolean |
isSubclass(Class<?> thisClass,
Class<?> target)
判断第一个参数Class是否是第二个参数的子类或者接口的实现类。
|
static boolean |
isUserDefinedMethod(Method method)
判断方法是否是用户定义的方法,而不是在
Object中定义的方法。 |
static Object |
newInstance(Class<?> type)
创建新的实例,包括创建没有默认构造函数的可变类。
|
static Object |
readAnnotationValue(Annotation annotation,
String name)
Reads annotation value.
|
static Class<?> |
toClass(Type type)
Returns
Class for the given type. |
public static final Class<?>[] NO_PARAMETERS
public static final Object[] NO_ARGUMENTS
public static final Type[] NO_TYPES
private static Method _getMethod0
public static Method getMethod0(Class<?> c, String name, Class<?>... parameterTypes)
Class.getMethod0()私有方法,获取类中的方法。不会抛出NoSuchMethodException异常。
返回指定的public方法或者没找到返回null。由于没有异常抛出,所以性能良好。c - 需要处理的Classname - 需要查找的方法名字parameterTypes - 需要查找方法的参数类型public static Method findMethod(Class<?> c, String methodName)
c - 需要获取方法的类methodName - 方法的全名public static Method findDeclaredMethod(Class<?> c, String methodName)
c - 需要获取方法的类methodName - 方法的全名private static Method findDeclaredMethod(Class<?> c, String methodName, boolean publicOnly)
c - 需要获取方法的类methodName - 方法的全名publicOnly - 是否在public范文内查找,还是在所有方法范围内查找public static Class<?>[] getClasses(Object... objects)
objects,需要获取Class的对象 - public static Object invoke(Class<?> c, Object obj, String method, Class<?>[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
c - 含有方法的Classobj - 需要执行方法的对象method - 需要调用的方法paramClasses - 参数类型Class数组params - 参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invoke(Object obj, String method, Class<?>[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - 需要执行方法的对象method - 需要调用的方法paramClasses - 参数类型Class数组params - 参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invoke(Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - 需要执行方法的对象method - 需要调用的方法params - 参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invoke(Class<?> c, Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
c - 对象的类型Classobj - 需要执行方法的对象method - 需要调用的方法params - 参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invokeDeclared(Class<?> c, Object obj, String method, Class<?>[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
c - 对象的类型Classobj - 需要执行方法的对象method - 需要调用的方法paramClasses - 方法的参数类型params - 方法的参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invokeDeclared(Object obj, String method, Class<?>[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - 需要执行方法的对象method - 需要调用的方法paramClasses - 方法的参数类型params - 方法的参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invokeDeclared(Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - 需要执行方法的对象method - 需要调用的方法params - 方法的参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invokeDeclared(Class<?> c, Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
c - 对象的类型Classobj - 需要执行方法的对象method - 需要调用的方法params - 方法的参数列表IllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static boolean isSubclass(Class<?> thisClass, Class<?> target)
instanceof进行判断。
方法不具有对称性thisClass - 需要检查的类target - 判断thisClass是否instanceof这个target类public static boolean isInterfaceImpl(Class<?> thisClass, Class<?> targetInterface)
truepublic static boolean isInstanceOf(Object o, Class<?> target)
instanceof的动态实现o - 需要检查的对象target - 检查的类truepublic static Method[] getAccessibleMethods(Class<?> clazz, Class<?> limit)
Object.class类。抽象方法被忽略private static void addMethodIfNotExist(List<Method> allMethods, Method newMethod)
public static Field[] getAccessibleFields(Class<?> clazz)
Object.classpublic static Field[] getAccessibleFields(Class<?> clazz, Class<?> limit)
clazz,当前类 - limit,限制到的超类 - public static Method[] getSupportedMethods(Class<?> clazz)
Object.classclazz,当前类 - public static Method[] getSupportedMethods(Class<?> clazz, Class<?> limit)
java.lang.Object中的方法,如果是null,那么返回所有方法Method array of the methods to which instances of the specified
respond except for those methods defined in the class specified by limit
or any of its superclasses. Note that limit is usually used to eliminate
them methods defined by java.lang.Object. If limit is null then all
methods are returned.clazz,当前类 - limit,限制到的超类 - public static Field[] getSupportedFields(Class<?> clazz)
Object.class类clazz,当前类 - public static Field[] getSupportedFields(Class<?> clazz, Class<?> limit)
clazz,当前类 - limit,限制类 - public static boolean compareDeclarations(Method first, Method second)
public static boolean compareSignatures(Method first, Method second)
public static boolean compareSignatures(Constructor<?> first, Constructor<?> second)
public static boolean compareParameters(Class<?>[] first, Class<?>[] second)
first,第一个参数列表 - second,第二个参数列表 - public static void forceAccess(AccessibleObject accObject)
public static boolean isPublic(Member member)
public static boolean isPublicPublic(Member member)
member,描述成员变量、方法和构造方法的访问相关属性的对象 - truepublic static boolean isPublic(Class<?> c)
truepublic static Object newInstance(Class<?> type) throws IllegalAccessException, InstantiationException
if方式实现,比使用HashMap要快public static boolean isAssignableFrom(Member member1, Member member2)
truepublic static boolean isUserDefinedMethod(Method method)
Object中定义的方法。
如果方法不是Object中的方法会返回truepublic static boolean isObjectMethod(Method method)
Object中定义的方法。
如果方法是Object中的方法会返回truepublic static boolean isBeanProperty(Method method)
truepublic static boolean isBeanPropertyGetter(Method method)
trueprivate static int getBeanPropertyGetterPrefixLength(Method method)
public static String getBeanPropertyGetterName(Method method)
null if method is not a real getter.public static boolean isBeanPropertySetter(Method method)
trueprivate static int getBeanPropertySetterPrefixLength(Method method)
public static String getBeanPropertySetterName(Method method)
null
Returns beans property setter name or null if method is not a real setter.public static Class<?> getComponentType(Type type)
type.
For ParameterizedType it returns the last type in array.public static Class<?> getComponentType(Type type, int index)
type.type - is the type where to get the component type from.type or
null if the given type does NOT have
a single (component) type.public static Class<?> toClass(Type type)
Class for the given type.type |
getSimpleType(type) |
|---|---|
String |
String |
List<String> |
List |
<T extends MyClass> T[] |
MyClass[] |
type - is the type to convert.type.public static Object readAnnotationValue(Annotation annotation, String name)
null on error.Copyright © 2023. All rights reserved.