|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jboss.solder.reflection.Reflections
public class Reflections
Utility class for working with JDK Reflection and also CDI's
Annotated
metadata.
Field Summary | |
---|---|
static Annotation[] |
EMPTY_ANNOTATION_ARRAY
An empty array of type Annotation , useful converting lists to
arrays. |
static Class<?>[] |
EMPTY_CLASSES
|
static Object[] |
EMPTY_OBJECT_ARRAY
An empty array of type Object , useful for converting lists to
arrays. |
static Type[] |
EMPTY_TYPES
|
Method Summary | ||
---|---|---|
static Map<Class<?>,Type> |
buildTypeMap(Set<Type> types)
|
|
static
|
cast(Object obj)
Perform a runtime cast. |
|
static Class<?> |
classForName(String name,
ClassLoader... loaders)
Loads and initializes a class for the given name. |
|
static boolean |
containsWildcards(Type[] types)
|
|
static Constructor<?> |
findDeclaredConstructor(Class<?> clazz,
Class<?>... args)
Search the class hierarchy for a constructor with the given arguments. |
|
static Field |
findDeclaredField(Class<?> clazz,
String name)
Search the class hierarchy for a field with the given name. |
|
static Method |
findDeclaredMethod(Class<?> clazz,
String name,
Class<?>... args)
Search the class hierarchy for a method with the given name and arguments. |
|
static Type[] |
getActualTypeArguments(Class<?> clazz)
Gets the actual type arguments of a class |
|
static Type[] |
getActualTypeArguments(Type type)
Gets the actual type arguments of a Type |
|
static Set<Constructor<?>> |
getAllDeclaredConstructors(Class<?> clazz)
Get all the declared constructors on the class hierarchy. |
|
static Set<Field> |
getAllDeclaredFields(Class<?> clazz)
Get all the declared fields on the class hierarchy. |
|
static Set<Method> |
getAllDeclaredMethods(Class<?> clazz)
Get all the declared methods on the class hierarchy. |
|
static Set<Annotation> |
getAnnotationsWithMetaAnnotation(Set<Annotation> annotations,
Class<? extends Annotation> metaAnnotationType)
Search for annotations with the specified meta annotation type |
|
static
|
getField(AnnotatedType<X> annotatedType,
Field field)
Search the annotatedType for the field, returning the AnnotatedField |
|
static Object |
getFieldValue(Field field,
Object instance)
|
|
static
|
getFieldValue(Field field,
Object instance,
Class<T> expectedType)
Get the value of the field, on the specified instance, casting the value of the field to the expected type. |
|
static Class<?> |
getMemberType(Member member)
Get the type of the member |
|
static int |
getNesting(Class<?> clazz)
|
|
static Object |
getNonPrivateFinalMethodOrType(Class<?> type)
|
|
static String |
getPropertyName(Method method)
Gets the property name from a getter method. |
|
static Set<Annotation> |
getQualifiers(Set<Annotation> annotations,
BeanManager beanManager)
Extract any qualifiers from the set of annotations |
|
static
|
getRawType(Type type)
Extract the raw type, given a type. |
|
static
|
invokeMethod(boolean setAccessible,
Method method,
Class<T> expectedReturnType,
Object instance,
Object... args)
Invoke the method on the instance, with any arguments specified, casting the result of invoking the method to the expected return type. |
|
static Object |
invokeMethod(boolean setAccessible,
Method method,
Object instance,
Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method. |
|
static
|
invokeMethod(Method method,
Class<T> expectedReturnType,
Object instance,
Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method. |
|
static Object |
invokeMethod(Method method,
Object instance,
Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method. |
|
static boolean |
isAbstract(Method method)
Checks if a method is abstract |
|
static boolean |
isArrayType(Class<?> rawType)
Checks if raw type is array type |
|
static boolean |
isAssignableFrom(Class<?> rawType1,
Type[] actualTypeArguments1,
Class<?> rawType2,
Type[] actualTypeArguments2)
Check the assignability of one type to another, taking into account the actual type arguements |
|
static boolean |
isAssignableFrom(Class<?> rawType1,
Type[] actualTypeArguments1,
Type type2)
|
|
static boolean |
isAssignableFrom(Set<Type> types1,
Set<Type> types2)
Check the assiginability of a set of flattened types. |
|
static boolean |
isAssignableFrom(Set<Type> types1,
Type type2)
Check the assiginability of a set of flattened types. |
|
static boolean |
isAssignableFrom(Type[] types1,
Type type2)
|
|
static boolean |
isAssignableFrom(Type[] actualTypeArguments1,
Type[] actualTypeArguments2)
|
|
static boolean |
isAssignableFrom(Type type1,
Set<? extends Type> types2)
|
|
static boolean |
isAssignableFrom(Type type1,
Type type2)
|
|
static boolean |
isAssignableFrom(Type type1,
Type[] types2)
|
|
static boolean |
isBindings(Annotation binding)
Deprecated. |
|
static boolean |
isCacheable(Annotation[] annotations)
|
|
static boolean |
isCacheable(Set<Annotation> annotations)
|
|
static boolean |
isFinal(Class<?> clazz)
Checks if class is final |
|
static boolean |
isFinal(Member member)
Checks if member is final |
|
static boolean |
isPackagePrivate(int mod)
|
|
static boolean |
isParamerterizedTypeWithWildcard(Class<?> type)
|
|
static boolean |
isParameterizedType(Class<?> type)
Checks if type is parameterized type |
|
static boolean |
isPrimitive(Type type)
|
|
static boolean |
isPrivate(Member member)
Checks if member is private |
|
static boolean |
isSerializable(Class<?> clazz)
Check if a class is serializable. |
|
static boolean |
isStatic(Class<?> type)
Checks if type is static |
|
static boolean |
isStatic(Member member)
Checks if member is static |
|
static boolean |
isTransient(Member member)
|
|
static boolean |
isTypeBounded(Type type,
Type[] lowerBounds,
Type[] upperBounds)
|
|
static boolean |
isTypeOrAnyMethodFinal(Class<?> type)
Checks if type or member is final |
|
static boolean |
matches(Class<?> rawType1,
Type[] actualTypeArguments1,
Class<?> rawType2,
Type[] actualTypeArguments2)
|
|
static boolean |
matches(Class<?> rawType1,
Type[] actualTypeArguments1,
Type type2)
|
|
static boolean |
matches(Set<Type> types1,
Set<Type> types2)
Check whether whether any of the types1 matches a type in types2 |
|
static boolean |
matches(Type type1,
Set<? extends Type> types2)
|
|
static boolean |
matches(Type type1,
Type type2)
|
|
static boolean |
methodExists(Class<?> clazz,
String name)
Determine if a method exists in a specified class hierarchy |
|
static
|
setAccessible(A member)
Set the accessibility flag on the AccessibleObject as described in
AccessibleObject.setAccessible(boolean) within the context of
a PrivilegedAction . |
|
static void |
setFieldValue(boolean setAccessible,
Field field,
Object instance,
Object value)
Sets the value of a field on the instance to the specified value. |
|
static void |
setFieldValue(Field field,
Object instance,
Object value)
Set the value of a field on the instance to the specified value. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Annotation[] EMPTY_ANNOTATION_ARRAY
Annotation
, useful converting lists to
arrays.
public static final Object[] EMPTY_OBJECT_ARRAY
Object
, useful for converting lists to
arrays.
public static final Type[] EMPTY_TYPES
public static final Class<?>[] EMPTY_CLASSES
Method Detail |
---|
public static <T> T cast(Object obj)
Perform a runtime cast. Similar to Class.cast(Object)
, but useful
when you do not have a Class
object for type you wish to cast to.
Class.cast(Object)
should be used if possible
T
- the type to cast toobj
- the object to perform the cast on
ClassCastException
- if the type T is not a subtype of the objectClass.cast(Object)
public static Set<Field> getAllDeclaredFields(Class<?> clazz)
clazz
- The class to search
public static Field findDeclaredField(Class<?> clazz, String name)
clazz
- The class to searchname
- The name of the field to search for
public static <X> AnnotatedField<? super X> getField(AnnotatedType<X> annotatedType, Field field)
AnnotatedField
annotatedType
- The annotatedType to searchfield
- the field to search for
AnnotatedField
found, or null if no field is foundpublic static Set<Annotation> getAnnotationsWithMetaAnnotation(Set<Annotation> annotations, Class<? extends Annotation> metaAnnotationType)
annotations
- The annotation set to searchmetaAnnotationType
- The type of the meta annotation to search for
public static Set<Annotation> getQualifiers(Set<Annotation> annotations, BeanManager beanManager)
annotations
- The set of annotations to searchbeanManager
- The beanManager to use to establish if an annotation is
a qualifier
public static boolean methodExists(Class<?> clazz, String name)
clazz
- The class to searchname
- The name of the method
public static Set<Method> getAllDeclaredMethods(Class<?> clazz)
clazz
- The class to search
public static Method findDeclaredMethod(Class<?> clazz, String name, Class<?>... args)
clazz
- The class to searchname
- The name of the method to search forargs
- The arguments of the method to search for
public static Constructor<?> findDeclaredConstructor(Class<?> clazz, Class<?>... args)
clazz
- The class to searchargs
- The arguments of the constructor to search for
public static Set<Constructor<?>> getAllDeclaredConstructors(Class<?> clazz)
clazz
- The class to search
public static Class<?> getMemberType(Member member)
member
- The member
UnsupportedOperationException
- if the member is not a field,
method, or constructorpublic static Class<?> classForName(String name, ClassLoader... loaders) throws ClassNotFoundException
Loads and initializes a class for the given name.
If the Thread Context Class Loader is available, it will be used,
otherwise the classloader used to load Reflections
will be used
It is also possible to specify additional classloaders to attempt to load the class with. If the first attempt fails, then these additional loaders are tried in order.
name
- the name of the class to loadloaders
- additional classloaders to use to attempt to load the class
ClassNotFoundException
- if the class cannot be foundpublic static <A extends AccessibleObject> A setAccessible(A member)
AccessibleObject
as described in
AccessibleObject.setAccessible(boolean)
within the context of
a PrivilegedAction
.
A
- member the accessible object typemember
- the accessible object
public static Object invokeMethod(Method method, Object instance, Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.
This method provides the same functionality and throws the same exceptions as
invokeMethod(boolean, Method, Class, Object, Object...)
, with the
expected return type set to Object
and no change to the method's accessibility.
invokeMethod(boolean, Method, Class, Object, Object...)
,
Method.invoke(Object, Object...)
public static Object invokeMethod(boolean setAccessible, Method method, Object instance, Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.
This method attempts to set the accessible flag of the method in a
PrivilegedAction
before invoking the method if the first argument
is true.
This method provides the same functionality and throws the same exceptions as
invokeMethod(boolean, Method, Class, Object, Object...)
, with the
expected return type set to Object
.
invokeMethod(boolean, Method, Class, Object, Object...)
,
Method.invoke(Object, Object...)
public static <T> T invokeMethod(Method method, Class<T> expectedReturnType, Object instance, Object... args)
Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.
This method provides the same functionality and throws the same exceptions as
invokeMethod(boolean, Method, Class, Object, Object...)
, with the
expected return type set to Object
and honoring the accessibility of
the method.
invokeMethod(boolean, Method, Class, Object, Object...)
,
Method.invoke(Object, Object...)
public static <T> T invokeMethod(boolean setAccessible, Method method, Class<T> expectedReturnType, Object instance, Object... args)
Invoke the method on the instance, with any arguments specified, casting the result of invoking the method to the expected return type.
This method wraps Method.invoke(Object, Object...)
, converting the
checked exceptions that Method.invoke(Object, Object...)
specifies
to runtime exceptions.
If instructed, this method attempts to set the accessible flag of the method in a
PrivilegedAction
before invoking the method.
setAccessible
- flag indicating whether method should first be set as
accessiblemethod
- the method to invokeinstance
- the instance to invoke the methodargs
- the arguments to the method
RuntimeException
- if this Method
object enforces Java
language access control and the underlying method is
inaccessible or if the underlying method throws an exception or
if the initialization provoked by this method fails.
IllegalArgumentException
- if the method is an instance method and
the specified instance
argument is not an instance
of the class or interface declaring the underlying method (or
of a subclass or implementor thereof); if the number of actual
and formal parameters differ; if an unwrapping conversion for
primitive arguments fails; or if, after possible unwrapping, a
parameter value cannot be converted to the corresponding formal
parameter type by a method invocation conversion.
NullPointerException
- if the specified instance
is
null and the method is an instance method.
ClassCastException
- if the result of invoking the method cannot be
cast to the expectedReturnType
ExceptionInInitializerError
- if the initialization provoked by this
method fails.Method.invoke(Object, Object...)
public static void setFieldValue(Field field, Object instance, Object value)
Set the value of a field on the instance to the specified value.
This method provides the same functionality and throws the same exceptions as
Reflections#setFieldValue(boolean, Method, Class, Object, Object...)
, honoring
the accessibility of the field.
public static void setFieldValue(boolean setAccessible, Field field, Object instance, Object value)
Sets the value of a field on the instance to the specified value.
This method wraps Field.set(Object, Object)
, converting the
checked exceptions that Field.set(Object, Object)
specifies to
runtime exceptions.
If instructed, this method attempts to set the accessible flag of the method in a
PrivilegedAction
before invoking the method.
field
- the field on which to operate, or null if the field is staticinstance
- the instance on which the field value should be set uponvalue
- the value to set the field to
RuntimeException
- if the underlying field is inaccessible.
IllegalArgumentException
- if the specified instance
is not an
instance of the class or interface declaring the underlying
field (or a subclass or implementor thereof), or if an
unwrapping conversion fails.
NullPointerException
- if the specified instance
is null and the field
is an instance field.
ExceptionInInitializerError
- if the initialization provoked by this
method fails.Field.set(Object, Object)
public static Object getFieldValue(Field field, Object instance)
public static <T> T getFieldValue(Field field, Object instance, Class<T> expectedType)
Get the value of the field, on the specified instance, casting the value of the field to the expected type.
This method wraps Field.get(Object)
, converting the checked
exceptions that Field.get(Object)
specifies to runtime exceptions.
T
- the type of the field's valuefield
- the field to operate oninstance
- the instance from which to retrieve the valueexpectedType
- the expected type of the field's value
RuntimeException
- if the underlying field is inaccessible.
IllegalArgumentException
- if the specified instance
is not an
instance of the class or interface declaring the underlying
field (or a subclass or implementor thereof).
NullPointerException
- if the specified instance
is null and the field
is an instance field.
ExceptionInInitializerError
- if the initialization provoked by this
method fails.public static <T> Class<T> getRawType(Type type)
T
- the typetype
- the type to extract the raw type from
public static boolean isSerializable(Class<?> clazz)
clazz
- The class to check
public static Map<Class<?>,Type> buildTypeMap(Set<Type> types)
public static boolean isCacheable(Set<Annotation> annotations)
public static boolean isCacheable(Annotation[] annotations)
public static String getPropertyName(Method method)
method
- The getter method
public static boolean isFinal(Class<?> clazz)
clazz
- The class to check
public static int getNesting(Class<?> clazz)
public static boolean isFinal(Member member)
member
- The member to check
public static boolean isPrivate(Member member)
member
- The member to check
public static boolean isTypeOrAnyMethodFinal(Class<?> type)
type
- Type or member
public static Object getNonPrivateFinalMethodOrType(Class<?> type)
public static boolean isPackagePrivate(int mod)
public static boolean isStatic(Class<?> type)
type
- Type to check
public static boolean isStatic(Member member)
member
- Member to check
public static boolean isTransient(Member member)
public static boolean isAbstract(Method method)
method
-
public static Type[] getActualTypeArguments(Class<?> clazz)
clazz
- The class to examine
public static Type[] getActualTypeArguments(Type type)
type
- The type to examine
public static boolean isArrayType(Class<?> rawType)
rawType
- The raw type to check
public static boolean isParameterizedType(Class<?> type)
type
- The type to check
public static boolean isParamerterizedTypeWithWildcard(Class<?> type)
public static boolean containsWildcards(Type[] types)
@Deprecated public static boolean isBindings(Annotation binding)
binding
- The binding type to check
public static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)
rawType1
- the raw type of the class to checkactualTypeArguments1
- the actual type arguements to check, or an
empty array if not a parameterized typerawType2
- the raw type of the class to checkactualTypeArguments2
- the actual type arguements to check, or an
empty array if not a parameterized type
public static boolean matches(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)
public static boolean isAssignableFrom(Type[] actualTypeArguments1, Type[] actualTypeArguments2)
public static boolean isAssignableFrom(Type type1, Set<? extends Type> types2)
public static boolean matches(Type type1, Set<? extends Type> types2)
public static boolean isAssignableFrom(Type type1, Type[] types2)
public static boolean isAssignableFrom(Type type1, Type type2)
public static boolean matches(Type type1, Type type2)
public static boolean isTypeBounded(Type type, Type[] lowerBounds, Type[] upperBounds)
public static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)
public static boolean matches(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)
public static boolean isAssignableFrom(Set<Type> types1, Set<Type> types2)
types1
- types2
-
public static boolean matches(Set<Type> types1, Set<Type> types2)
types1
- types2
-
public static boolean isAssignableFrom(Set<Type> types1, Type type2)
types1
- type2
-
public static boolean isAssignableFrom(Type[] types1, Type type2)
public static boolean isPrimitive(Type type)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |