Class AnnotationUtils
- Since:
- 3.0
- Author:
- Allard Buijze
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanBoolean specifying that afindAnnotationAttributes(AnnotatedElement, String, boolean)invocation should contain all attributes of the target annotation recursively taken into account attributes of other annotations the target is present on.static final booleanBoolean specifying that afindAnnotationAttributes(AnnotatedElement, String, boolean)invocation should only contain the exact attributes of the target annotation, overridden by identical attributes on meta-annotated annotations. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T> findAnnotationAttribute(AnnotatedElement element, Class<? extends Annotation> annotationType, String attributeName) Find theattributeNameof an annotation of typeannotationTypeon the givenelement.findAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType) Find the attributes of an annotation of typeannotationTypeon the givenelement.findAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType, boolean overrideOnly) Find the attributes of an annotation of typeannotationTypeon the givenelement.findAnnotationAttributes(AnnotatedElement element, String annotationName) Find the attributes of an annotation with givenannotationNameon the givenelement.findAnnotationAttributes(AnnotatedElement element, String annotationName, boolean overrideOnly) Find the attributes of an annotation with givenannotationNameon the givenelement.findAnnotationAttributesOnType(Class<?> type, Class<? extends Annotation> annotationType, Predicate<Map<String, Object>> expectedAttributes) Find the attributes of an annotation of typeannotationTypeon the giventype, following a layered search strategy.isAnnotatedWith(Class<? extends Annotation> annotationType) static booleanisAnnotatedWith(Class<? extends Annotation> target, Class<? extends Annotation> subject, Set<Class<? extends Annotation>> annotatedWithSubject, Set<Class<? extends Annotation>> visited) static booleanisAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType) Indicates whether an annotation of givenannotationTypeis present on the givenelement, considering that the givenannotationTypemay be present as a meta annotation on any other annotation on that element.static booleanisAnnotationPresent(AnnotatedElement element, String annotationType) Indicates whether an annotation with givenannotationTypeis present on the givenelement, considering that the givenannotationTypemay be present as a meta annotation on any other annotation on that element.isTypeAnnotatedWith(Class<? extends Annotation> annotationType) Returns a predicate testing if a type of the given instance is annotated with provided annotation type.isTypeAnnotatedWithHavingAttributeValue(Class<? extends Annotation> annotationType, String attributeName, @Nullable Object value) Returns a predicate testing id a type of given instance us annotated with provided annotation type having a single attribute value equals to the provided one.
-
Field Details
-
OVERRIDE_ONLY
public static final boolean OVERRIDE_ONLYBoolean specifying that afindAnnotationAttributes(AnnotatedElement, String, boolean)invocation should only contain the exact attributes of the target annotation, overridden by identical attributes on meta-annotated annotations.- See Also:
-
ADD_ALL
public static final boolean ADD_ALLBoolean specifying that afindAnnotationAttributes(AnnotatedElement, String, boolean)invocation should contain all attributes of the target annotation recursively taken into account attributes of other annotations the target is present on.- See Also:
-
-
Method Details
-
isAnnotationPresent
public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType) Indicates whether an annotation of givenannotationTypeis present on the givenelement, considering that the givenannotationTypemay be present as a meta annotation on any other annotation on that element.- Parameters:
element- The element to inspectannotationType- The type of annotation to find- Returns:
trueif such annotation is present.
-
isAnnotationPresent
Indicates whether an annotation with givenannotationTypeis present on the givenelement, considering that the givenannotationTypemay be present as a meta annotation on any other annotation on that element.- Parameters:
element- The element to inspectannotationType- The name of the annotation to find- Returns:
trueif such annotation is present.
-
findAnnotationAttributes
public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType) Find the attributes of an annotation of typeannotationTypeon the givenelement. The returned optional has a value present if the annotation has been found, either directly on theelement, or as a meta-annotation.The map of attributes contains all the attributes found on the annotation, as well as attributes of any annotations on which the targeted annotation was placed (directly, or indirectly).
Note that the
valueproperty of annotations is reported as the simple class name (lowercase first character) of the annotation. This allows specific attribute overrides for annotations that have multiple meta-annotation with thevalueproperty.- Parameters:
element- The element for find the annotation onannotationType- The type of the annotation to find- Returns:
- an optional that resolved to a map with attribute names and value, if the annotation is found
-
findAnnotationAttributes
public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, Class<? extends Annotation> annotationType, boolean overrideOnly) Find the attributes of an annotation of typeannotationTypeon the givenelement. The returned optional has a value present if the annotation has been found, either directly on theelement, or as a meta-annotation.The map of attributes contains all the attributes found on the annotation. The
overrideOnlyparameter defines whether all attributes of any annotation on which the targeted annotation was placed (directly, or indirectly) should be included. ForOVERRIDE_ONLY, only attribute overrides will be added on top of that, whereas forADD_ALLall attributes on any meta-annotated level will be included in the result.Note that the
valueproperty of annotations is reported as the simple class name (lowercase first character) of the annotation. This allows specific attribute overrides for annotations that have multiple meta-annotation with thevalueproperty.- Parameters:
element- the element for find the annotation onannotationType- the type of the annotation to findoverrideOnly-booleandefining whether or not to only take attribute overrides from meta-annotations into account for the result or to include all attributes from every level- Returns:
- an optional that resolved to a map with attribute names and value, if the annotation is found
-
findAnnotationAttributes
public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, String annotationName) Find the attributes of an annotation with givenannotationNameon the givenelement. The returned optional has a value present if the annotation has been found, either directly on theelement, or as a meta-annotation.The map of attributes contains all the attributes found on the annotation, as well as attributes of any annotations on which the targeted annotation was placed (directly, or indirectly).
- Parameters:
element- The element for find the annotation onannotationName- The name of the annotation to find- Returns:
- an optional that resolved to a map with attribute names and value, if the annotation is found
-
findAnnotationAttributes
public static Optional<Map<String,Object>> findAnnotationAttributes(AnnotatedElement element, String annotationName, boolean overrideOnly) Find the attributes of an annotation with givenannotationNameon the givenelement. The returned optional has a value present if the annotation has been found, either directly on theelement, or as a meta-annotation.The map of attributes contains all the attributes found on the annotation. The
overrideOnlyparameter defines whether all attributes of any annotation on which the targeted annotation was placed (directly, or indirectly) should be included. ForOVERRIDE_ONLY, only attribute overrides will be added on top of that, whereas forADD_ALLall attributes on any meta-annotated level will be included in the result.Note that the
valueproperty of annotations is reported as the simple class name (lowercase first character) of the annotation. This allows specific attribute overrides for annotations that have multiple meta-annotation with thevalueproperty.- Parameters:
element- the element for find the annotation onannotationName- the name of the annotation to findoverrideOnly-booleandefining whether or not to only take attribute overrides from meta-annotations into account for the result or to include all attributes from every level- Returns:
- an optional that resolved to a map with attribute names and value, if the annotation is found
-
findAnnotationAttribute
public static <T> Optional<T> findAnnotationAttribute(AnnotatedElement element, Class<? extends Annotation> annotationType, String attributeName) Find theattributeNameof an annotation of typeannotationTypeon the givenelement. The returned optional has a value present if the annotation has been found, either directly on theelement, or as a meta-annotation, if the named attribute exist.- Parameters:
element- the element to find the annotation onannotationType- the type of the annotation to findattributeName- the name of the attribute to find- Returns:
- an optional that resolved to the attribute value, if the annotation is found and if the attribute exists
-
findAnnotationAttributesOnType
public static Optional<Map<String,Object>> findAnnotationAttributesOnType(Class<?> type, Class<? extends Annotation> annotationType, Predicate<Map<String, Object>> expectedAttributes) Find the attributes of an annotation of typeannotationTypeon the giventype, following a layered search strategy. The search stops as soon as theexpectedAttributespredicate returnstruefor the found attributes.The search follows this ordering:
- The
typeitself - The
enclosing classes(from innermost to outermost) of thetype - The
packageof thetype - The
moduleof thetype
- Parameters:
type- the type to search for the annotation on taking the described orderingannotationType- the type of the annotation to findexpectedAttributes- a predicate that returnstruewhen the desired attribute is found in the attributes map, indicating the search should stop. For example, to search until a non-empty "namespace" attribute is found:attrs -> !StringUtils.emptyOrNull((String) attrs.get("namespace"))- Returns:
- an optional containing the attributes map if the annotation was found and the predicate was satisfied, or an empty optional if the annotation was not found at any level
- The
-
isAnnotatedWith
public static boolean isAnnotatedWith(Class<? extends Annotation> target, Class<? extends Annotation> subject, Set<Class<? extends Annotation>> annotatedWithSubject, Set<Class<? extends Annotation>> visited) Validate whether the giventargetannotationClassis meta-annotated with the givensubject. If this is the case for thetargetitself or any meta-annotation on any level of thetarget,truewill be returned.Any
Annotationclasses which are directly annotated or meta-annotated with the givensubjectwill be stored in theannotatedWithSubjectSet. ThevisitedSetis used to ignore annotations which have already been validated.- Parameters:
target- the annotationClassto validate if it is annotated with the givensubjectsubject- the annotationClassto check whether it is present on the giventarget, directly or through meta-annotationsannotatedWithSubject- aSetto store all class' in which are annotated with thesubject, either directly or through meta-annotationsvisited- aSetcontaining all annotation class' which have been visited in the process to overcome an endless validation loop- Returns:
trueif thetargetor any meta-annotations of thetargetare annotated with thesubject,falseotherwise
-
isAnnotatedWith
Creates aPredicatethat checks whether the givenMemberis annotated with the givenannotationType.- Parameters:
annotationType- The annotation type to check.- Returns:
- Predicate that checks whether the given annotation is present.
-
isTypeAnnotatedWith
Returns a predicate testing if a type of the given instance is annotated with provided annotation type.- Parameters:
annotationType- An annotated type to check for.- Returns:
- The predicate.
-
isTypeAnnotatedWithHavingAttributeValue
public static Predicate<Object> isTypeAnnotatedWithHavingAttributeValue(Class<? extends Annotation> annotationType, String attributeName, @Nullable Object value) Returns a predicate testing id a type of given instance us annotated with provided annotation type having a single attribute value equals to the provided one.- Parameters:
annotationType- The annotation type to check for.attributeName- The name of the attribute.value- The value of the attribute.- Returns:
- The predicate.
-