Package io.micronaut.inject.annotation
Class AbstractAnnotationMetadataBuilder<T,A>
java.lang.Object
io.micronaut.inject.annotation.AbstractAnnotationMetadataBuilder<T,A>
- Type Parameters:
T
- The element typeA
- The annotation type
- Direct Known Subclasses:
GroovyAnnotationMetadataBuilder
,JavaAnnotationMetadataBuilder
An abstract implementation that builds
AnnotationMetadata
.- Since:
- 1.0
- Author:
- Graeme Rocher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
The caching entry. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
Adds an error.protected abstract void
addWarning
(T originatingElement, @NonNull String warning) Adds an warning.<A2 extends Annotation>
@NonNull AnnotationMetadataannotate
(@NonNull AnnotationMetadata annotationMetadata, @NonNull AnnotationValue<A2> annotationValue) Annotate an existing annotation metadata object.buildDeclared
(T element) Build only metadata for declared annotations.buildEvaluatedExpressionReference
(T originatingElement, @NonNull String annotationName, @NonNull String memberName, @NonNull Object initialAnnotationValue) Wraps original annotation value to make it processable at later stages.buildHierarchy
(T element, boolean inheritTypeAnnotations, boolean declaredOnly) Build the type hierarchy for the given element.static void
Used to clear caches at the end of a compilation cycle.static void
Used to clear mutated metadata at the end of a compilation cycle.static void
This is used for testing scenarios only where annotation metadata is created without bean creation.protected abstract VisitorContext
Creates the visitor context for this implementation.protected <K extends Annotation>
@NonNull List<AnnotationMapper<K>>getAnnotationMappers
(@NonNull String annotationName) Obtain the annotation mappers for the given annotation name.protected abstract T
getAnnotationMember
(T annotationElement, CharSequence member) Get the annotation member.protected abstract String
getAnnotationMemberName
(T member) Read the name of an annotation member.getAnnotationMirror
(String annotationName) Return a mirror for the given annotation.getAnnotationsForType
(T element) Obtain the annotations for the given type.protected <K extends Annotation>
@NonNull List<AnnotationTransformer<K>>getAnnotationTransformers
(@NonNull String annotationName) Obtain the transformers mappers for the given annotation name.protected abstract String
getAnnotationTypeName
(A annotationMirror) Get the given type of the annotation.protected abstract <K extends Annotation>
Optional<AnnotationValue<K>>getAnnotationValues
(T originatingElement, T member, Class<K> annotationType) Resolve the annotations values from the given member for the given type.protected @NonNull Map<CharSequence,
Object> getCachedAnnotationDefaults
(String annotationName, T annotationType) Get the cached annotation defaults.protected abstract String
getElementName
(T element) Get the name for the given element.protected @Nullable AnnotatedElementValidator
Obtains the element validator.getOriginatingClassName
(T orginatingElement) getRepeatableName
(A annotationMirror) Obtain the name of the repeatable annotation if the annotation is is one.getRepeatableNameForType
(T annotationType) Obtain the name of the repeatable annotation if the annotation is is one.getRetentionPolicy
(@NonNull String annotation) Gets the retention policy for the given annotation.protected abstract @NonNull RetentionPolicy
getRetentionPolicy
(T annotation) Gets the retention policy for the given annotation.protected abstract T
getTypeForAnnotation
(A annotationMirror) Get the type of the given annotation.protected abstract boolean
hasAnnotation
(T element, Class<? extends Annotation> annotation) Checks whether an annotation is present.protected abstract boolean
hasAnnotation
(T element, String annotation) Checks whether an annotation is present.protected abstract boolean
hasAnnotations
(T element) Checks whether any annotations are present on the given element.protected boolean
isEvaluatedExpression
(@Nullable Object value) Detect evaluated expression in annotation value.protected boolean
isExcludedAnnotation
(T element, @NonNull String annotationName) Is the given annotation excluded for the specified element.protected abstract boolean
isValidationRequired
(T member) Return whether the given member requires validation.lookupOrBuild
(Object key, T element) Lookup or build new annotation metadata.lookupOrBuildForField
(T owningType, T element) Build the metadata for the given field element excluding any class metadata.lookupOrBuildForMethod
(T owningType, T element) Build the metadata for the given method element excluding any class metadata.lookupOrBuildForParameter
(T owningType, T methodElement, T parameterElement) Build the meta data for the given element.lookupOrBuildForType
(T typeElement) Build the meta data for the given element.protected void
postProcess
(MutableAnnotationMetadata mutableAnnotationMetadata, T element) readAnnotationDefaultValues
(String annotationName, T annotationType) Read the raw default annotation values from the given annotation.readAnnotationRawValues
(A annotationMirror) Read the raw annotation values from the given annotation.protected abstract void
readAnnotationRawValues
(T originatingElement, String annotationName, T member, String memberName, Object annotationValue, Map<CharSequence, Object> annotationValues) Read the given member and value, applying conversions if necessary, and place the data in the given map.protected abstract Object
readAnnotationValue
(T originatingElement, T member, String annotationName, String memberName, Object annotationValue) Read the given member and value, applying conversions if necessary, and place the data in the given map.protected AnnotationValue<?>
readNestedAnnotationValue
(T annotationElement, A annotationType) removeAnnotation
(@NonNull AnnotationMetadata annotationMetadata, @NonNull String annotationType) Removes an annotation from the given annotation metadata.<T1 extends Annotation>
@NonNull AnnotationMetadataremoveAnnotationIf
(@NonNull AnnotationMetadata annotationMetadata, @NonNull Predicate<AnnotationValue<T1>> predicate) Removes an annotation from the metadata for the given predicate.removeStereotype
(@NonNull AnnotationMetadata annotationMetadata, @NonNull String annotationType) Removes an annotation from the given annotation metadata.protected void
validateAnnotationValue
(T originatingElement, String annotationName, T member, String memberName, Object resolvedValue) Validates an annotation value.
-
Constructor Details
-
AbstractAnnotationMetadataBuilder
protected AbstractAnnotationMetadataBuilder()Default constructor.
-
-
Method Details
-
buildDeclared
Build only metadata for declared annotations.- Parameters:
element
- The element- Returns:
- The
AnnotationMetadata
-
lookupOrBuildForParameter
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForParameter(T owningType, T methodElement, T parameterElement) Build the meta data for the given element. If the element is a method the class metadata will be included.- Parameters:
owningType
- The owning typemethodElement
- The method elementparameterElement
- The parameter element- Returns:
- The
AnnotationMetadata
-
lookupOrBuildForType
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForType(T typeElement) Build the meta data for the given element.- Parameters:
typeElement
- The element- Returns:
- The
AnnotationMetadata
-
lookupOrBuildForMethod
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForMethod(T owningType, T element) Build the metadata for the given method element excluding any class metadata.- Parameters:
owningType
- The owningTypeelement
- The element- Returns:
- The
AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata
-
lookupOrBuildForField
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForField(T owningType, T element) Build the metadata for the given field element excluding any class metadata.- Parameters:
owningType
- The owningTypeelement
- The element- Returns:
- The
AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata
-
lookupOrBuild
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuild(Object key, T element) Lookup or build new annotation metadata.- Parameters:
key
- The cache keyelement
- The type element- Returns:
- The annotation metadata
- Since:
- 4.0.0
-
getTypeForAnnotation
Get the type of the given annotation.- Parameters:
annotationMirror
- The annotation- Returns:
- The type
-
hasAnnotation
Checks whether an annotation is present.- Parameters:
element
- The elementannotation
- The annotation type- Returns:
- True if the annotation is present
-
hasAnnotation
Checks whether an annotation is present.- Parameters:
element
- The elementannotation
- The annotation type name- Returns:
- True if the annotation is present
-
hasAnnotations
Checks whether any annotations are present on the given element.- Parameters:
element
- The element- Returns:
- True if the annotation is present
-
getAnnotationTypeName
Get the given type of the annotation.- Parameters:
annotationMirror
- The annotation- Returns:
- The type
-
getElementName
Get the name for the given element.- Parameters:
element
- The element- Returns:
- The name
-
getAnnotationsForType
Obtain the annotations for the given type. This method is also responsible for unwrapping repeatable annotations.For example,
@Parent(value = {@Child, @Child})
should result in the two child annotations being returned from this method instead of the parent annotation.- Parameters:
element
- The type element- Returns:
- The annotations
-
buildHierarchy
protected abstract List<T> buildHierarchy(T element, boolean inheritTypeAnnotations, boolean declaredOnly) Build the type hierarchy for the given element.- Parameters:
element
- The elementinheritTypeAnnotations
- Whether to inherit type annotationsdeclaredOnly
- Whether to only include declared annotations- Returns:
- The type hierarchy
-
readAnnotationRawValues
protected abstract void readAnnotationRawValues(T originatingElement, String annotationName, T member, String memberName, Object annotationValue, Map<CharSequence, Object> annotationValues) Read the given member and value, applying conversions if necessary, and place the data in the given map.- Parameters:
originatingElement
- The originating elementannotationName
- The annotation namemember
- The member being read frommemberName
- The memberannotationValue
- The valueannotationValues
- The values to populate
-
validateAnnotationValue
protected void validateAnnotationValue(T originatingElement, String annotationName, T member, String memberName, Object resolvedValue) Validates an annotation value.- Parameters:
originatingElement
- The originating elementannotationName
- The annotation namemember
- The membermemberName
- The member nameresolvedValue
- The resolved value
-
isValidationRequired
Return whether the given member requires validation.- Parameters:
member
- The member- Returns:
- True if it is
-
getElementValidator
Obtains the element validator.- Returns:
- The validator.
-
addError
Adds an error.- Parameters:
originatingElement
- The originating elementerror
- The error
-
addWarning
Adds an warning.- Parameters:
originatingElement
- The originating elementwarning
- The warning
-
readAnnotationValue
protected abstract Object readAnnotationValue(T originatingElement, T member, String annotationName, String memberName, Object annotationValue) Read the given member and value, applying conversions if necessary, and place the data in the given map.- Parameters:
originatingElement
- The originating elementmember
- The memberannotationName
- The annotation namememberName
- The member nameannotationValue
- The value- Returns:
- The object
-
readAnnotationDefaultValues
protected abstract Map<? extends T,?> readAnnotationDefaultValues(String annotationName, T annotationType) Read the raw default annotation values from the given annotation.- Parameters:
annotationName
- annotation nameannotationType
- the type- Returns:
- The values
-
readAnnotationRawValues
Read the raw annotation values from the given annotation.- Parameters:
annotationMirror
- The annotation- Returns:
- The values
-
getAnnotationValues
protected abstract <K extends Annotation> Optional<AnnotationValue<K>> getAnnotationValues(T originatingElement, T member, Class<K> annotationType) Resolve the annotations values from the given member for the given type.- Type Parameters:
K
- The annotation type- Parameters:
originatingElement
- The originating elementmember
- The memberannotationType
- The type- Returns:
- The values
-
getAnnotationMemberName
Read the name of an annotation member.- Parameters:
member
- The member- Returns:
- The name
-
getRepeatableName
Obtain the name of the repeatable annotation if the annotation is is one.- Parameters:
annotationMirror
- The annotation mirror- Returns:
- Return the name or null
-
getRepeatableNameForType
Obtain the name of the repeatable annotation if the annotation is is one.- Parameters:
annotationType
- The annotation mirror- Returns:
- Return the name or null
-
readNestedAnnotationValue
- Parameters:
annotationElement
- The annotation elementannotationType
- The annotation type- Returns:
- The annotation value
-
getAnnotationMirror
Return a mirror for the given annotation.- Parameters:
annotationName
- The annotation name- Returns:
- An optional mirror
-
isEvaluatedExpression
Detect evaluated expression in annotation value.- Parameters:
value
- - Annotation value- Returns:
- if value contains evaluated expression
-
buildEvaluatedExpressionReference
@NonNull protected @NonNull Object buildEvaluatedExpressionReference(@NonNull T originatingElement, @NonNull @NonNull String annotationName, @NonNull @NonNull String memberName, @NonNull @NonNull Object initialAnnotationValue) Wraps original annotation value to make it processable at later stages.- Parameters:
originatingElement
- originating annotated elementannotationName
- annotation namememberName
- annotation member nameinitialAnnotationValue
- original annotation value- Returns:
- expression reference
-
getOriginatingClassName
-
getAnnotationMember
Get the annotation member.- Parameters:
annotationElement
- The annotation elementmember
- The member- Returns:
- The annotation member element
-
getAnnotationMappers
@NonNull protected <K extends Annotation> @NonNull List<AnnotationMapper<K>> getAnnotationMappers(@NonNull @NonNull String annotationName) Obtain the annotation mappers for the given annotation name.- Type Parameters:
K
- The annotation type- Parameters:
annotationName
- The annotation name- Returns:
- The mappers
-
getAnnotationTransformers
@NonNull protected <K extends Annotation> @NonNull List<AnnotationTransformer<K>> getAnnotationTransformers(@NonNull @NonNull String annotationName) Obtain the transformers mappers for the given annotation name.- Type Parameters:
K
- The annotation type- Parameters:
annotationName
- The annotation name- Returns:
- The transformers
-
createVisitorContext
Creates the visitor context for this implementation.- Returns:
- The visitor context
-
getRetentionPolicy
Gets the retention policy for the given annotation.- Parameters:
annotation
- The annotation- Returns:
- The retention policy
-
getRetentionPolicy
Gets the retention policy for the given annotation.- Parameters:
annotation
- The annotation- Returns:
- The retention policy
-
postProcess
-
getCachedAnnotationDefaults
@NonNull protected @NonNull Map<CharSequence,Object> getCachedAnnotationDefaults(String annotationName, T annotationType) Get the cached annotation defaults.- Parameters:
annotationName
- The annotation nameannotationType
- The annotation type- Returns:
- The defaults
-
isExcludedAnnotation
Is the given annotation excluded for the specified element.- Parameters:
element
- The elementannotationName
- The annotation name- Returns:
- True if it is excluded
-
clearMutated
Used to clear mutated metadata at the end of a compilation cycle. -
clearCaches
Used to clear caches at the end of a compilation cycle. -
copyToRuntime
This is used for testing scenarios only where annotation metadata is created without bean creation. It is needed because at compile time there are no defaults added via DefaultAnnotationMetadata. -
getMappedAnnotationNames
- Returns:
- Additional mapped annotation names
-
getMappedAnnotationPackages
- Returns:
- Additional mapped annotation names
-
annotate
@NonNull public <A2 extends Annotation> @NonNull AnnotationMetadata annotate(@NonNull @NonNull AnnotationMetadata annotationMetadata, @NonNull @NonNull AnnotationValue<A2> annotationValue) Annotate an existing annotation metadata object.- Type Parameters:
A2
- The annotation type- Parameters:
annotationMetadata
- The annotation metadataannotationValue
- The annotation value- Returns:
- The mutated metadata
-
removeAnnotation
@NonNull public @NonNull AnnotationMetadata removeAnnotation(@NonNull @NonNull AnnotationMetadata annotationMetadata, @NonNull @NonNull String annotationType) Removes an annotation from the given annotation metadata.- Parameters:
annotationMetadata
- The annotation metadataannotationType
- The annotation type- Returns:
- The updated metadata
- Since:
- 3.0.0
-
removeStereotype
@NonNull public @NonNull AnnotationMetadata removeStereotype(@NonNull @NonNull AnnotationMetadata annotationMetadata, @NonNull @NonNull String annotationType) Removes an annotation from the given annotation metadata.- Parameters:
annotationMetadata
- The annotation metadataannotationType
- The annotation type- Returns:
- The updated metadata
- Since:
- 3.0.0
-
removeAnnotationIf
@NonNull public <T1 extends Annotation> @NonNull AnnotationMetadata removeAnnotationIf(@NonNull @NonNull AnnotationMetadata annotationMetadata, @NonNull @NonNull Predicate<AnnotationValue<T1>> predicate) Removes an annotation from the metadata for the given predicate.- Type Parameters:
T1
- The annotation type- Parameters:
annotationMetadata
- The annotation metadatapredicate
- The predicate- Returns:
- The potentially modified metadata
-