Class BeanDefinitionWriter
java.lang.Object
io.micronaut.inject.writer.BeanDefinitionWriter
- All Implemented Interfaces:
BeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>, AnnotatedElement, AnnotationMetadata, AnnotationMetadataDelegate, AnnotationMetadataProvider, AnnotationSource, Described, Named, Buildable<List<OutputObjectDef>>, Toggleable, MutableAnnotationMetadataDelegate<Element>, BeanElement, Element, ElementBeanDefinitionBuilder<OutputObjectDef>, OriginatingElements
@NullUnmarked
@Internal
public final class BeanDefinitionWriter
extends Object
implements BeanElement, Toggleable, ElementBeanDefinitionBuilder<OutputObjectDef>
Responsible for building BeanDefinition instances at compile time. Uses ASM build the class definition.
Should be used from AST frameworks to build bean definitions from source code data.
For example:
BeanDefinitionWriter writer = new BeanDefinitionWriter("my.package", "MyClass", "jakarta.inject.Singleton", true)
writer.visitBeanDefinitionConstructor()
writer.visitFieldInjectionPoint("my.Qualifier", false, "my.package.MyDependency", "myfield" )
writer.visitBeanDefinitionEnd()
writer.writeTo(new File(..))
- Since:
- 1.0
- Author:
- Graeme Rocher, Denis Stepanov
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe custom initializer builder. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final StringThe suffix use for generated AOP intercepted types.Fields inherited from interface AnnotationMetadata
CLASS_NAME_SUFFIX, EMPTY_METADATA, VALUE_MEMBERFields inherited from interface AnnotationSource
EMPTYFields inherited from interface Element
EMPTY_ELEMENT_ARRAYFields inherited from interface MutableAnnotationMetadataDelegate
EMPTY -
Constructor Summary
ConstructorsConstructorDescriptionBeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, VisitorContext visitorContext) BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, OriginatingElements originatingElements, VisitorContext visitorContext) BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, @Nullable String beanDefinitionName, @Nullable AnnotationMetadata annotationMetadata, VisitorContext visitorContext) BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, @Nullable String beanDefinitionName, @Nullable AnnotationMetadata annotationMetadata, OriginatingElements originatingElements, VisitorContext visitorContext) BeanDefinitionWriter(FieldDefinition<ClassElement, FieldElement> factoryFieldDefinition, VisitorContext visitorContext) BeanDefinitionWriter(MethodDefinition<ClassElement, MethodElement> factoryMethodDefinition, VisitorContext visitorContext, int uniqueIdentifier) -
Method Summary
Modifier and TypeMethodDescriptionaddAssociatedBean(ClassElement type, VisitorContext visitorContext) This method adds an associated bean using this class element as the originating element.addExecutableMethod(MethodElement methodElement, boolean requiresReflection) Adds an executable method to the bean definition.addFieldConfigurationBuilder(FieldElement fieldElement, AnnotationMetadata annotationMetadata, List<MethodDefinition<ClassElement, MethodElement>> builderMethods) Adds a field-based configuration builder.addFieldInjection(FieldDefinition<ClassElement, FieldElement> fieldDefinition) Adds a field injection point to the bean definition.addMethodConfigurationBuilder(MethodElement methodElement, AnnotationMetadata annotationMetadata, List<MethodDefinition<ClassElement, MethodElement>> builderMethods) Adds a method-based configuration builder.addMethodInjection(MethodDefinition<ClassElement, MethodElement> methodDefinition) Adds a method injection point to the bean definition.voidaddOriginatingElement(Element element) Add another element that should be included in the originating elements.addPostConstruct(MethodDefinition<ClassElement, MethodElement> methodDefinition) Registers a@PostConstructmethod.addPreDestroy(MethodDefinition<ClassElement, MethodElement> methodDefinition) Registers a@PreDestroymethod.voidaddTypeArguments(Map<String, Map<String, ClassElement>> typeArguments) <T extends Annotation>
Elementannotate(String annotationType, Consumer<AnnotationValueBuilder<T>> consumer) Annotate this element with the given annotation type.build()Builds the result.void@Nullable ExecutableMethodsDefinitionWriterReturnsExecutableMethodsDefinitionWriterof one exists.static voidfinish()Finish any work writing beans.voidgenerateProxyReference(String proxyBeanDefinitionName, String proxyBeanTypeName) Mark to generate proxy methods.Supplies the metadata.The type names produced by the bean.Returns the declaringClassElementwhich may differ from theBeanElement.getBeanTypes()in the case of factory beans.ReturnsExecutableMethodsDefinitionWriterof one exists.Returns all injection points for the bean.getName()Returns the native underlying type.Element[]The element that produces the bean, this could be aClassElementfor regular beans, or either aMethodElementorFieldElementfor factory beans.getScope()The scope of the bean.booleanbooleanbooleanbooleanbooleanisPublic()booleanremoveAnnotation(String annotationType) Removes an annotation of the given type from the element.<T extends Annotation>
ElementremoveAnnotationIf(Predicate<AnnotationValue<T>> predicate) Removes all annotations that pass the given predicate.removeStereotype(String annotationType) Removes a stereotype of the given name from the element.voidsetExecutableMethodsWriter(ExecutableMethodsDefinitionWriter executableMethodsDefinitionWriter) voidsetExposes(Set<ClassElement> exposes) voidsetInterceptedType(String typeName) voidsetProxiedBean(boolean proxiedBean, boolean isProxyTarget) Sets whether this bean is a proxied type.voidsetValidated(boolean validated) toString()voidvisitBeanDefinitionInterface(Class<? extends BeanDefinition> interfaceType) voidvoidvoidvisitSuperBeanDefinitionFactory(String beanName) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface AnnotatedElement
isDeclaredNonNull, isDeclaredNullable, isNonNull, isNullableMethods inherited from interface AnnotationMetadata
enumValuesSet, enumValuesSet, getValues, hasDeclaredStereotype, hasEvaluatedExpressions, hasStereotypeNonRepeating, isAnnotationPresent, isDeclaredAnnotationPresentMethods inherited from interface 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, synthesizeDeclaredAnnotationsByTypeMethods inherited from interface Element
annotate, annotate, annotate, annotate, getDescription, getDescription, getDocumentation, getDocumentation, getModifiers, getSimpleName, isFinal, isPackagePrivate, isPrivate, isStatic, isSynthetic, removeAnnotation, removeStereotype, withAnnotationMetadataMethods inherited from interface ElementBeanDefinitionBuilder
addFieldInjection, addFieldPropertyInjection, addFieldPropertyInjection, addMethodInjection, addPostConstruct, addPreDestroy
-
Field Details
-
PROXY_SUFFIX
The suffix use for generated AOP intercepted types.- See Also:
-
OMIT_CONFPROP_INJECTION_POINTS
- See Also:
-
CLASS_SUFFIX
- See Also:
-
-
Constructor Details
-
BeanDefinitionWriter
public BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, @Nullable String beanDefinitionName, @Nullable AnnotationMetadata annotationMetadata, VisitorContext visitorContext) -
BeanDefinitionWriter
public BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, VisitorContext visitorContext) -
BeanDefinitionWriter
public BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, OriginatingElements originatingElements, VisitorContext visitorContext) -
BeanDefinitionWriter
public BeanDefinitionWriter(ConstructorDefinition<ClassElement, MethodElement> constructorDefinition, @Nullable String beanDefinitionName, @Nullable AnnotationMetadata annotationMetadata, OriginatingElements originatingElements, VisitorContext visitorContext) -
BeanDefinitionWriter
public BeanDefinitionWriter(FieldDefinition<ClassElement, FieldElement> factoryFieldDefinition, VisitorContext visitorContext) -
BeanDefinitionWriter
public BeanDefinitionWriter(MethodDefinition<ClassElement, MethodElement> factoryMethodDefinition, VisitorContext visitorContext, int uniqueIdentifier)
-
-
Method Details
-
getElementProducerDefinition
-
getBeanTypeElement
-
addTypeArguments
-
addPostConstruct
public BeanDefinitionWriter addPostConstruct(MethodDefinition<ClassElement, MethodElement> methodDefinition) Description copied from interface:BeanDefinitionBuilderRegisters a@PostConstructmethod.- Specified by:
addPostConstructin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
methodDefinition- The lifecycle method definition
-
addPreDestroy
public BeanDefinitionWriter addPreDestroy(MethodDefinition<ClassElement, MethodElement> methodDefinition) Description copied from interface:BeanDefinitionBuilderRegisters a@PreDestroymethod.- Specified by:
addPreDestroyin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
methodDefinition- The lifecycle method definition
-
addMethodInjection
public BeanDefinitionWriter addMethodInjection(MethodDefinition<ClassElement, MethodElement> methodDefinition) Description copied from interface:BeanDefinitionBuilderAdds a method injection point to the bean definition.- Specified by:
addMethodInjectionin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
methodDefinition- The method definition
-
addFieldConfigurationBuilder
public BeanDefinitionWriter addFieldConfigurationBuilder(FieldElement fieldElement, AnnotationMetadata annotationMetadata, List<MethodDefinition<ClassElement, MethodElement>> builderMethods) Description copied from interface:BeanDefinitionBuilderAdds a field-based configuration builder.- Specified by:
addFieldConfigurationBuilderin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
fieldElement- The configuration fieldannotationMetadata- Associated annotation metadatabuilderMethods- The builder methods
-
addMethodConfigurationBuilder
public BeanDefinitionWriter addMethodConfigurationBuilder(MethodElement methodElement, AnnotationMetadata annotationMetadata, List<MethodDefinition<ClassElement, MethodElement>> builderMethods) Description copied from interface:BeanDefinitionBuilderAdds a method-based configuration builder.- Specified by:
addMethodConfigurationBuilderin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
methodElement- The configuration methodannotationMetadata- Associated annotation metadatabuilderMethods- The builder methods
-
addFieldInjection
public BeanDefinitionWriter addFieldInjection(FieldDefinition<ClassElement, FieldElement> fieldDefinition) Description copied from interface:BeanDefinitionBuilderAdds a field injection point to the bean definition.- Specified by:
addFieldInjectionin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
fieldDefinition- The field definition
-
generateProxyReference
-
isEnabled
public boolean isEnabled()- Specified by:
isEnabledin interfaceToggleable- Returns:
- Whether the component is enabled
-
getExecutableMethodsWriter
ReturnsExecutableMethodsDefinitionWriterof one exists.- Returns:
- An instance of
ExecutableMethodsDefinitionWriter
-
setExecutableMethodsWriter
public void setExecutableMethodsWriter(ExecutableMethodsDefinitionWriter executableMethodsDefinitionWriter) -
dontGenerateExecutableMethodsDefinitionWriter
public void dontGenerateExecutableMethodsDefinitionWriter() -
findExecutableMethodsWriter
ReturnsExecutableMethodsDefinitionWriterof one exists.- Returns:
- An instance of
ExecutableMethodsDefinitionWriteror null
-
getTypeArguments
- Returns:
- The type arguments
-
getPostConstructMethods
- Returns:
- The post construct method definitions scheduled for invocation
-
isInterface
public boolean isInterface()- Returns:
- Is interface
-
visitBeanDefinitionInterface
- Parameters:
interfaceType- An interface to add
-
visitSuperBeanDefinition
- Parameters:
name- Visit the super bean definition
-
visitSuperBeanDefinitionFactory
- Parameters:
beanName- Visit a super bean factory
-
getBeanTypeName
- Returns:
- Teh bean type name
-
setValidated
public void setValidated(boolean validated) - Parameters:
validated- If the bean is validated
-
setInterceptedType
- Parameters:
typeName- The intercepted type
-
setExposes
- Parameters:
exposes- Teh exposed types
-
isValidated
public boolean isValidated()- Returns:
- Is validated
-
getBeanDefinitionName
- Returns:
- The bean definition name
-
getOriginatingElement
- Specified by:
getOriginatingElementin interfaceBeanElement- Returns:
- The originating element.
-
build
Description copied from interface:BuildableBuilds the result.- Specified by:
buildin interfaceBuildable<List<OutputObjectDef>>- Returns:
- The builder result
-
addExecutableMethod
public BeanDefinitionWriter addExecutableMethod(MethodElement methodElement, boolean requiresReflection) Description copied from interface:BeanDefinitionBuilderAdds an executable method to the bean definition.- Specified by:
addExecutableMethodin interfaceBeanDefinitionBuilder<ClassElement, MethodElement, FieldElement, List<OutputObjectDef>>- Parameters:
methodElement- The method elementrequiresReflection- Whether reflective invocation is required
-
toString
-
getAnnotationMetadata
Description copied from interface:AnnotationMetadataProviderSupplies the metadata. Defaults toAnnotationMetadata.EMPTY_METADATA.- Specified by:
getAnnotationMetadatain interfaceAnnotationMetadataProvider- Returns:
- The
AnnotationMetadata
-
visitBuildCustomMethodDefinition
-
getName
-
isProtected
public boolean isProtected()- Specified by:
isProtectedin interfaceElement- Returns:
- True if the element is protected.
-
isPublic
-
getNativeType
Description copied from interface:ElementReturns 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:
getNativeTypein interfaceElement- Returns:
- The native type
-
getInjectionPoints
Description copied from interface:BeanElementReturns all injection points for the bean. These will be a combination ofFieldElementandMethodElementinstances.- Specified by:
getInjectionPointsin interfaceBeanElement- Returns:
- The injection points for the bean.
-
isAbstract
public boolean isAbstract()- Specified by:
isAbstractin interfaceElement- Returns:
- True if the element is abstract.
-
annotate
public <T extends Annotation> Element annotate(String annotationType, Consumer<AnnotationValueBuilder<T>> consumer) Description copied from interface:MutableAnnotationMetadataDelegateAnnotate 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:
annotatein interfaceElement- Specified by:
annotatein interfaceMutableAnnotationMetadataDelegate<Element>- Type Parameters:
T- The annotation generic type- Parameters:
annotationType- The annotation typeconsumer- A function that receives theAnnotationValueBuilder- Returns:
- This element
-
removeAnnotation
Description copied from interface:MutableAnnotationMetadataDelegateRemoves 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:
removeAnnotationin interfaceElement- Specified by:
removeAnnotationin interfaceMutableAnnotationMetadataDelegate<Element>- Parameters:
annotationType- The annotation type- Returns:
- This element
-
removeAnnotationIf
Description copied from interface:MutableAnnotationMetadataDelegateRemoves all annotations that pass the given predicate.- Specified by:
removeAnnotationIfin interfaceElement- Specified by:
removeAnnotationIfin interfaceMutableAnnotationMetadataDelegate<Element>- Type Parameters:
T- The annotation generic type- Parameters:
predicate- The predicate- Returns:
- This element
-
removeStereotype
Description copied from interface:MutableAnnotationMetadataDelegateRemoves a stereotype of the given name from the element.- Specified by:
removeStereotypein interfaceElement- Specified by:
removeStereotypein interfaceMutableAnnotationMetadataDelegate<Element>- Parameters:
annotationType- The annotation type- Returns:
- This element
-
getDeclaringClass
Description copied from interface:BeanElementReturns the declaringClassElementwhich may differ from theBeanElement.getBeanTypes()in the case of factory beans.- Specified by:
getDeclaringClassin interfaceBeanElement- Returns:
- The declaring class of the bean.
-
getProducingElement
Description copied from interface:BeanElementThe element that produces the bean, this could be aClassElementfor regular beans, or either aMethodElementorFieldElementfor factory beans.- Specified by:
getProducingElementin interfaceBeanElement- Returns:
- The producing element
-
getBeanTypes
Description copied from interface:BeanElementThe type names produced by the bean.- Specified by:
getBeanTypesin interfaceBeanElement- Returns:
- A set of types
-
getScope
Description copied from interface:BeanElementThe scope of the bean.- Specified by:
getScopein interfaceBeanElement- Returns:
- The fully qualified name of the scope or empty if no scope is defined.
-
getQualifiers
- Specified by:
getQualifiersin interfaceBeanElement- Returns:
- One or more fully qualified qualifier types defined by the bean.
-
addAssociatedBean
Description copied from interface:BeanElementThis method adds an associated bean using this class element as the originating element.Note that this method can only be called on classes being directly compiled by Micronaut. If the ClassElement is loaded from pre-compiled code an
UnsupportedOperationExceptionwill be thrown.- Specified by:
addAssociatedBeanin interfaceBeanElement- Parameters:
type- The type of the beanvisitorContext- The visitor context- Returns:
- A bean builder
-
getOriginatingElements
- Specified by:
getOriginatingElementsin interfaceOriginatingElements- Returns:
- The elements where the bean definition originated from as an array.
-
addOriginatingElement
Description copied from interface:OriginatingElementsAdd another element that should be included in the originating elements.- Specified by:
addOriginatingElementin interfaceOriginatingElements- Parameters:
element- The element to add
-
setProxiedBean
public void setProxiedBean(boolean proxiedBean, boolean isProxyTarget) Sets whether this bean is a proxied type.- Parameters:
proxiedBean- True if it proxiedisProxyTarget- True if the proxied bean is a retained target
-
finish
Finish any work writing beans.
-