Package io.micronaut.inject.writer
Class AbstractBeanDefinitionBuilder
java.lang.Object
io.micronaut.inject.writer.AbstractBeanDefinitionBuilder
- All Implemented Interfaces:
AnnotatedElement
,AnnotationMetadata
,AnnotationMetadataDelegate
,AnnotationMetadataProvider
,AnnotationSource
,Described
,Named
,MutableAnnotationMetadataDelegate<Element>
,BeanElementBuilder
,ConfigurableElement
,Element
@Internal
public abstract class AbstractBeanDefinitionBuilder
extends Object
implements BeanElementBuilder
Abstract implementation of the
BeanElementBuilder
interface that should be implemented by downstream language specific implementations.- Since:
- 3.0.0
- Author:
- graemerocher
-
Field Summary
Modifier and TypeFieldDescriptionprotected final ElementAnnotationMetadataFactory
protected final ConfigurationMetadataBuilder
protected final VisitorContext
Fields inherited from interface io.micronaut.core.annotation.AnnotationMetadata
CLASS_NAME_SUFFIX, EMPTY_METADATA, VALUE_MEMBER
Fields inherited from interface io.micronaut.core.annotation.AnnotationSource
EMPTY
Fields inherited from interface io.micronaut.inject.ast.Element
EMPTY_ELEMENT_ARRAY
Fields inherited from interface io.micronaut.inject.ast.annotation.MutableAnnotationMetadataDelegate
EMPTY
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractBeanDefinitionBuilder
(Element originatingElement, ClassElement beanType, ConfigurationMetadataBuilder metadataBuilder, VisitorContext visitorContext, ElementAnnotationMetadataFactory elementAnnotationMetadataFactory) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract <T extends Annotation>
voidannotate
(@NonNull AnnotationMetadata annotationMetadata, @NonNull AnnotationValue<T> annotationValue) Add an annotation to the given metadata.<T extends Annotation>
@NonNull BeanElementBuilderAnnotate this element with the given annotation type.protected abstract <T extends Annotation>
voidannotate
(AnnotationMetadata annotationMetadata, String annotationType, Consumer<AnnotationValueBuilder<T>> consumer) Add an annotation to the given metadata.<T extends Annotation>
Elementannotate
(AnnotationValue<T> annotationValue) Annotate this element with the given annotation type.build()
Build the bean definition writer.protected boolean
configureBeanVisitor
(BeanDefinitionVisitor beanDefinitionWriter) Configure the bean visitor for this builder.protected abstract @NonNull BeanDefinitionVisitor
createAopWriter
(BeanDefinitionWriter beanDefinitionWriter, AnnotationMetadata annotationMetadata) Creates the AOP writer.protected abstract @NonNull BiConsumer<TypedElement,
MethodElement> createAroundMethodVisitor
(BeanDefinitionVisitor aopProxyWriter) Creates the around method visitor.protected BeanDefinitionVisitor
protected abstract @NonNull AbstractBeanDefinitionBuilder
createChildBean
(FieldElement producerField) Creates a child bean for the given producer field.protected abstract @NonNull AbstractBeanDefinitionBuilder
createChildBean
(MethodElement producerMethod) Creates a child bean for the given producer method.createWith
(@NonNull MethodElement element) Specifies the bean will be created with the given method element.protected void
finalizeAndWriteBean
(ClassWriterOutputVisitor classWriterOutputVisitor, BeanDefinitionVisitor beanDefinitionWriter) Finish the given bean and write it to the output.Supplies the metadata.Any child bean definitions.getName()
Returns the native underlying type.protected @NonNull BeanParameterElement[]
protected final BeanParameterElement[]
initBeanParameters
(@NonNull ParameterElement[] constructorParameters) Initialize the bean parameters.inject()
Dependency inject this bean.intercept
(AnnotationValue<?>... annotationValue) Intercept the bean.protected boolean
Is the bean to be built intercepted?boolean
boolean
isPublic()
<E extends MemberElement>
BeanElementBuilderproduceBeans
(ElementQuery<E> methodsOrFields, Consumer<BeanElementBuilder> childBeanBuilder) Produce additional beans from the given methods.removeAnnotation
(@NonNull String annotationType) Removes an annotation of the given type from the element.protected abstract void
removeAnnotation
(AnnotationMetadata annotationMetadata, String annotationType) Remove an annotation for the given name.<T extends Annotation>
BeanElementBuilderremoveAnnotationIf
(@NonNull Predicate<AnnotationValue<T>> predicate) Removes all annotations that pass the given predicate.protected abstract <T extends Annotation>
voidremoveAnnotationIf
(AnnotationMetadata annotationMetadata, Predicate<AnnotationValue<T>> predicate) Remove an annotation if it matches the given condition.removeStereotype
(@NonNull String annotationType) Removes a stereotype of the given name from the element.protected abstract void
removeStereotype
(AnnotationMetadata annotationMetadata, String annotationType) Remove a stereotype from the given metadata.typeArguments
(@NonNull ClassElement... types) Fills the type arguments for the bean with the given types.typeArgumentsForType
(ClassElement type, @NonNull ClassElement... types) Fills the type arguments for the given interface or super class with the given types.typed
(ClassElement... types) Alters the exposed types for the bean limiting the exposed type to the given types.protected void
Visit the intercepted methods of this type.withConstructor
(Consumer<BeanConstructorElement> constructorElement) Allows configuring the bean constructor.withFields
(@NonNull ElementQuery<FieldElement> fields, @NonNull Consumer<BeanFieldElement> beanFields) Allows configuring fields of the bean.withMethods
(@NonNull ElementQuery<MethodElement> methods, @NonNull Consumer<BeanMethodElement> beanMethods) Allows configuring methods of the bean.withParameters
(Consumer<BeanParameterElement[]> parameters) Allows configuring the parameters for the current constructor.static void
writeBeanDefinitionBuilders
(ClassWriterOutputVisitor classWriterOutputVisitor, List<AbstractBeanDefinitionBuilder> beanDefinitionBuilders) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micronaut.core.annotation.AnnotatedElement
isDeclaredNonNull, isDeclaredNullable, isNonNull, isNullable
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadata
enumValuesSet, enumValuesSet, getValues, hasDeclaredStereotype, hasEvaluatedExpressions, hasStereotypeNonRepeating, isAnnotationPresent, isDeclaredAnnotationPresent
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataDelegate
booleanValue, booleanValue, booleanValue, booleanValue, classValue, classValue, classValue, classValue, classValues, classValues, classValues, classValues, copyAnnotationMetadata, doubleValue, doubleValue, doubleValue, enumValue, enumValue, enumValue, enumValue, enumValues, enumValues, enumValues, enumValues, findAnnotation, findAnnotation, findDeclaredAnnotation, findDeclaredAnnotation, findRepeatableAnnotation, findRepeatableAnnotation, getAnnotation, getAnnotation, getAnnotationNameByStereotype, getAnnotationNameByStereotype, getAnnotationNames, getAnnotationNamesByStereotype, getAnnotationNamesByStereotype, getAnnotationType, getAnnotationType, getAnnotationTypeByStereotype, getAnnotationTypeByStereotype, getAnnotationTypesByStereotype, getAnnotationTypesByStereotype, getAnnotationTypesByStereotype, getAnnotationValuesByName, getAnnotationValuesByStereotype, getAnnotationValuesByType, getDeclaredAnnotation, getDeclaredAnnotation, getDeclaredAnnotationNameByStereotype, getDeclaredAnnotationNames, getDeclaredAnnotationNamesByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationValuesByName, getDeclaredAnnotationValuesByType, getDeclaredMetadata, getDeclaredStereotypeAnnotationNames, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValues, getStereotypeAnnotationNames, getTargetAnnotationMetadata, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValue, getValues, getValues, hasAnnotation, hasAnnotation, hasDeclaredAnnotation, hasDeclaredAnnotation, hasDeclaredStereotype, hasDeclaredStereotype, hasDeclaredStereotype, hasPropertyExpressions, hasSimpleAnnotation, hasSimpleDeclaredAnnotation, hasStereotype, hasStereotype, hasStereotype, hasStereotype, intValue, intValue, intValue, isAnnotationPresent, isDeclaredAnnotationPresent, isEmpty, isFalse, isFalse, isPresent, isPresent, isRepeatableAnnotation, isRepeatableAnnotation, isTrue, isTrue, longValue, longValue, stringValue, stringValue, stringValue, stringValue, stringValues, stringValues, stringValues, stringValues, synthesize, synthesize, synthesizeAll, synthesizeAnnotationsByType, synthesizeDeclared, synthesizeDeclared, synthesizeDeclared, synthesizeDeclaredAnnotationsByType
Methods inherited from interface io.micronaut.inject.ast.beans.BeanElementBuilder
annotate, annotate, annotate, getDeclaringElement, getProducingElement, produceBeans, qualifier, qualifier, removeAnnotation, removeStereotype, scope, scope
Methods inherited from interface io.micronaut.inject.ast.Element
getDescription, getDescription, getDocumentation, getModifiers, getSimpleName, isAbstract, isFinal, isPackagePrivate, isPrivate, isStatic, isSynthetic, withAnnotationMetadata
-
Field Details
-
metadataBuilder
-
visitorContext
-
elementAnnotationMetadataFactory
-
-
Constructor Details
-
AbstractBeanDefinitionBuilder
protected AbstractBeanDefinitionBuilder(Element originatingElement, ClassElement beanType, ConfigurationMetadataBuilder metadataBuilder, VisitorContext visitorContext, ElementAnnotationMetadataFactory elementAnnotationMetadataFactory) Default constructor.- Parameters:
originatingElement
- The originating elementbeanType
- The bean typemetadataBuilder
- the metadata buildervisitorContext
- the visitor contextelementAnnotationMetadataFactory
- The element annotation metadata factory
-
-
Method Details
-
intercept
Description copied from interface:BeanElementBuilder
Intercept the bean.- Specified by:
intercept
in interfaceBeanElementBuilder
- Parameters:
annotationValue
- The annotation to intercept- Returns:
- This bean method
-
writeBeanDefinitionBuilders
@Internal public static void writeBeanDefinitionBuilders(ClassWriterOutputVisitor classWriterOutputVisitor, List<AbstractBeanDefinitionBuilder> beanDefinitionBuilders) throws IOException - Throws:
IOException
-
isIntercepted
protected boolean isIntercepted()Is the bean to be built intercepted?- Returns:
- True if it is
-
inject
Description copied from interface:BeanElementBuilder
Dependency inject this bean.- Specified by:
inject
in interfaceBeanElementBuilder
- Returns:
- this bean builder
-
getChildBeans
Any child bean definitions.- Returns:
- The child beans
-
getOriginatingElement
- Specified by:
getOriginatingElement
in interfaceBeanElementBuilder
- Returns:
- The originating element
-
getBeanType
- Specified by:
getBeanType
in interfaceBeanElementBuilder
- Returns:
- The bean type
-
initBeanParameters
protected final BeanParameterElement[] initBeanParameters(@NonNull @NonNull ParameterElement[] constructorParameters) Initialize the bean parameters.- Parameters:
constructorParameters
- The parameters to use.- Returns:
- The initialized parameters
-
getAnnotationMetadata
Description copied from interface:AnnotationMetadataProvider
Supplies the metadata. Defaults toAnnotationMetadata.EMPTY_METADATA
.- Specified by:
getAnnotationMetadata
in interfaceAnnotationMetadataProvider
- Returns:
- The
AnnotationMetadata
-
createWith
Description copied from interface:BeanElementBuilder
Specifies the bean will be created with the given method element. If not specified the bean will be created withClassElement.getPrimaryConstructor()
.Note that the method can be a one of the following:
- An accessible constructor on the bean type being generated
- An accessible static method on the bean type being generated
- Specified by:
createWith
in interfaceBeanElementBuilder
- Parameters:
element
- The element- Returns:
- This bean builder
-
typed
Description copied from interface:BeanElementBuilder
Alters the exposed types for the bean limiting the exposed type to the given types.- Specified by:
typed
in interfaceBeanElementBuilder
- Parameters:
types
- 1 or more types to expose- Returns:
- This builder
-
typeArguments
Description copied from interface:BeanElementBuilder
Fills the type arguments for the bean with the given types.- Specified by:
typeArguments
in interfaceBeanElementBuilder
- Specified by:
typeArguments
in interfaceConfigurableElement
- Parameters:
types
- The types- Returns:
- This bean builder
-
typeArgumentsForType
@NonNull public @NonNull BeanElementBuilder typeArgumentsForType(ClassElement type, @NonNull @NonNull ClassElement... types) Description copied from interface:BeanElementBuilder
Fills the type arguments for the given interface or super class with the given types.- Specified by:
typeArgumentsForType
in interfaceBeanElementBuilder
- Parameters:
type
- The type or interface. If null, results in a no-optypes
- The types- Returns:
- This bean builder
-
withConstructor
Description copied from interface:BeanElementBuilder
Allows configuring the bean constructor.- Specified by:
withConstructor
in interfaceBeanElementBuilder
- Parameters:
constructorElement
- The constructor element- Returns:
- This bean builder
-
withMethods
@NonNull public @NonNull BeanElementBuilder withMethods(@NonNull @NonNull ElementQuery<MethodElement> methods, @NonNull @NonNull Consumer<BeanMethodElement> beanMethods) Description copied from interface:BeanElementBuilder
Allows configuring methods of the bean.- Specified by:
withMethods
in interfaceBeanElementBuilder
- Parameters:
methods
- TheElementQuery
to locate selected methods.beanMethods
- A consumer that receives eachBeanMethodElement
- Returns:
- This builder
-
withFields
@NonNull public @NonNull BeanElementBuilder withFields(@NonNull @NonNull ElementQuery<FieldElement> fields, @NonNull @NonNull Consumer<BeanFieldElement> beanFields) Description copied from interface:BeanElementBuilder
Allows configuring fields of the bean.- Specified by:
withFields
in interfaceBeanElementBuilder
- Parameters:
fields
- TheElementQuery
to locate fields.beanFields
- The bean fields- Returns:
- This builder
-
withParameters
@NonNull public @NonNull BeanElementBuilder withParameters(Consumer<BeanParameterElement[]> parameters) Description copied from interface:BeanElementBuilder
Allows configuring the parameters for the current constructor.- Specified by:
withParameters
in interfaceBeanElementBuilder
- Parameters:
parameters
- The parameters- Returns:
- This builder
-
getParameters
- Returns:
- The bean creation parameters.
-
getName
-
isProtected
public boolean isProtected()- Specified by:
isProtected
in interfaceElement
- Returns:
- True if the element is protected.
-
isPublic
public boolean isPublic() -
getNativeType
Description copied from interface:Element
Returns the native underlying type. This API is extended by all inject language implementations. The object returned by this method will be the language native type the information is being retrieved from.- Specified by:
getNativeType
in interfaceElement
- Returns:
- The native type
-
annotate
@NonNull public <T extends Annotation> @NonNull BeanElementBuilder annotate(@NonNull @NonNull String annotationType, @NonNull @NonNull Consumer<AnnotationValueBuilder<T>> consumer) Description copied from interface:MutableAnnotationMetadataDelegate
Annotate this element with the given annotation type. If the annotation is already present then any values populated by the builder will be merged/overridden with the existing values.- Specified by:
annotate
in interfaceBeanElementBuilder
- Specified by:
annotate
in interfaceElement
- Specified by:
annotate
in interfaceMutableAnnotationMetadataDelegate<Element>
- Type Parameters:
T
- The annotation generic type- Parameters:
annotationType
- The annotation typeconsumer
- A function that receives theAnnotationValueBuilder
- Returns:
- This element
-
annotate
Description copied from interface:MutableAnnotationMetadataDelegate
Annotate this element with the given annotation type. If the annotation is already present then any values populated by the builder will be merged/overridden with the existing values.- Specified by:
annotate
in interfaceElement
- Specified by:
annotate
in interfaceMutableAnnotationMetadataDelegate<Element>
- Type Parameters:
T
- The annotation generic type- Parameters:
annotationValue
- The annotation type- Returns:
- This element
-
removeAnnotation
Description copied from interface:MutableAnnotationMetadataDelegate
Removes an annotation of the given type from the element.If the annotation features any stereotypes these will also be removed unless there are other annotations that reference the stereotype to be removed.
In the case of repeatable annotations this method will remove all repeated annotations, effectively clearing out all declared repeated annotations of the given type.
- Specified by:
removeAnnotation
in interfaceBeanElementBuilder
- Specified by:
removeAnnotation
in interfaceElement
- Specified by:
removeAnnotation
in interfaceMutableAnnotationMetadataDelegate<Element>
- Parameters:
annotationType
- The annotation type- Returns:
- This element
-
removeAnnotationIf
public <T extends Annotation> BeanElementBuilder removeAnnotationIf(@NonNull @NonNull Predicate<AnnotationValue<T>> predicate) Description copied from interface:MutableAnnotationMetadataDelegate
Removes all annotations that pass the given predicate.- Specified by:
removeAnnotationIf
in interfaceBeanElementBuilder
- Specified by:
removeAnnotationIf
in interfaceElement
- Specified by:
removeAnnotationIf
in interfaceMutableAnnotationMetadataDelegate<Element>
- Type Parameters:
T
- The annotation generic type- Parameters:
predicate
- The predicate- Returns:
- This element
-
removeStereotype
Description copied from interface:MutableAnnotationMetadataDelegate
Removes a stereotype of the given name from the element.- Specified by:
removeStereotype
in interfaceBeanElementBuilder
- Specified by:
removeStereotype
in interfaceElement
- Specified by:
removeStereotype
in interfaceMutableAnnotationMetadataDelegate<Element>
- Parameters:
annotationType
- The annotation type- Returns:
- This element
-
produceBeans
public <E extends MemberElement> BeanElementBuilder produceBeans(ElementQuery<E> methodsOrFields, Consumer<BeanElementBuilder> childBeanBuilder) Description copied from interface:BeanElementBuilder
Produce additional beans from the given methods.- Specified by:
produceBeans
in interfaceBeanElementBuilder
- Type Parameters:
E
- A type variable to- Parameters:
methodsOrFields
- TheElementQuery
representing the methods or fieldschildBeanBuilder
- Configure the child bean builder- Returns:
- This bean builder
-
createChildBean
@NonNull protected abstract @NonNull AbstractBeanDefinitionBuilder createChildBean(FieldElement producerField) Creates a child bean for the given producer field.- Parameters:
producerField
- The producer field- Returns:
- The child bean builder
-
visitInterceptedMethods
Visit the intercepted methods of this type.- Parameters:
consumer
- A consumer to handle the method
-
createChildBean
@NonNull protected abstract @NonNull AbstractBeanDefinitionBuilder createChildBean(MethodElement producerMethod) Creates a child bean for the given producer method.- Parameters:
producerMethod
- The producer method- Returns:
- The child bean builder
-
build
Build the bean definition writer.- Returns:
- The writer, possibly null if it wasn't possible to build it
-
createAroundMethodVisitor
@NonNull protected abstract @NonNull BiConsumer<TypedElement,MethodElement> createAroundMethodVisitor(BeanDefinitionVisitor aopProxyWriter) Creates the around method visitor.- Parameters:
aopProxyWriter
- The AOP writer- Returns:
- The visitor
-
createAopWriter
@NonNull protected abstract @NonNull BeanDefinitionVisitor createAopWriter(BeanDefinitionWriter beanDefinitionWriter, AnnotationMetadata annotationMetadata) Creates the AOP writer.- Parameters:
beanDefinitionWriter
- The bean definition writerannotationMetadata
- The annotation metadata- Returns:
- The AOP writer
-
finalizeAndWriteBean
protected void finalizeAndWriteBean(ClassWriterOutputVisitor classWriterOutputVisitor, BeanDefinitionVisitor beanDefinitionWriter) throws IOException Finish the given bean and write it to the output.- Parameters:
classWriterOutputVisitor
- The outputbeanDefinitionWriter
- The writer- Throws:
IOException
- If an error occurred
-
configureBeanVisitor
Configure the bean visitor for this builder.- Parameters:
beanDefinitionWriter
- The bean visitor- Returns:
- True if an error occurred
-
createBeanDefinitionWriter
- Returns:
- Creates the bean definition writer.
-
annotate
protected abstract <T extends Annotation> void annotate(AnnotationMetadata annotationMetadata, String annotationType, Consumer<AnnotationValueBuilder<T>> consumer) Add an annotation to the given metadata.- Type Parameters:
T
- The annotation generic type- Parameters:
annotationMetadata
- The annotation metadataannotationType
- the annotation typeconsumer
- The builder
-
annotate
protected abstract <T extends Annotation> void annotate(@NonNull @NonNull AnnotationMetadata annotationMetadata, @NonNull @NonNull AnnotationValue<T> annotationValue) Add an annotation to the given metadata.- Type Parameters:
T
- The annotation generic type- Parameters:
annotationMetadata
- The annotation metadataannotationValue
- The value- Since:
- 3.3.0
-
removeStereotype
protected abstract void removeStereotype(AnnotationMetadata annotationMetadata, String annotationType) Remove a stereotype from the given metadata.- Parameters:
annotationMetadata
- The metadataannotationType
- The stereotype
-
removeAnnotationIf
protected abstract <T extends Annotation> void removeAnnotationIf(AnnotationMetadata annotationMetadata, Predicate<AnnotationValue<T>> predicate) Remove an annotation if it matches the given condition.- Type Parameters:
T
- The annotation type- Parameters:
annotationMetadata
- The metadatapredicate
- The predicate
-
removeAnnotation
protected abstract void removeAnnotation(AnnotationMetadata annotationMetadata, String annotationType) Remove an annotation for the given name.- Parameters:
annotationMetadata
- The metadataannotationType
- The type
-