public class ReflectUtil
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
METHOD_GET_PREFIX |
static java.lang.String |
METHOD_IS_PREFIX |
static java.lang.String |
METHOD_SET_PREFIX |
static java.lang.Object[] |
NO_ARGUMENTS
Empty object array.
|
static java.lang.Class[] |
NO_PARAMETERS
Empty class array.
|
static java.lang.reflect.Type[] |
NO_TYPES
Empty type array.
|
| Constructor and Description |
|---|
ReflectUtil() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
compareDeclarations(java.lang.reflect.Method first,
java.lang.reflect.Method second)
Compares method declarations: signature and return types.
|
static boolean |
compareParameters(java.lang.Class[] first,
java.lang.Class[] second)
Compares method or ctor parameters.
|
static boolean |
compareSignatures(java.lang.reflect.Constructor first,
java.lang.reflect.Constructor second)
Compares constructor signatures: names and parameters.
|
static boolean |
compareSignatures(java.lang.reflect.Field first,
java.lang.reflect.Field second) |
static boolean |
compareSignatures(java.lang.reflect.Method first,
java.lang.reflect.Method second)
Compares method signatures: names and parameters.
|
static java.lang.reflect.Method |
findDeclaredMethod(java.lang.Class c,
java.lang.String methodName) |
static java.lang.reflect.Method |
findMethod(java.lang.Class c,
java.lang.String methodName)
Returns method from an object, matched by name.
|
static void |
forceAccess(java.lang.reflect.AccessibleObject accObject)
Suppress access check against a reflection object.
|
static java.lang.reflect.Field[] |
getAccessibleFields(java.lang.Class clazz) |
static java.lang.reflect.Field[] |
getAccessibleFields(java.lang.Class clazz,
java.lang.Class limit) |
static java.lang.reflect.Method[] |
getAccessibleMethods(java.lang.Class clazz)
Returns array of all methods that are accessible from given class.
|
static java.lang.reflect.Method[] |
getAccessibleMethods(java.lang.Class clazz,
java.lang.Class limit)
Returns array of all methods that are accessible from given class, upto limit
(usually
Object.class). |
static java.lang.String |
getBeanPropertyGetterName(java.lang.reflect.Method method)
Returns beans property getter name or
null if method is not a real getter. |
static java.lang.String |
getBeanPropertySetterName(java.lang.reflect.Method method)
Returns beans property setter name or
null if method is not a real setter. |
static java.lang.Class |
getCallerClass(int framesToSkip)
Emulates
Reflection.getCallerClass using standard API. |
static java.lang.Class[] |
getClasses(java.lang.Object... objects)
Returns classes from array of specified objects.
|
static java.lang.Class |
getComponentType(java.lang.reflect.Type type) |
static java.lang.Class |
getComponentType(java.lang.reflect.Type type,
java.lang.Class implClass) |
static java.lang.Class |
getComponentType(java.lang.reflect.Type type,
java.lang.Class implClass,
int index)
Returns the component type of the given type.
|
static java.lang.Class |
getComponentType(java.lang.reflect.Type type,
int index) |
static java.lang.Class |
getGenericSupertype(java.lang.Class type) |
static java.lang.Class |
getGenericSupertype(java.lang.Class type,
int index)
Returns generic supertype for given class and 0-based index.
|
static java.lang.reflect.Method |
getMethod0(java.lang.Class c,
java.lang.String name,
java.lang.Class... parameterTypes)
Invokes private
Class.getMethod0() without throwing NoSuchMethodException. |
static java.lang.Class |
getRawType(java.lang.reflect.Type type)
Returns raw class for given
type. |
static java.lang.Class<?> |
getRawType(java.lang.reflect.Type type,
java.lang.Class implClass)
Returns raw class for given
type when implementation class is known
and it makes difference. |
static java.lang.Class[] |
getSuperclasses(java.lang.Class type)
Returns all superclasses.
|
static java.lang.reflect.Field[] |
getSupportedFields(java.lang.Class clazz) |
static java.lang.reflect.Field[] |
getSupportedFields(java.lang.Class clazz,
java.lang.Class limit) |
static java.lang.reflect.Method[] |
getSupportedMethods(java.lang.Class clazz) |
static java.lang.reflect.Method[] |
getSupportedMethods(java.lang.Class clazz,
java.lang.Class 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 java.lang.Object |
invoke(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
Invokes accessible method of an object.
|
static java.lang.Object |
invoke(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Object... params) |
static java.lang.Object |
invoke(java.lang.Class c,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
Invokes static method.
|
static java.lang.Object |
invoke(java.lang.Class c,
java.lang.String method,
java.lang.Object... params)
Invokes static method.
|
static java.lang.Object |
invoke(java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
Invokes accessible method of an object.
|
static java.lang.Object |
invoke(java.lang.Object obj,
java.lang.String method,
java.lang.Object... params)
Invokes accessible method of an object without specifying parameter types.
|
static java.lang.Object |
invokeDeclared(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
Invokes any method of a class, even private ones.
|
static java.lang.Object |
invokeDeclared(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Object... params) |
static java.lang.Object |
invokeDeclared(java.lang.Class c,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params) |
static java.lang.Object |
invokeDeclared(java.lang.Class c,
java.lang.String method,
java.lang.Object... params) |
static java.lang.Object |
invokeDeclared(java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
Invokes any method of a class suppressing java access checking.
|
static java.lang.Object |
invokeDeclared(java.lang.Object obj,
java.lang.String method,
java.lang.Object... params) |
static boolean |
isAssignableFrom(java.lang.reflect.Member member1,
java.lang.reflect.Member member2)
Returns
true if the first member is accessible from second one. |
static boolean |
isBeanProperty(java.lang.reflect.Method method)
Returns
true if method is a bean property. |
static boolean |
isBeanPropertyGetter(java.lang.reflect.Method method)
Returns
true if method is bean getter. |
static boolean |
isBeanPropertySetter(java.lang.reflect.Method method)
Returns
true if method is bean setter. |
static boolean |
isInstanceOf(java.lang.Object o,
java.lang.Class target)
Dynamic version of
instanceof. |
static boolean |
isInterfaceImpl(java.lang.Class thisClass,
java.lang.Class targetInterface)
Returns
true if provided class is interface implementation. |
static boolean |
isObjectMethod(java.lang.reflect.Method method)
Returns
true if method defined in Object class. |
static boolean |
isPublic(java.lang.Class c)
Returns
true if class is public. |
static boolean |
isPublic(java.lang.reflect.Member member)
Returns
true if class member is public. |
static boolean |
isPublicPublic(java.lang.reflect.Member member)
Returns
true if class member is public and if its declaring class is also public. |
static boolean |
isSubclass(java.lang.Class thisClass,
java.lang.Class target)
Determines if first class match the destination and simulates kind
of
instanceof. |
static boolean |
isUserDefinedMethod(java.lang.reflect.Method method)
Returns
true if method is user defined and not defined in Object class. |
static java.lang.Object |
newInstance(java.lang.Class type)
Creates new instances including for common mutable classes that do not have a default constructor.
|
static java.lang.Object |
readAnnotationValue(java.lang.annotation.Annotation annotation,
java.lang.String name)
Reads annotation value.
|
static java.lang.reflect.Type |
resolveVariable(java.lang.reflect.TypeVariable variable,
java.lang.Class implClass)
Resolves
TypeVariable with given implementation class. |
static java.lang.String |
typeToString(java.lang.reflect.Type type)
Converts
Type to a String. |
public static final java.lang.Class[] NO_PARAMETERS
public static final java.lang.Object[] NO_ARGUMENTS
public static final java.lang.reflect.Type[] NO_TYPES
public static final java.lang.String METHOD_GET_PREFIX
public static final java.lang.String METHOD_IS_PREFIX
public static final java.lang.String METHOD_SET_PREFIX
public static java.lang.reflect.Method getMethod0(java.lang.Class c,
java.lang.String name,
java.lang.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 java.lang.reflect.Method findMethod(java.lang.Class c,
java.lang.String methodName)
c - class to examinemethodName - Full name of the method.public static java.lang.reflect.Method findDeclaredMethod(java.lang.Class c,
java.lang.String methodName)
findMethod(Class, String)public static java.lang.Class[] getClasses(java.lang.Object... objects)
public static java.lang.Object invoke(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
c - class that contains methodobj - object to executemethod - method to invokeparamClasses - classes of parametersparams - parametersjava.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invoke(java.lang.Class c,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invoke(java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
obj - objectmethod - name of the objects methodparams - method parametersparamClasses - method parameter typesjava.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invoke(java.lang.Object obj,
java.lang.String method,
java.lang.Object... params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
obj - objectmethod - method of an objectparams - method parametersjava.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invoke(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Object... params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invoke(java.lang.Class c,
java.lang.String method,
java.lang.Object... params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invokeDeclared(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
c - class to examineobj - object to inspectmethod - method to invokeparamClasses - parameter typesparams - parametersjava.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invokeDeclared(java.lang.Class c,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invokeDeclared(java.lang.Object obj,
java.lang.String method,
java.lang.Class[] paramClasses,
java.lang.Object[] params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
obj - object to inspectmethod - method to invokeparamClasses - parameter typesparams - parametersjava.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invokeDeclared(java.lang.Object obj,
java.lang.String method,
java.lang.Object... params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invokeDeclared(java.lang.Class c,
java.lang.Object obj,
java.lang.String method,
java.lang.Object... params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static java.lang.Object invokeDeclared(java.lang.Class c,
java.lang.String method,
java.lang.Object... params)
throws java.lang.IllegalAccessException,
java.lang.NoSuchMethodException,
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionpublic static boolean isSubclass(java.lang.Class thisClass,
java.lang.Class target)
instanceof. All subclasses and interface of first class
are examined against second class. Method is not symmetric.public static boolean isInterfaceImpl(java.lang.Class thisClass,
java.lang.Class targetInterface)
true if provided class is interface implementation.public static boolean isInstanceOf(java.lang.Object o,
java.lang.Class target)
instanceof.o - object to matchtarget - target classtrue if object is an instance of target classpublic static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz)
getAccessibleMethods(Class, Class)public static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz,
java.lang.Class limit)
Object.class). Abstract methods are ignored.public static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz)
public static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz,
java.lang.Class limit)
public static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz)
public static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz,
java.lang.Class 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 java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz)
public static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz,
java.lang.Class limit)
public static boolean compareDeclarations(java.lang.reflect.Method first,
java.lang.reflect.Method second)
public static boolean compareSignatures(java.lang.reflect.Method first,
java.lang.reflect.Method second)
public static boolean compareSignatures(java.lang.reflect.Constructor first,
java.lang.reflect.Constructor second)
public static boolean compareSignatures(java.lang.reflect.Field first,
java.lang.reflect.Field second)
public static boolean compareParameters(java.lang.Class[] first,
java.lang.Class[] second)
public static void forceAccess(java.lang.reflect.AccessibleObject accObject)
public static boolean isPublic(java.lang.reflect.Member member)
true if class member is public.public static boolean isPublicPublic(java.lang.reflect.Member member)
true if class member is public and if its declaring class is also public.public static boolean isPublic(java.lang.Class c)
true if class is public.public static java.lang.Object newInstance(java.lang.Class type)
throws java.lang.IllegalAccessException,
java.lang.InstantiationException
if blocks
is faster then using a HashMap.java.lang.IllegalAccessExceptionjava.lang.InstantiationExceptionpublic static boolean isAssignableFrom(java.lang.reflect.Member member1,
java.lang.reflect.Member member2)
true if the first member is accessible from second one.public static java.lang.Class[] getSuperclasses(java.lang.Class type)
public static boolean isUserDefinedMethod(java.lang.reflect.Method method)
true if method is user defined and not defined in Object class.public static boolean isObjectMethod(java.lang.reflect.Method method)
true if method defined in Object class.public static boolean isBeanProperty(java.lang.reflect.Method method)
true if method is a bean property.public static boolean isBeanPropertyGetter(java.lang.reflect.Method method)
true if method is bean getter.public static java.lang.String getBeanPropertyGetterName(java.lang.reflect.Method method)
null if method is not a real getter.public static boolean isBeanPropertySetter(java.lang.reflect.Method method)
true if method is bean setter.public static java.lang.String getBeanPropertySetterName(java.lang.reflect.Method method)
null if method is not a real setter.public static java.lang.Class getComponentType(java.lang.reflect.Type type)
public static java.lang.Class getComponentType(java.lang.reflect.Type type,
java.lang.Class implClass)
public static java.lang.Class getComponentType(java.lang.reflect.Type type,
int index)
public static java.lang.Class getComponentType(java.lang.reflect.Type type,
java.lang.Class implClass,
int index)
null if given type does not have a single
component type. For example the following types all have the
component-type MyClass:
Map<A, B> has 2 component types. If index is 0 or positive,
than it represents order of component type. If the value is negative, then it represents
component type counted from the end! Therefore, the default value of -1
always returns the last component type.public static java.lang.Class getGenericSupertype(java.lang.Class type,
int index)
public static java.lang.Class getGenericSupertype(java.lang.Class type)
public static java.lang.Class getRawType(java.lang.reflect.Type type)
type. Use this method with both
regular and generic types.type - the type to converttypegetRawType(java.lang.reflect.Type, Class)public static java.lang.Class<?> getRawType(java.lang.reflect.Type type,
java.lang.Class implClass)
type when implementation class is known
and it makes difference.public static java.lang.reflect.Type resolveVariable(java.lang.reflect.TypeVariable variable,
java.lang.Class implClass)
TypeVariable with given implementation class.public static java.lang.String typeToString(java.lang.reflect.Type type)
Type to a String. Supports successor interfaces:
java.lang.Class - represents usual classjava.lang.reflect.ParameterizedType - class with generic parameter (e.g. List)java.lang.reflect.TypeVariable - generic type literal (e.g. List, T - type variable)java.lang.reflect.WildcardType - wildcard type (List<? extends Number>, "? extends Number - wildcard type)java.lang.reflect.GenericArrayType - type for generic array (e.g. T[], T - array type)public static java.lang.Object readAnnotationValue(java.lang.annotation.Annotation annotation,
java.lang.String name)
null on error
(e.g. when value name not found).public static java.lang.Class getCallerClass(int framesToSkip)
Reflection.getCallerClass using standard API.
This implementation uses custom SecurityManager
and it is the fastest. Other implementations are:
new Throwable().getStackTrace()[callStackDepth]Thread.currentThread().getStackTrace()[callStackDepth] (the slowest)
In case when usage of SecurityManager is not allowed,
this method fails back to the second implementation.
Note that original Reflection.getCallerClass is way faster
then any emulation.
Copyright © 2003-2013 Jodd Team