Class AnnotationUtils
DISCLAIMER
These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!
Some utilities are published via the maintained AnnotationSupport
class.
- Since:
- 1.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ConcurrentHashMap
<Class<? extends Annotation>, Boolean> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindAnnotatedFields
(Class<?> clazz, Class<? extends Annotation> annotationType, Predicate<Field> predicate) findAnnotatedFields
(Class<?> clazz, Class<? extends Annotation> annotationType, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) Find all fields of the supplied class or interface that are annotated or meta-annotated with the specifiedannotationType
and match the specifiedpredicate
.findAnnotatedMethods
(Class<?> clazz, Class<? extends Annotation> annotationType, ReflectionUtils.HierarchyTraversalMode traversalMode) static <A extends Annotation>
Optional<A> findAnnotation
(Class<?> clazz, Class<A> annotationType, boolean searchEnclosingClasses) Find the first annotation of the specified type that is either directly present, meta-present, or indirectly present on the supplied class, optionally searching recursively through the enclosing class hierarchy if not found on the supplied class.static <A extends Annotation>
Optional<A> findAnnotation
(AnnotatedElement element, Class<A> annotationType) private static <A extends Annotation>
Optional<A> findAnnotation
(AnnotatedElement element, Class<A> annotationType, boolean inherited, Set<Annotation> visited) static <A extends Annotation>
Optional<A> findAnnotation
(Parameter parameter, int index, Class<A> annotationType) static <A extends Annotation>
Optional<A> findAnnotation
(Optional<? extends AnnotatedElement> element, Class<A> annotationType) private static <A extends Annotation>
Optional<A> findMetaAnnotation
(Class<A> annotationType, Annotation[] candidates, boolean inherited, Set<Annotation> visited) findPublicAnnotatedFields
(Class<?> clazz, Class<?> fieldType, Class<? extends Annotation> annotationType) private static <A extends Annotation>
voidfindRepeatableAnnotations
(Annotation[] candidates, Class<A> annotationType, Class<? extends Annotation> containerType, boolean inherited, Set<A> found, Set<Annotation> visited) static <A extends Annotation>
List<A> findRepeatableAnnotations
(AnnotatedElement element, Class<A> annotationType) private static <A extends Annotation>
voidfindRepeatableAnnotations
(AnnotatedElement element, Class<A> annotationType, Class<? extends Annotation> containerType, boolean inherited, Set<A> found, Set<Annotation> visited) static <A extends Annotation>
List<A> findRepeatableAnnotations
(Parameter parameter, int index, Class<A> annotationType) static <A extends Annotation>
List<A> findRepeatableAnnotations
(Optional<? extends AnnotatedElement> element, Class<A> annotationType) private static AnnotatedElement
getEffectiveAnnotatedParameter
(Parameter parameter, int index) Due to a bug injavac
on JDK versions prior to JDK 9, looking up annotations directly on aParameter
will fail for inner class constructors.static boolean
isAnnotated
(AnnotatedElement element, Class<? extends Annotation> annotationType) Determine if an annotation ofannotationType
is either present or meta-present on the suppliedelement
.static boolean
isAnnotated
(Parameter parameter, int index, Class<? extends Annotation> annotationType) static boolean
isAnnotated
(Optional<? extends AnnotatedElement> element, Class<? extends Annotation> annotationType) Determine if an annotation ofannotationType
is either present or meta-present on the supplied optionalelement
.private static boolean
isInJavaLangAnnotationPackage
(Class<? extends Annotation> annotationType) private static boolean
isRepeatableAnnotationContainer
(Class<? extends Annotation> candidateContainerType) Determine if the supplied annotation type is a container for a repeatable annotation.
-
Field Details
-
repeatableAnnotationContainerCache
private static final ConcurrentHashMap<Class<? extends Annotation>,Boolean> repeatableAnnotationContainerCache
-
-
Constructor Details
-
AnnotationUtils
private AnnotationUtils()
-
-
Method Details
-
isAnnotated
public static boolean isAnnotated(Optional<? extends AnnotatedElement> element, Class<? extends Annotation> annotationType) Determine if an annotation ofannotationType
is either present or meta-present on the supplied optionalelement
.- See Also:
-
isAnnotated
public static boolean isAnnotated(Parameter parameter, int index, Class<? extends Annotation> annotationType) - Since:
- 1.8
- See Also:
-
isAnnotated
public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType) Determine if an annotation ofannotationType
is either present or meta-present on the suppliedelement
.- Parameters:
element
- the element on which to search for the annotation; may benull
annotationType
- the annotation type to search for; nevernull
- Returns:
true
if the annotation is present or meta-present- See Also:
-
findAnnotation
public static <A extends Annotation> Optional<A> findAnnotation(Optional<? extends AnnotatedElement> element, Class<A> annotationType) - See Also:
-
findAnnotation
public static <A extends Annotation> Optional<A> findAnnotation(Parameter parameter, int index, Class<A> annotationType) - Since:
- 1.8
- See Also:
-
findAnnotation
public static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationType) - See Also:
-
findAnnotation
private static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationType, boolean inherited, Set<Annotation> visited) -
findMetaAnnotation
private static <A extends Annotation> Optional<A> findMetaAnnotation(Class<A> annotationType, Annotation[] candidates, boolean inherited, Set<Annotation> visited) -
findAnnotation
public static <A extends Annotation> Optional<A> findAnnotation(Class<?> clazz, Class<A> annotationType, boolean searchEnclosingClasses) Find the first annotation of the specified type that is either directly present, meta-present, or indirectly present on the supplied class, optionally searching recursively through the enclosing class hierarchy if not found on the supplied class.The enclosing class hierarchy will only be searched above an inner class (i.e., a non-static member class).
- Type Parameters:
A
- the annotation type- Parameters:
clazz
- the class on which to search for the annotation; may benull
annotationType
- the annotation type to search for; nevernull
searchEnclosingClasses
- whether the enclosing class hierarchy should be searched- Returns:
- an
Optional
containing the annotation; nevernull
but potentially empty - Since:
- 1.8
- See Also:
-
findRepeatableAnnotations
public static <A extends Annotation> List<A> findRepeatableAnnotations(Optional<? extends AnnotatedElement> element, Class<A> annotationType) - Since:
- 1.5
- See Also:
-
findRepeatableAnnotations
public static <A extends Annotation> List<A> findRepeatableAnnotations(Parameter parameter, int index, Class<A> annotationType) - Since:
- 1.8
- See Also:
-
findRepeatableAnnotations
public static <A extends Annotation> List<A> findRepeatableAnnotations(AnnotatedElement element, Class<A> annotationType) - See Also:
-
findRepeatableAnnotations
private static <A extends Annotation> void findRepeatableAnnotations(AnnotatedElement element, Class<A> annotationType, Class<? extends Annotation> containerType, boolean inherited, Set<A> found, Set<Annotation> visited) -
findRepeatableAnnotations
private static <A extends Annotation> void findRepeatableAnnotations(Annotation[] candidates, Class<A> annotationType, Class<? extends Annotation> containerType, boolean inherited, Set<A> found, Set<Annotation> visited) -
isRepeatableAnnotationContainer
private static boolean isRepeatableAnnotationContainer(Class<? extends Annotation> candidateContainerType) Determine if the supplied annotation type is a container for a repeatable annotation.- Since:
- 1.5
-
getEffectiveAnnotatedParameter
Due to a bug injavac
on JDK versions prior to JDK 9, looking up annotations directly on aParameter
will fail for inner class constructors.Bug in
javac
on JDK versions prior to JDK 9The parameter annotations array in the compiled byte code for the user's class excludes an entry for the implicit enclosing instance parameter for an inner class constructor.
Workaround
This method provides a workaround for this off-by-one error by helping JUnit maintainers and extension authors to access annotations on the preceding
Parameter
object (i.e.,index - 1
). If the suppliedindex
is zero in such situations this method will returnnull
since the parameter for the implicit enclosing instance will never be annotated.WARNING
The
AnnotatedElement
returned by this method should never be cast and treated as aParameter
since the metadata (e.g.,Parameter.getName()
,Parameter.getType()
, etc.) will not match those for the declared parameter at the given index in an inner class constructor for code compiled with JDK 8.- Returns:
- the supplied
Parameter
, or the effectiveParameter
if the aforementioned bug is detected, ornull
if the bug is detected and the suppliedindex
is0
- Since:
- 1.8
-
findPublicAnnotatedFields
public static List<Field> findPublicAnnotatedFields(Class<?> clazz, Class<?> fieldType, Class<? extends Annotation> annotationType) - See Also:
-
findAnnotatedFields
public static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotationType, Predicate<Field> predicate) - See Also:
-
findAnnotatedFields
public static List<Field> findAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotationType, Predicate<Field> predicate, ReflectionUtils.HierarchyTraversalMode traversalMode) Find all fields of the supplied class or interface that are annotated or meta-annotated with the specifiedannotationType
and match the specifiedpredicate
.- Parameters:
clazz
- the class or interface in which to find the fields; nevernull
annotationType
- the annotation type to search for; nevernull
predicate
- the field filter; nevernull
traversalMode
- the hierarchy traversal mode; nevernull
- Returns:
- the list of all such fields found; neither
null
nor mutable
-
findAnnotatedMethods
public static List<Method> findAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationType, ReflectionUtils.HierarchyTraversalMode traversalMode) - See Also:
-
isInJavaLangAnnotationPackage
-