Class AnnotationSupport
- java.lang.Object
-
- org.junit.platform.commons.support.AnnotationSupport
-
@API(status=MAINTAINED, since="1.0") public final class AnnotationSupport extends java.lang.Object
Common annotation support.- Since:
- 1.0
- See Also:
ClassSupport,ReflectionSupport
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<java.lang.reflect.Method>findAnnotatedMethods(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, HierarchyTraversalMode traversalMode)Find all methods of the supplied class or interface that are annotated or meta-annotated with the specifiedannotationType.static <A extends java.lang.annotation.Annotation>
java.util.Optional<A>findAnnotation(java.lang.reflect.AnnotatedElement element, java.lang.Class<A> annotationType)Find the first annotation ofannotationTypethat is either directly present, meta-present, or indirectly present on the suppliedelement.static <A extends java.lang.annotation.Annotation>
java.util.Optional<A>findAnnotation(java.util.Optional<? extends java.lang.reflect.AnnotatedElement> element, java.lang.Class<A> annotationType)Find the first annotation ofannotationTypethat is either present or meta-present on the supplied optionalelement.static java.util.List<java.lang.reflect.Field>findPublicAnnotatedFields(java.lang.Class<?> clazz, java.lang.Class<?> fieldType, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Find allpublicfields of the supplied class or interface that are of the specifiedfieldTypeand annotated or meta-annotated with the specifiedannotationType.static <A extends java.lang.annotation.Annotation>
java.util.List<A>findRepeatableAnnotations(java.lang.reflect.AnnotatedElement element, java.lang.Class<A> annotationType)Find all repeatable annotations ofannotationTypethat are either present, indirectly present, or meta-present on the suppliedAnnotatedElement.static booleanisAnnotated(java.lang.reflect.AnnotatedElement element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Determine if an annotation ofannotationTypeis either present or meta-present on the suppliedelement.
-
-
-
Method Detail
-
isAnnotated
public static boolean isAnnotated(java.lang.reflect.AnnotatedElement element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Determine if an annotation ofannotationTypeis either present or meta-present on the suppliedelement.- See Also:
findAnnotation(AnnotatedElement, Class)
-
findAnnotation
public static <A extends java.lang.annotation.Annotation> java.util.Optional<A> findAnnotation(java.util.Optional<? extends java.lang.reflect.AnnotatedElement> element, java.lang.Class<A> annotationType)Find the first annotation ofannotationTypethat is either present or meta-present on the supplied optionalelement.- See Also:
findAnnotation(AnnotatedElement, Class)
-
findAnnotation
public static <A extends java.lang.annotation.Annotation> java.util.Optional<A> findAnnotation(java.lang.reflect.AnnotatedElement element, java.lang.Class<A> annotationType)Find the first annotation ofannotationTypethat is either directly present, meta-present, or indirectly present on the suppliedelement.If the element is a class and the annotation is neither directly present nor meta-present on the class, this method will additionally search on interfaces implemented by the class before finding an annotation that is indirectly present on the class.
- Returns:
- an
Optionalcontaining the annotation; nevernullbut potentially empty
-
findRepeatableAnnotations
public static <A extends java.lang.annotation.Annotation> java.util.List<A> findRepeatableAnnotations(java.lang.reflect.AnnotatedElement element, java.lang.Class<A> annotationType)Find all repeatable annotations ofannotationTypethat are either present, indirectly present, or meta-present on the suppliedAnnotatedElement.This method extends the functionality of
AnnotatedElement.getAnnotationsByType(Class)with additional support for meta-annotations.In addition, if the element is a class and the repeatable annotation is
@Inherited, this method will search on superclasses first in order to support top-down semantics. The result is that this algorithm finds repeatable annotations that would be shadowed and therefore not visible according to Java's standard semantics for inherited, repeatable annotations, but most developers will naturally assume that all repeatable annotations in JUnit are discovered regardless of whether they are declared stand-alone, in a container, or as a meta-annotation (e.g., multiple declarations of@ExtendWithwithin a test class hierarchy).If the element is a class and the repeatable annotation is not discovered within the class hierarchy, this method will additionally search on interfaces implemented by each class in the hierarchy.
If the supplied
elementisnull, this method simply returns an empty list.- Parameters:
element- the element to search on, potentiallynullannotationType- the repeatable annotation type to search for; nevernull- Returns:
- the list of all such annotations found; neither
nullnor mutable - See Also:
Repeatable,Inherited
-
findPublicAnnotatedFields
public static java.util.List<java.lang.reflect.Field> findPublicAnnotatedFields(java.lang.Class<?> clazz, java.lang.Class<?> fieldType, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Find allpublicfields of the supplied class or interface that are of the specifiedfieldTypeand annotated or meta-annotated with the specifiedannotationType.Consult the Javadoc for
Class.getFields()for details on inheritance and ordering.- Parameters:
clazz- the class or interface in which to find the fields; nevernullfieldType- the type of field to find; nevernullannotationType- the annotation type to search for; nevernull- Returns:
- the list of all such fields found; neither
nullnor mutable - See Also:
Class.getFields()
-
findAnnotatedMethods
public static java.util.List<java.lang.reflect.Method> findAnnotatedMethods(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType, HierarchyTraversalMode traversalMode)Find all methods of the supplied class or interface that are annotated or meta-annotated with the specifiedannotationType.- Parameters:
clazz- the class or interface in which to find the methods; nevernullannotationType- the annotation type to search for; nevernulltraversalMode- the hierarchy traversal mode; nevernull- Returns:
- the list of all such methods found; neither
nullnor mutable
-
-