Class AbstractAnnotationMetadataBuilder<T,A>
- Type Parameters:
T- The element typeA- The annotation type
- Direct Known Subclasses:
GroovyAnnotationMetadataBuilder, JavaAnnotationMetadataBuilder, PythonAnnotationMetadataBuilder
AnnotationMetadata.- Since:
- 1.0
- Author:
- Graeme Rocher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe caching entry. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final @Nullable AnnotatedElementValidatorNames of annotations that should produce deprecation warnings. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidAdds an error.protected abstract voidaddWarning(T originatingElement, String warning) Adds a warning.<A2 extends Annotation>
AnnotationMetadataannotate(AnnotationMetadata annotationMetadata, AnnotationValue<A2> annotationValue) Annotate an existing annotation metadata object.final Optional<AnnotationValue<?>> buildAnnotation(String annotationName) Builds an annotation value for the given annotation type name.buildDeclared(T element) Build only metadata for declared annotations.protected ObjectbuildEvaluatedExpressionReference(T originatingElement, String annotationName, String memberName, 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.protected final AnnotationMetadatabuildInternal(T element) static voidUsed to clear caches at the end of a compilation cycle.static voidUsed to clear mutated metadata at the end of a compilation cycle.static voidclearMutated(Object key) Used to clear mutated metadata at the end of a compilation cycle.voidForgets the annotation types remembered during the processing, which are the fallback used to derive the aliases of an annotation the retained tree holds and to decide whether one isRetainable.static voidThis is used for testing scenarios only where annotation metadata is created without bean creation.protected @Nullable StringfindRepeatableContainerNameForType(String annotationName) Find the repeatable container for given annotation type.final Map<CharSequence, Object> getAnnotationDefaultValues(String annotationName) Resolves the default values declared by the given annotation type.protected <K extends Annotation>
@Nullable List<AnnotationMapper<K>> getAnnotationMappers(String annotationName) Obtain the annotation mappers for the given annotation name.protected abstract @Nullable TgetAnnotationMember(T annotationElement, CharSequence member) Get the annotation member.protected abstract StringgetAnnotationMemberName(T member) Read the name of an annotation member.getAnnotationMirror(String annotationName) Return a mirror for the given annotation.protected List<AnnotationRemapper> getAnnotationRemappers(String packageName) Obtain the remappers for the given annotation package.getAnnotationsForType(T element) Obtain the annotations for the given type.protected <K extends Annotation>
@Nullable List<AnnotationTransformer<K>> getAnnotationTransformers(String annotationName) Obtain the transformers mappers for the given annotation name.protected abstract StringgetAnnotationTypeName(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 Map<CharSequence, Object> getCachedAnnotationDefaults(String annotationName, T annotationType) Get the cached annotation defaults.protected abstract StringgetElementName(T element) Get the name for the given element.protected @Nullable AnnotatedElementValidatorObtains the element validator.protected abstract @Nullable StringgetOriginatingClassName(T orginatingElement) protected abstract @Nullable StringgetRepeatableContainerNameForType(T annotationType) Obtain the name of the repeatable annotation if the annotation is one.protected abstract @Nullable StringgetRepeatableName(A annotationMirror) Obtain the name of the repeatable annotation if the annotation is one.getRetentionPolicy(String annotation) Gets the retention policy for the given annotation.protected abstract RetentionPolicygetRetentionPolicy(T annotation) Gets the retention policy for the given annotation.protected abstract TgetTypeForAnnotation(A annotationMirror) Get the type of the given annotation.protected abstract VisitorContextReturns the visitor context for this implementation.protected abstract booleanhasAnnotation(T element, Class<? extends Annotation> annotation) Checks whether an annotation is present.protected abstract booleanhasAnnotation(T element, String annotation) Checks whether an annotation is present.protected abstract booleanhasAnnotations(T element) Checks whether any annotations are present on the given element.protected booleanisEvaluatedExpression(@Nullable Object value) Detect evaluated expression in annotation value.protected booleanisExcludedAnnotation(T element, String annotationName) Is the given annotation excluded for the specified element.protected booleanisRepeatableAnnotationContainer(AnnotationValue<?> annotationValue) protected abstract booleanisValidationRequired(T member) Return whether the given member requires validation.lookupOrBuild(Object key, T element) Lookup or build new annotation metadata.lookupOrBuildForField(T declaringType, T element) Build the metadata for the given field element excluding any class metadata.lookupOrBuildForField(T owningType, T declaringType, T element, boolean propertyComponent) Build the metadata for the given field element read through the given owning type, excluding any class metadata.lookupOrBuildForMethod(T owningType, T element) Build the metadata for the given method element excluding any class metadata.lookupOrBuildForOwner(Object sharedKey, Object ownerKey, T element, boolean ownerMutation) Lookup or build the metadata of a member shared by the types it is read through, keeping the mutations made for one owning type apart from the shared metadata.lookupOrBuildForParameter(T declaringType, T methodElement, T parameterElement) Build the metadata for the given parameter element.lookupOrBuildForType(T typeElement) Build the metadata for the given element.protected voidpostProcess(MutableAnnotationMetadata mutableAnnotationMetadata, T element) readAnnotationDefaultValues(String annotationName, T annotationType) Read the raw default annotation values from the given annotation.readAnnotationDefaultValues(String annotationName, T annotationType, boolean includeEmptyValues) Read the raw default annotation values from the given annotation.readAnnotationRawValues(A annotationMirror) Read the raw annotation values from the given annotation.protected abstract voidreadAnnotationRawValues(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 voidreadAnnotationRawValues(T originatingElement, String annotationName, T member, String memberName, Object annotationValue, Map<CharSequence, Object> annotationValues, Map<String, Map<CharSequence, Object>> resolvedDefaults) Read the given member and value, applying conversions if necessary, and place the data in the given map.protected abstract ObjectreadAnnotationValue(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) protected AnnotationValue<?> readNestedAnnotationValue(T annotationElement, A annotationType, Map<String, Map<CharSequence, Object>> resolvedDefaults) removeAnnotation(AnnotationMetadata annotationMetadata, String annotationType) Removes an annotation from the given annotation metadata.<T1 extends Annotation>
AnnotationMetadataremoveAnnotationIf(AnnotationMetadata annotationMetadata, Predicate<AnnotationValue<T1>> predicate) Removes an annotation from the metadata for the given predicate.removeStereotype(AnnotationMetadata annotationMetadata, String annotationType) Removes an annotation from the given annotation metadata.protected voidvalidateAnnotationValue(T originatingElement, String annotationName, T member, String memberName, Object resolvedValue) Validates an annotation value.
-
Field Details
-
ELEMENT_VALIDATOR
Names of annotations that should produce deprecation warnings. The key in the map is the deprecated annotation the value the replacement.
-
-
Constructor Details
-
AbstractAnnotationMetadataBuilder
protected AbstractAnnotationMetadataBuilder()Default constructor.
-
-
Method Details
-
buildAnnotation
Builds an annotation value for the given annotation type name.- Parameters:
annotationName- The annotation name- Returns:
- The built annotation value, if the annotation type can be resolved
-
getAnnotationDefaultValues
Resolves the default values declared by the given annotation type.- Parameters:
annotationName- The annotation type name- Returns:
- The default values, or an empty map if the annotation type cannot be resolved
- Since:
- 5.1.0
-
buildDeclared
Build only metadata for declared annotations.- Parameters:
element- The element- Returns:
- The
AnnotationMetadata
-
lookupOrBuildForParameter
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForParameter(T declaringType, T methodElement, T parameterElement) Build the metadata for the given parameter element. The metadata is keyed by the type that declares the method, not by the type the method is reached through: a parameter of an inherited method is the same parameter whether it is read through the declaring class or through a subclass, so a mutation made through one must be visible through the other. Unlike a method, a parameter's metadata never includes the owning type's annotations, so nothing distinguishes it per owner.- Parameters:
declaringType- The type declaring the methodmethodElement- The method elementparameterElement- The parameter element- Returns:
- The
AnnotationMetadata
-
lookupOrBuildForType
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForType(T typeElement) Build the metadata 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 declaringType, T element) Build the metadata for the given field element excluding any class metadata. The metadata is keyed by the type that declares the field, not by the type the field is reached through: an inherited field is the same field whether it is read through the declaring class or through a subclass, so a mutation made through one must be visible through the other.- Parameters:
declaringType- The type declaring the fieldelement- The element- Returns:
- The
AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata
-
lookupOrBuildForField
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForField(T owningType, T declaringType, T element, boolean propertyComponent) Build the metadata for the given field element read through the given owning type, excluding any class metadata. A mutation made on the field itself is keyed by the declaring type, as withlookupOrBuildForField(Object, Object), and is visible through every type the field is read through. A mutation made on the field as a component of a bean property belongs to the owning type: a bean property is resolved for the type it is read through, as its accessor methods are, so annotating the property of a super type must not annotate the same property read through a subclass. Once the owning type holds such a mutation, the field read or mutated through the owning type uses it.- Parameters:
owningType- The type the field is read throughdeclaringType- The type declaring the fieldelement- The elementpropertyComponent- Whether a mutation is made on the field as a component of a bean property- Returns:
- The
AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata - Since:
- 5.2.1
-
lookupOrBuildForOwner
public AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata lookupOrBuildForOwner(Object sharedKey, Object ownerKey, T element, boolean ownerMutation) Lookup or build the metadata of a member shared by the types it is read through, keeping the mutations made for one owning type apart from the shared metadata.- Parameters:
sharedKey- The cache key of the metadata shared by every owning typeownerKey- The cache key of the metadata mutated for the owning typeelement- The elementownerMutation- Whether a mutation belongs to the owning type- Returns:
- The
AbstractAnnotationMetadataBuilder.CachedAnnotationMetadata - Since:
- 5.2.1
- See Also:
-
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
-
buildInternal
-
getTypeForAnnotation
-
hasAnnotation
Checks whether an annotation is present.- Parameters:
element- The elementannotation- The annotation type- Returns:
- True if the annotation is present
-
hasAnnotation
-
hasAnnotations
Checks whether any annotations are present on the given element.- Parameters:
element- The element- Returns:
- True if the annotation is present
-
getAnnotationTypeName
-
getElementName
-
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
-
readAnnotationRawValues
protected void readAnnotationRawValues(T originatingElement, String annotationName, T member, String memberName, Object annotationValue, Map<CharSequence, Object> annotationValues, Map<String, Map<CharSequence, Object>> resolvedDefaults) 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 populateresolvedDefaults- The resolved defaults- Since:
- 4.3.0
-
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
-
addWarning
-
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
-
readAnnotationDefaultValues
protected Map<? extends T, ?> readAnnotationDefaultValues(String annotationName, T annotationType, boolean includeEmptyValues) Read the raw default annotation values from the given annotation.Implementations must report a default for every member that declares one, whatever the shape of the default expression: a constant, an enum constant, a class literal, an array literal (including the empty array) or a nested annotation.
includeEmptyValuesonly concerns the empty string: when it is not set, a member whose default is the empty string is reported as having no default, which keeps the written annotation metadata smaller. An empty array default is always reported. SeeJavaAnnotationMetadataBuilder#isValidDefaultValuefor the rationale. All language implementations must apply this rule identically.- Parameters:
annotationName- annotation nameannotationType- the typeincludeEmptyValues- Whether empty values should be included- Returns:
- The values
- Since:
- 5.1.0
-
readAnnotationRawValues
-
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
-
getRepeatableName
-
getRepeatableContainerNameForType
-
readNestedAnnotationValue
- Parameters:
annotationElement- The annotation elementannotationType- The annotation type- Returns:
- The annotation value
-
readNestedAnnotationValue
protected AnnotationValue<?> readNestedAnnotationValue(T annotationElement, A annotationType, Map<String, Map<CharSequence, Object>> resolvedDefaults) - Parameters:
annotationElement- The annotation elementannotationType- The annotation typeresolvedDefaults- resoldved defaults- Returns:
- The annotation value
-
getAnnotationMirror
-
isEvaluatedExpression
Detect evaluated expression in annotation value.- Parameters:
value- - Annotation value- Returns:
- if value contains evaluated expression
-
buildEvaluatedExpressionReference
protected Object buildEvaluatedExpressionReference(T originatingElement, String annotationName, String memberName, 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
protected <K extends Annotation> @Nullable List<AnnotationMapper<K>> getAnnotationMappers(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
protected <K extends Annotation> @Nullable List<AnnotationTransformer<K>> getAnnotationTransformers(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
-
getAnnotationRemappers
Obtain the remappers for the given annotation package.- Parameters:
packageName- The annotation package- Returns:
- The remappers
- Since:
- 5.2.0
-
getVisitorContext
Returns 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
protected Map<CharSequence, Object> getCachedAnnotationDefaults(String annotationName, T annotationType) Get the cached annotation defaults.- Parameters:
annotationName- The annotation nameannotationType- The annotation type- Returns:
- The defaults
-
isRepeatableAnnotationContainer
- Parameters:
annotationValue- The annotation value- Returns:
- true if the annotation is a repeatable container
-
findRepeatableContainerNameForType
-
isExcludedAnnotation
-
clearMutated
Used to clear mutated metadata at the end of a compilation cycle. -
clearMutated
-
clearProcessedAnnotationTypes
Forgets the annotation types remembered during the processing, which are the fallback used to derive the aliases of an annotation the retained tree holds and to decide whether one isRetainable.An implementation that rebuilds its builder for every processing round need not call this. One that reuses a builder across rounds has to call it at the boundary: the entries are native elements of the session that has ended, and the map is keyed by annotation name, so a later round would hit and be handed a dead element rather than miss.
-
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
-
getMappedAnnotationPackages
-
annotate
public <A2 extends Annotation> AnnotationMetadata annotate(AnnotationMetadata annotationMetadata, 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
public AnnotationMetadata removeAnnotation(AnnotationMetadata annotationMetadata, 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
public AnnotationMetadata removeStereotype(AnnotationMetadata annotationMetadata, 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
public <T1 extends Annotation> AnnotationMetadata removeAnnotationIf(AnnotationMetadata annotationMetadata, 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
-