Class Reflections
- java.lang.Object
-
- org.infinispan.cdi.common.util.Reflections
-
public class Reflections extends Object
Utility class for working with JDK Reflection and also CDI'sAnnotatedmetadata.- Author:
- Stuart Douglas, Pete Muir
-
-
Field Summary
Fields Modifier and Type Field Description static Annotation[]EMPTY_ANNOTATION_ARRAYAn empty array of typeAnnotation, useful converting lists to arrays.static Class<?>[]EMPTY_CLASSESstatic Object[]EMPTY_OBJECT_ARRAYAn empty array of typeObject, useful for converting lists to arrays.static Type[]EMPTY_TYPES
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tcast(Object obj)Perform a runtime cast.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 <T> TgetFieldValue(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 <A extends Annotation>
AgetMetaAnnotation(javax.enterprise.inject.spi.Annotated element, Class<A> annotationType)Inspects an annotated element for the given meta annotation.static Set<Annotation>getQualifiers(javax.enterprise.inject.spi.BeanManager beanManager, Iterable<Annotation> annotations)Extract the qualifiers from a set of annotations.static Set<Annotation>getQualifiers(javax.enterprise.inject.spi.BeanManager beanManager, Iterable<Annotation>... annotations)Extract the qualifiers from a set of annotations.static <T> Class<T>getRawType(Type type)Extract the raw type, given a type.static <T> TinvokeMethod(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 ObjectinvokeMethod(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 <T> TinvokeMethod(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 booleanisAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)Check the assignability of one type to another, taking into account the actual type arguementsstatic booleanisAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)static booleanisAssignableFrom(Type[] types1, Type type2)static booleanisAssignableFrom(Type[] actualTypeArguments1, Type[] actualTypeArguments2)static booleanisAssignableFrom(Type type1, Type type2)static booleanisAssignableFrom(Type type1, Type[] types2)static booleanisSerializable(Class<?> clazz)Check if a class is serializable.static booleanisTypeBounded(Type type, Type[] lowerBounds, Type[] upperBounds)static booleanmatches(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)static booleanmatches(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)static booleanmatches(Type type1, Type type2)static booleanmatches(Type type1, Set<? extends Type> types2)static booleanmatches(Set<Type> types1, Set<Type> types2)Check whether whether any of the types1 matches a type in types2static <A extends AccessibleObject>
AsetAccessible(A member)Set the accessibility flag on theAccessibleObjectas described inAccessibleObject.setAccessible(boolean)within the context of aPrivilegedAction.
-
-
-
Field Detail
-
EMPTY_ANNOTATION_ARRAY
public static final Annotation[] EMPTY_ANNOTATION_ARRAY
An empty array of typeAnnotation, useful converting lists to arrays.
-
EMPTY_OBJECT_ARRAY
public static final Object[] EMPTY_OBJECT_ARRAY
An empty array of typeObject, useful for converting lists to arrays.
-
EMPTY_TYPES
public static final Type[] EMPTY_TYPES
-
EMPTY_CLASSES
public static final Class<?>[] EMPTY_CLASSES
-
-
Method Detail
-
cast
public static <T> T cast(Object obj)
Perform a runtime cast. Similar to
Class.cast(Object), but useful when you do not have aClassobject for type you wish to cast to.Class.cast(Object)should be used if possible- Type Parameters:
T- the type to cast to- Parameters:
obj- the object to perform the cast on- Returns:
- the casted object
- Throws:
ClassCastException- if the type T is not a subtype of the object- See Also:
Class.cast(Object)
-
getAllDeclaredMethods
public static Set<Method> getAllDeclaredMethods(Class<?> clazz)
Get all the declared methods on the class hierarchy. This will return overridden methods.- Parameters:
clazz- The class to search- Returns:
- the set of all declared methods or an empty set if there are none
-
setAccessible
public static <A extends AccessibleObject> A setAccessible(A member)
Set the accessibility flag on theAccessibleObjectas described inAccessibleObject.setAccessible(boolean)within the context of aPrivilegedAction.- Type Parameters:
A- member the accessible object type- Parameters:
member- the accessible object- Returns:
- the accessible object after the accessible flag has been altered
-
invokeMethod
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
PrivilegedActionbefore 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 toObject.
-
invokeMethod
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 toObjectand honoring the accessibility of the method.
-
invokeMethod
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 thatMethod.invoke(Object, Object...)specifies to runtime exceptions.If instructed, this method attempts to set the accessible flag of the method in a
PrivilegedActionbefore invoking the method.- Parameters:
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- Returns:
- the result of invoking the method, or null if the method's return type is void
- Throws:
RuntimeException- if thisMethodobject 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 specifiedinstanceargument 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 specifiedinstanceis null and the method is an instance method.ClassCastException- if the result of invoking the method cannot be cast to the expectedReturnTypeExceptionInInitializerError- if the initialization provoked by this method fails.- See Also:
Method.invoke(Object, Object...)
-
getFieldValue
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 thatField.get(Object)specifies to runtime exceptions.- Type Parameters:
T- the type of the field's value- Parameters:
field- the field to operate oninstance- the instance from which to retrieve the valueexpectedType- the expected type of the field's value- Returns:
- the value of the field
- Throws:
RuntimeException- if the underlying field is inaccessible.IllegalArgumentException- if the specifiedinstanceis not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).NullPointerException- if the specifiedinstanceis null and the field is an instance field.ExceptionInInitializerError- if the initialization provoked by this method fails.
-
getRawType
public static <T> Class<T> getRawType(Type type)
Extract the raw type, given a type.- Type Parameters:
T- the type- Parameters:
type- the type to extract the raw type from- Returns:
- the raw type, or null if the raw type cannot be determined.
-
isSerializable
public static boolean isSerializable(Class<?> clazz)
Check if a class is serializable.- Parameters:
clazz- The class to check- Returns:
- true if the class implements serializable or is a primitive
-
isAssignableFrom
public 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- Parameters:
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- Returns:
-
matches
public static boolean matches(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)
-
isAssignableFrom
public static boolean isAssignableFrom(Type[] actualTypeArguments1, Type[] actualTypeArguments2)
-
isTypeBounded
public static boolean isTypeBounded(Type type, Type[] lowerBounds, Type[] upperBounds)
-
isAssignableFrom
public static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)
-
matches
public static boolean matches(Set<Type> types1, Set<Type> types2)
Check whether whether any of the types1 matches a type in types2- Parameters:
types1-types2-- Returns:
-
getMetaAnnotation
public static <A extends Annotation> A getMetaAnnotation(javax.enterprise.inject.spi.Annotated element, Class<A> annotationType)
Inspects an annotated element for the given meta annotation. This should only be used for user defined meta annotations, where the annotation must be physically present.- Parameters:
element- The element to inspectannotationType- The meta annotation to search for- Returns:
- The annotation instance found on this element or null if no matching annotation was found.
-
getQualifiers
public static Set<Annotation> getQualifiers(javax.enterprise.inject.spi.BeanManager beanManager, Iterable<Annotation> annotations)
Extract the qualifiers from a set of annotations.- Parameters:
beanManager- the beanManager to use to determine if an annotation is a qualifierannotations- the annotations to check- Returns:
- any qualifiers present in
annotations
-
getQualifiers
public static Set<Annotation> getQualifiers(javax.enterprise.inject.spi.BeanManager beanManager, Iterable<Annotation>... annotations)
Extract the qualifiers from a set of annotations.- Parameters:
beanManager- the beanManager to use to determine if an annotation is a qualifierannotations- the annotations to check- Returns:
- any qualifiers present in
annotations
-
-