public class ReflectUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
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() |
| 限定符和类型 | 方法和说明 |
|---|---|
static boolean |
compareDeclarations(Method first,
Method second)
Compares method declarations: signature and return types.
|
static boolean |
compareParameters(Class[] first,
Class[] second)
Compares method or ctor parameters.
|
static boolean |
compareSignatures(Constructor<?> first,
Constructor<?> second)
Compares constructor signatures: names and parameters.
|
static boolean |
compareSignatures(Field first,
Field second) |
static boolean |
compareSignatures(Method first,
Method second)
Compares method signatures: names and parameters.
|
static Method |
findDeclaredMethod(Class<?> c,
String methodName) |
static Method |
findMethod(Class<?> c,
String methodName)
Returns method from an object, matched by name.
|
static void |
forceAccess(AccessibleObject accObject)
Suppress access check against a reflection object.
|
static Field[] |
getAccessibleFields(Class<?> clazz) |
static Field[] |
getAccessibleFields(Class<?> clazz,
Class<Object> limit) |
static Method[] |
getAccessibleMethods(Class<?> clazz)
Returns array of all methods that are accessible from given class.
|
static Method[] |
getAccessibleMethods(Class<?> clazz,
Class<Object> limit)
Returns array of all methods that are accessible from given class, upto limit
(usually
Object.class). |
static String |
getBeanPropertyGetterName(Method method)
Returns beans property getter name or
null if method is not a real getter. |
static String |
getBeanPropertySetterName(Method method)
Returns beans property setter name or
null if method is not a real setter. |
static Class[] |
getClasses(Object... objects)
Returns classes from array of specified objects.
|
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)
Invokes private
Class.getMethod0() without throwing NoSuchMethodException. |
static Class[] |
getSuperclasses(Class<?> type)
Returns all superclasses.
|
static Field[] |
getSupportedFields(Class<?> clazz) |
static Field[] |
getSupportedFields(Class<?> clazz,
Class<Object> limit) |
static Method[] |
getSupportedMethods(Class<?> clazz) |
static Method[] |
getSupportedMethods(Class<?> clazz,
Class<Object> limit)
Returns a
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. |
static Object |
invoke(Class<?> c,
Object obj,
String method,
Class[] paramClasses,
Object[] params)
Invokes accessible method of an object.
|
static Object |
invoke(Class<?> c,
Object obj,
String method,
Object[] params) |
static Object |
invoke(Object obj,
String method,
Class[] paramClasses,
Object[] params)
Invokes accessible method of an object.
|
static Object |
invoke(Object obj,
String method,
Object[] params)
Invokes accessible method of an object without specifying parameter types.
|
static Object |
invokeDeclared(Class<?> c,
Object obj,
String method,
Class[] paramClasses,
Object[] params)
Invokes any method of a class, even private ones.
|
static Object |
invokeDeclared(Class<?> c,
Object obj,
String method,
Object[] params) |
static Object |
invokeDeclared(Object obj,
String method,
Class[] paramClasses,
Object[] params)
Invokes any method of a class suppressing java access checking.
|
static Object |
invokeDeclared(Object obj,
String method,
Object[] params) |
static boolean |
isAssignableFrom(Member member1,
Member member2)
Returns
true if the first member is accessible from second one. |
static boolean |
isBeanProperty(Method method)
Returns
true if method is a bean property. |
static boolean |
isBeanPropertyGetter(Method method)
Returns
true if method is bean getter. |
static boolean |
isBeanPropertySetter(Method method)
Returns
true if method is bean setter. |
static boolean |
isInstanceOf(Object o,
Class<?> target)
Dynamic version of
instanceof. |
static boolean |
isInterfaceImpl(Class<?> thisClass,
Class<?> targetInterface)
Returns
true if provided class is interface implementation. |
static boolean |
isObjectMethod(Method method)
Returns
true if method defined in Object class. |
static boolean |
isPublic(Class<?> c)
Returns
true if class is public. |
static boolean |
isPublic(Member member)
Returns
true if class member is public. |
static boolean |
isPublicPublic(Member member)
Returns
true if class member is public and if its declaring class is also public. |
static boolean |
isSubclass(Class<?> thisClass,
Class<?> target)
Determines if first class match the destination and simulates kind
of
instanceof. |
static boolean |
isUserDefinedMethod(Method method)
Returns
true if method is user defined and not defined in Object class. |
static Object |
newInstance(Class<?> type)
Creates new instances including for common mutable classes that do not have a default constructor.
|
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
public static Method getMethod0(Class<?> c, String name, Class<?>... parameterTypes)
Class.getMethod0() without throwing NoSuchMethodException.
Returns only public methods or null if method not found. Since no exception is
throwing, it works faster.c - class to inspectname - name of method to findparameterTypes - parameter typespublic static Method findMethod(Class<?> c, String methodName)
c - class to examinemethodName - Full name of the method.public static Class[] getClasses(Object... objects)
public static Object invoke(Class<?> c, Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
c - class that contains methodobj - object to executemethod - method to invokeparamClasses - classes of parametersparams - parametersIllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invoke(Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - objectmethod - name of the objects methodparams - method parametersparamClasses - method parameter typesIllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invoke(Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - objectmethod - method of an objectparams - method parametersIllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invoke(Class<?> c, Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
public static Object invokeDeclared(Class<?> c, Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
c - class to examineobj - object to inspectmethod - method to invokeparamClasses - parameter typesparams - parametersIllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invokeDeclared(Object obj, String method, Class[] paramClasses, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
obj - object to inspectmethod - method to invokeparamClasses - parameter typesparams - parametersIllegalAccessExceptionNoSuchMethodExceptionInvocationTargetExceptionpublic static Object invokeDeclared(Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
public static Object invokeDeclared(Class<?> c, Object obj, String method, Object[] params) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
public static boolean isSubclass(Class<?> thisClass, Class<?> target)
instanceof. All subclasses and interface of first class
are examined against second class. Method is not symmetric.public static boolean isInterfaceImpl(Class<?> thisClass, Class<?> targetInterface)
true if provided class is interface implementation.public static boolean isInstanceOf(Object o, Class<?> target)
instanceof.o - object to matchtarget - target classtrue if object is an instance of target classpublic static Method[] getAccessibleMethods(Class<?> clazz)
public static Method[] getAccessibleMethods(Class<?> clazz, Class<Object> limit)
Object.class). Abstract methods are ignored.public static Method[] getSupportedMethods(Class<?> clazz, Class<Object> limit)
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.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)
public static void forceAccess(AccessibleObject accObject)
public static boolean isPublic(Member member)
true if class member is public.public static boolean isPublicPublic(Member member)
true if class member is public and if its declaring class is also public.public static boolean isPublic(Class<?> c)
true if class is public.public static Object newInstance(Class<?> type) throws IllegalAccessException, InstantiationException
if blocks
is faster then using a HashMap.public static boolean isAssignableFrom(Member member1, Member member2)
true if the first member is accessible from second one.public static boolean isUserDefinedMethod(Method method)
true if method is user defined and not defined in Object class.public static boolean isObjectMethod(Method method)
true if method defined in Object class.public static boolean isBeanProperty(Method method)
true if method is a bean property.public static boolean isBeanPropertyGetter(Method method)
true if method is bean getter.public static String getBeanPropertyGetterName(Method method)
null if method is not a real getter.public static boolean isBeanPropertySetter(Method method)
true if method is bean setter.public static String getBeanPropertySetterName(Method method)
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 © 2016. All rights reserved.