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.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.
-
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
-
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
-