Package io.micronaut.inject.writer
Class AbstractBeanDefinitionBuilder
- java.lang.Object
-
- io.micronaut.inject.writer.AbstractBeanDefinitionBuilder
-
- All Implemented Interfaces:
io.micronaut.core.annotation.AnnotatedElement
,io.micronaut.core.annotation.AnnotationMetadata
,io.micronaut.core.annotation.AnnotationMetadataDelegate
,io.micronaut.core.annotation.AnnotationMetadataProvider
,io.micronaut.core.annotation.AnnotationSource
,io.micronaut.core.naming.Described
,io.micronaut.core.naming.Named
,BeanElementBuilder
,ConfigurableElement
,Element
@Internal public abstract class AbstractBeanDefinitionBuilder extends java.lang.Object implements BeanElementBuilder
Abstract implementation of theBeanElementBuilder
interface that should be implemented by downstream language specific implementations.- Since:
- 3.0.0
-
-
Field Summary
Fields Modifier and Type Field Description protected ConfigurationMetadataBuilder<?>
metadataBuilder
protected VisitorContext
visitorContext
-
Fields inherited from interface io.micronaut.core.annotation.AnnotationMetadata
CLASS_NAME_SUFFIX, EMPTY_METADATA, VALUE_MEMBER
-
Fields inherited from interface io.micronaut.inject.ast.Element
EMPTY_ELEMENT_ARRAY
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractBeanDefinitionBuilder(Element originatingElement, ClassElement beanType, ConfigurationMetadataBuilder<?> metadataBuilder, VisitorContext visitorContext)
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract <T extends java.lang.annotation.Annotation>
voidannotate(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, io.micronaut.core.annotation.AnnotationValue<T> annotationValue)
Add an annotation to the given metadata.protected abstract <T extends java.lang.annotation.Annotation>
voidannotate(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.lang.String annotationType, java.util.function.Consumer<io.micronaut.core.annotation.AnnotationValueBuilder<T>> consumer)
Add an annotation to the given metadata.<T extends java.lang.annotation.Annotation>
Elementannotate(io.micronaut.core.annotation.AnnotationValue<T> annotationValue)
Annotate this element with the given annotation type.<T extends java.lang.annotation.Annotation>
BeanElementBuilderannotate(java.lang.String annotationType, java.util.function.Consumer<io.micronaut.core.annotation.AnnotationValueBuilder<T>> consumer)
Annotate this element with the given annotation type.BeanClassWriter
build()
Build the bean definition writer.protected boolean
configureBeanVisitor(BeanDefinitionVisitor beanDefinitionWriter)
Configure the bean visitor for this builder.protected abstract BeanDefinitionVisitor
createAopWriter(BeanDefinitionWriter beanDefinitionWriter, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
Creates the AOP writer.protected abstract java.util.function.BiConsumer<TypedElement,MethodElement>
createAroundMethodVisitor(BeanDefinitionVisitor aopProxyWriter)
Creates the around method visitor.protected BeanDefinitionVisitor
createBeanDefinitionWriter()
protected abstract AbstractBeanDefinitionBuilder
createChildBean(FieldElement producerField)
Creates a child bean for the given producer field.protected abstract AbstractBeanDefinitionBuilder
createChildBean(MethodElement producerMethod)
Creates a child bean for the given producer method.BeanElementBuilder
createWith(MethodElement element)
Specifies the bean will created with the given method element.protected void
finalizeAndWriteBean(ClassWriterOutputVisitor classWriterOutputVisitor, BeanDefinitionVisitor beanDefinitionWriter)
Finish the given bean and write it to the output.io.micronaut.core.annotation.AnnotationMetadata
getAnnotationMetadata()
ClassElement
getBeanType()
java.util.List<AbstractBeanDefinitionBuilder>
getChildBeans()
Any child bean definitions.java.lang.String
getName()
java.lang.Object
getNativeType()
Returns the native underlying type.Element
getOriginatingElement()
protected BeanParameterElement[]
getParameters()
protected BeanParameterElement[]
initBeanParameters(ParameterElement[] constructorParameters)
Initialize the bean parameters.BeanElementBuilder
inject()
Dependency inject this bean.BeanElementBuilder
intercept(io.micronaut.core.annotation.AnnotationValue<?>... annotationValue)
Intercept the bean.protected boolean
isIntercepted()
Is the bean to be built intercepted?boolean
isProtected()
boolean
isPublic()
<E extends MemberElement>
BeanElementBuilderproduceBeans(ElementQuery<E> methodsOrFields, java.util.function.Consumer<BeanElementBuilder> childBeanBuilder)
Produce additional beans from the given methods.protected abstract void
removeAnnotation(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.lang.String annotationType)
Remove an annotation for the given name.BeanElementBuilder
removeAnnotation(java.lang.String annotationType)
Removes an annotation of the given type from the element.protected abstract <T extends java.lang.annotation.Annotation>
voidremoveAnnotationIf(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.util.function.Predicate<io.micronaut.core.annotation.AnnotationValue<T>> predicate)
Remove an annotation if it matches the given condition.<T extends java.lang.annotation.Annotation>
BeanElementBuilderremoveAnnotationIf(java.util.function.Predicate<io.micronaut.core.annotation.AnnotationValue<T>> predicate)
Removes all annotations that pass the given predicate.protected abstract void
removeStereotype(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.lang.String annotationType)
Remove a stereotype from the given metadata.BeanElementBuilder
removeStereotype(java.lang.String annotationType)
Removes a stereotype of the given name from the element.BeanElementBuilder
typeArguments(ClassElement... types)
Fills the type arguments for the bean with the given types.BeanElementBuilder
typeArgumentsForType(ClassElement type, ClassElement... types)
Fills the type arguments for the given interface or super class with the given types.BeanElementBuilder
typed(ClassElement... types)
Alters the exposed types for the bean limiting the exposed type to the given types.protected void
visitInterceptedMethods(java.util.function.BiConsumer<TypedElement,MethodElement> consumer)
Visit the intercepted methods of this type.BeanElementBuilder
withConstructor(java.util.function.Consumer<BeanConstructorElement> constructorElement)
Allows configuring the bean constructor.BeanElementBuilder
withFields(ElementQuery<FieldElement> fields, java.util.function.Consumer<BeanFieldElement> beanFields)
Allows configuring fields of the bean.BeanElementBuilder
withMethods(ElementQuery<MethodElement> methods, java.util.function.Consumer<BeanMethodElement> beanMethods)
Allows configuring methods of the bean.BeanElementBuilder
withParameters(java.util.function.Consumer<BeanParameterElement[]> parameters)
Allows configuring the parameters for the current constructor.static void
writeBeanDefinitionBuilders(ClassWriterOutputVisitor classWriterOutputVisitor, java.util.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
getAnnotationValuesByName, getAnnotationValuesByStereotype, getDeclaredAnnotationValuesByName, getValues, hasDeclaredStereotype, isAnnotationPresent, isDeclaredAnnotationPresent
-
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataDelegate
booleanValue, booleanValue, booleanValue, booleanValue, classValue, classValue, classValue, classValue, classValues, classValues, classValues, classValues, 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, getAnnotationValuesByType, getDeclaredAnnotation, getDeclaredAnnotation, getDeclaredAnnotationNameByStereotype, getDeclaredAnnotationNames, getDeclaredAnnotationNamesByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationTypeByStereotype, getDeclaredAnnotationValuesByType, getDeclaredMetadata, getDeclaredStereotypeAnnotationNames, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValue, getDefaultValues, getStereotypeAnnotationNames, 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
-
-
-
-
Field Detail
-
metadataBuilder
protected final ConfigurationMetadataBuilder<?> metadataBuilder
-
visitorContext
protected final VisitorContext visitorContext
-
-
Constructor Detail
-
AbstractBeanDefinitionBuilder
protected AbstractBeanDefinitionBuilder(Element originatingElement, ClassElement beanType, ConfigurationMetadataBuilder<?> metadataBuilder, VisitorContext visitorContext)
Default constructor.- Parameters:
originatingElement
- The originating elementbeanType
- The bean typemetadataBuilder
- the metadata buildervisitorContext
- the visitor context
-
-
Method Detail
-
intercept
public BeanElementBuilder intercept(io.micronaut.core.annotation.AnnotationValue<?>... annotationValue)
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, java.util.List<AbstractBeanDefinitionBuilder> beanDefinitionBuilders) throws java.io.IOException
- Throws:
java.io.IOException
-
isIntercepted
protected boolean isIntercepted()
Is the bean to be built intercepted?- Returns:
- True if it is
-
inject
public BeanElementBuilder inject()
Description copied from interface:BeanElementBuilder
Dependency inject this bean.- Specified by:
inject
in interfaceBeanElementBuilder
- Returns:
- this bean builder
-
getChildBeans
public java.util.List<AbstractBeanDefinitionBuilder> getChildBeans()
Any child bean definitions.- Returns:
- The child beans
-
getOriginatingElement
@NonNull public Element getOriginatingElement()
- Specified by:
getOriginatingElement
in interfaceBeanElementBuilder
- Returns:
- The originating element
-
getBeanType
@NonNull public ClassElement getBeanType()
- Specified by:
getBeanType
in interfaceBeanElementBuilder
- Returns:
- The bean type
-
initBeanParameters
protected final BeanParameterElement[] initBeanParameters(@NonNull ParameterElement[] constructorParameters)
Initialize the bean parameters.- Parameters:
constructorParameters
- The parameters to use.- Returns:
- The initialized parameters
-
getAnnotationMetadata
@NonNull public io.micronaut.core.annotation.AnnotationMetadata getAnnotationMetadata()
- Specified by:
getAnnotationMetadata
in interfaceio.micronaut.core.annotation.AnnotationMetadataProvider
-
createWith
@NonNull public BeanElementBuilder createWith(@NonNull MethodElement element)
Description copied from interface:BeanElementBuilder
Specifies the bean will 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
@NonNull public BeanElementBuilder typed(ClassElement... types)
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
@NonNull public BeanElementBuilder typeArguments(@NonNull ClassElement... types)
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 BeanElementBuilder typeArgumentsForType(ClassElement type, @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
public BeanElementBuilder withConstructor(java.util.function.Consumer<BeanConstructorElement> constructorElement)
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 BeanElementBuilder withMethods(@NonNull ElementQuery<MethodElement> methods, @NonNull java.util.function.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 BeanElementBuilder withFields(@NonNull ElementQuery<FieldElement> fields, @NonNull java.util.function.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 BeanElementBuilder withParameters(java.util.function.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
@NonNull protected BeanParameterElement[] getParameters()
- Returns:
- The bean creation parameters.
-
getName
@NonNull public java.lang.String getName()
-
isProtected
public boolean isProtected()
- Specified by:
isProtected
in interfaceElement
- Returns:
- True if the element is protected.
-
isPublic
public boolean isPublic()
-
getNativeType
@NonNull public java.lang.Object getNativeType()
Description copied from interface:Element
Returns the native underlying type. This API is extended by all of the 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 java.lang.annotation.Annotation> BeanElementBuilder annotate(@NonNull java.lang.String annotationType, @NonNull java.util.function.Consumer<io.micronaut.core.annotation.AnnotationValueBuilder<T>> consumer)
Description copied from interface:Element
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
- Type Parameters:
T
- The annotation generic type- Parameters:
annotationType
- The annotation typeconsumer
- A function that receives theAnnotationValueBuilder
- Returns:
- This element
-
annotate
public <T extends java.lang.annotation.Annotation> Element annotate(io.micronaut.core.annotation.AnnotationValue<T> annotationValue)
Description copied from interface:Element
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.
-
removeAnnotation
public BeanElementBuilder removeAnnotation(@NonNull java.lang.String annotationType)
Description copied from interface:Element
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
- Parameters:
annotationType
- The annotation type- Returns:
- This element
-
removeAnnotationIf
public <T extends java.lang.annotation.Annotation> BeanElementBuilder removeAnnotationIf(@NonNull java.util.function.Predicate<io.micronaut.core.annotation.AnnotationValue<T>> predicate)
Description copied from interface:Element
Removes all annotations that pass the given predicate.- Specified by:
removeAnnotationIf
in interfaceBeanElementBuilder
- Specified by:
removeAnnotationIf
in interfaceElement
- Type Parameters:
T
- The annotation generic type- Parameters:
predicate
- The predicate- Returns:
- This element
-
removeStereotype
public BeanElementBuilder removeStereotype(@NonNull java.lang.String annotationType)
Description copied from interface:Element
Removes a stereotype of the given name from the element.- Specified by:
removeStereotype
in interfaceBeanElementBuilder
- Specified by:
removeStereotype
in interfaceElement
- Parameters:
annotationType
- The annotation type- Returns:
- This element
-
produceBeans
public <E extends MemberElement> BeanElementBuilder produceBeans(ElementQuery<E> methodsOrFields, java.util.function.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 AbstractBeanDefinitionBuilder createChildBean(FieldElement producerField)
Creates a child bean for the given producer field.- Parameters:
producerField
- The producer field- Returns:
- The child bean builder
-
visitInterceptedMethods
protected void visitInterceptedMethods(java.util.function.BiConsumer<TypedElement,MethodElement> consumer)
Visit the intercepted methods of this type.- Parameters:
consumer
- A consumer to handle the method
-
createChildBean
@NonNull protected abstract AbstractBeanDefinitionBuilder createChildBean(MethodElement producerMethod)
Creates a child bean for the given producer method.- Parameters:
producerMethod
- The producer method- Returns:
- The child bean builder
-
build
@Nullable public BeanClassWriter build()
Build the bean definition writer.- Returns:
- The writer, possibly null if it wasn't possible to build it
-
createAroundMethodVisitor
@NonNull protected abstract java.util.function.BiConsumer<TypedElement,MethodElement> createAroundMethodVisitor(BeanDefinitionVisitor aopProxyWriter)
Creates the around method visitor.- Parameters:
aopProxyWriter
- The AOP writer- Returns:
- The visitor
-
createAopWriter
@NonNull protected abstract BeanDefinitionVisitor createAopWriter(BeanDefinitionWriter beanDefinitionWriter, io.micronaut.core.annotation.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 java.io.IOException
Finish the given bean and write it to the output.- Parameters:
classWriterOutputVisitor
- The outputbeanDefinitionWriter
- The writer- Throws:
java.io.IOException
- If an error occurred
-
configureBeanVisitor
protected boolean configureBeanVisitor(BeanDefinitionVisitor beanDefinitionWriter)
Configure the bean visitor for this builder.- Parameters:
beanDefinitionWriter
- The bean visitor- Returns:
- True if an error occurred
-
createBeanDefinitionWriter
protected BeanDefinitionVisitor createBeanDefinitionWriter()
- Returns:
- Creates the bean definition writer.
-
annotate
protected abstract <T extends java.lang.annotation.Annotation> void annotate(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.lang.String annotationType, java.util.function.Consumer<io.micronaut.core.annotation.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 java.lang.annotation.Annotation> void annotate(@NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull io.micronaut.core.annotation.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(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.lang.String annotationType)
Remove a stereotype from the given metadata.- Parameters:
annotationMetadata
- The metadataannotationType
- The stereotype
-
removeAnnotationIf
protected abstract <T extends java.lang.annotation.Annotation> void removeAnnotationIf(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.util.function.Predicate<io.micronaut.core.annotation.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(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, java.lang.String annotationType)
Remove an annotation for the given name.- Parameters:
annotationMetadata
- The metadataannotationType
- The type
-
-