Class BeanDefinitionWriter

  • 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, io.micronaut.core.util.Toggleable, BeanElement, Element, BeanDefinitionVisitor, ClassOutputWriter, OriginatingElements, org.objectweb.asm.Opcodes

    @Internal
    public class BeanDefinitionWriter
    extends AbstractClassFileWriter
    implements BeanDefinitionVisitor, BeanElement, io.micronaut.core.util.Toggleable

    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", "javax.inject.Singleton", true)
              writer.visitBeanDefinitionConstructor()
              writer.visitFieldInjectionPoint("my.Qualifier", false, "my.package.MyDependency", "myfield" )
              writer.visitBeanDefinitionEnd()
              writer.writeTo(new File(..))
         
     
    Since:
    1.0
    See Also:
    BeanDefinition
    • Field Detail

      • OMIT_CONFPROP_INJECTION_POINTS

        public static final java.lang.String OMIT_CONFPROP_INJECTION_POINTS
        See Also:
        Constant Field Values
    • Constructor Detail

      • BeanDefinitionWriter

        public BeanDefinitionWriter​(ClassElement classElement,
                                    ConfigurationMetadataBuilder<?> metadataBuilder,
                                    VisitorContext visitorContext)
        Creates a bean definition writer.
        Parameters:
        classElement - The class element
        metadataBuilder - The configuration metadata builder
        visitorContext - The visitor context
      • BeanDefinitionWriter

        public BeanDefinitionWriter​(ClassElement classElement,
                                    OriginatingElements originatingElements,
                                    ConfigurationMetadataBuilder<?> metadataBuilder,
                                    VisitorContext visitorContext)
        Creates a bean definition writer.
        Parameters:
        classElement - The class element
        originatingElements - The originating elements
        metadataBuilder - The configuration metadata builder
        visitorContext - The visitor context
      • BeanDefinitionWriter

        public BeanDefinitionWriter​(Element beanProducingElement,
                                    OriginatingElements originatingElements,
                                    ConfigurationMetadataBuilder<?> metadataBuilder,
                                    VisitorContext visitorContext,
                                    @Nullable
                                    java.lang.Integer uniqueIdentifier)
        Creates a bean definition writer.
        Parameters:
        beanProducingElement - The bean producing element
        originatingElements - The originating elements
        metadataBuilder - The configuration metadata builder
        visitorContext - The visitor context
        uniqueIdentifier - An optional unique identifier to include in the bean name
    • Method Detail

      • isEnabled

        public boolean isEnabled()
        Specified by:
        isEnabled in interface io.micronaut.core.util.Toggleable
      • getPostConstructMethodVisits

        public final java.util.List<BeanDefinitionWriter.MethodVisitData> getPostConstructMethodVisits()
        Returns:
        The data for any post construct methods that were visited
      • getClassWriter

        public org.objectweb.asm.ClassVisitor getClassWriter()
        Returns:
        The underlying class writer
      • setValidated

        public void setValidated​(boolean validated)
        Description copied from interface: BeanDefinitionVisitor
        Make the bean definition as validated by javax.validation.
        Specified by:
        setValidated in interface BeanDefinitionVisitor
        Parameters:
        validated - Whether the bean definition is validated
      • isValidated

        public boolean isValidated()
        Specified by:
        isValidated in interface BeanDefinitionVisitor
        Returns:
        Return whether the bean definition is validated.
      • visitBeanFactoryMethod

        public void visitBeanFactoryMethod​(ClassElement factoryClass,
                                           MethodElement factoryMethod)

        In the case where the produced class is produced by a factory method annotated with Bean this method should be called.

        Specified by:
        visitBeanFactoryMethod in interface BeanDefinitionVisitor
        Parameters:
        factoryClass - The factory class
        factoryMethod - The factory method
      • visitBeanFactoryMethod

        public void visitBeanFactoryMethod​(ClassElement factoryClass,
                                           MethodElement factoryMethod,
                                           ParameterElement[] parameters)

        In the case where the produced class is produced by a factory method annotated with Bean this method should be called.

        Specified by:
        visitBeanFactoryMethod in interface BeanDefinitionVisitor
        Parameters:
        factoryClass - The factory class
        factoryMethod - The factory method
        parameters - The parameters
      • visitBeanFactoryField

        public void visitBeanFactoryField​(ClassElement factoryClass,
                                          FieldElement factoryField)

        In the case where the produced class is produced by a factory field annotated with Bean this method should be called.

        Specified by:
        visitBeanFactoryField in interface BeanDefinitionVisitor
        Parameters:
        factoryClass - The factory class
        factoryField - The factory field
      • visitBeanDefinitionConstructor

        public void visitBeanDefinitionConstructor​(MethodElement constructor,
                                                   boolean requiresReflection,
                                                   VisitorContext visitorContext)
        Visits the constructor used to create the bean definition.
        Specified by:
        visitBeanDefinitionConstructor in interface BeanDefinitionVisitor
        Parameters:
        constructor - The constructor
        requiresReflection - Whether invoking the constructor requires reflection
        visitorContext - The visitor context
      • visitDefaultConstructor

        public void visitDefaultConstructor​(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata,
                                            VisitorContext visitorContext)
        Description copied from interface: BeanDefinitionVisitor
        Visits the constructor used to create the bean definition in the case where no constructor is present. This method should only be called in the class defines no constructor.
        Specified by:
        visitDefaultConstructor in interface BeanDefinitionVisitor
        Parameters:
        annotationMetadata - The annotation metadata for the constructor
        visitorContext - The visitor context
      • toByteArray

        public byte[] toByteArray()
        Returns:
        The bytes of the class
      • visitSetterValue

        public void visitSetterValue​(TypedElement declaringType,
                                     MethodElement methodElement,
                                     boolean requiresReflection,
                                     boolean isOptional)
        Description copied from interface: BeanDefinitionVisitor
        Visits an injection point for a setter.
        Specified by:
        visitSetterValue in interface BeanDefinitionVisitor
        Parameters:
        declaringType - The declaring type
        methodElement - The method element
        requiresReflection - Whether the setter requires reflection
        isOptional - Whether the setter is optional
      • visitPostConstructMethod

        public void visitPostConstructMethod​(TypedElement declaringType,
                                             MethodElement methodElement,
                                             boolean requiresReflection,
                                             VisitorContext visitorContext)
        Description copied from interface: BeanDefinitionVisitor
        Visits a method injection point.
        Specified by:
        visitPostConstructMethod in interface BeanDefinitionVisitor
        Parameters:
        declaringType - The declaring type of the method. Either a Class or a string representing the name of the type
        methodElement - The method element
        requiresReflection - Whether the method requires reflection
        visitorContext - The visitor context
      • visitPreDestroyMethod

        public void visitPreDestroyMethod​(TypedElement declaringType,
                                          MethodElement methodElement,
                                          boolean requiresReflection,
                                          VisitorContext visitorContext)
        Description copied from interface: BeanDefinitionVisitor
        Visits a method injection point.
        Specified by:
        visitPreDestroyMethod in interface BeanDefinitionVisitor
        Parameters:
        declaringType - The bean type of the method
        methodElement - The method element
        requiresReflection - Whether the method requires reflection
        visitorContext - The visitor context
      • visitMethodInjectionPoint

        public void visitMethodInjectionPoint​(TypedElement declaringType,
                                              MethodElement methodElement,
                                              boolean requiresReflection,
                                              VisitorContext visitorContext)
        Description copied from interface: BeanDefinitionVisitor
        Visits a method injection point.
        Specified by:
        visitMethodInjectionPoint in interface BeanDefinitionVisitor
        Parameters:
        declaringType - The bean type of the method
        methodElement - The method element
        requiresReflection - Whether the method requires reflection
        visitorContext - The visitor context
      • visitExecutableMethod

        public int visitExecutableMethod​(TypedElement declaringBean,
                                         MethodElement methodElement,
                                         VisitorContext visitorContext)
        Description copied from interface: BeanDefinitionVisitor
        Visit a method that is to be made executable allow invocation of said method without reflection.
        Specified by:
        visitExecutableMethod in interface BeanDefinitionVisitor
        Parameters:
        declaringBean - The declaring bean of the method. Note this may differ from MemberElement.getDeclaringType() in the case of the method coming from a super class or interface.
        methodElement - The method element
        visitorContext - The visitor context
        Returns:
        The index of a new method
      • visitExecutableMethod

        public int visitExecutableMethod​(TypedElement declaringType,
                                         MethodElement methodElement,
                                         java.lang.String interceptedProxyClassName,
                                         java.lang.String interceptedProxyBridgeMethodName)
        Visit a method that is to be made executable allow invocation of said method without reflection.
        Parameters:
        declaringType - The declaring type of the method. Either a Class or a string representing the name of the type
        methodElement - The method element
        interceptedProxyClassName - The intercepted proxy class name
        interceptedProxyBridgeMethodName - The intercepted proxy bridge method name
        Returns:
        The index of a new method.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getAnnotationMetadata

        public io.micronaut.core.annotation.AnnotationMetadata getAnnotationMetadata()
        Specified by:
        getAnnotationMetadata in interface io.micronaut.core.annotation.AnnotationMetadataProvider
        Specified by:
        getAnnotationMetadata in interface BeanDefinitionVisitor
        Returns:
        The annotation metadata
      • visitConfigBuilderDurationMethod

        public void visitConfigBuilderDurationMethod​(java.lang.String prefix,
                                                     ClassElement returnType,
                                                     java.lang.String methodName,
                                                     java.lang.String path)
        Description copied from interface: BeanDefinitionVisitor
        Visit a configuration builder method that accepts a long and a TimeUnit.
        Specified by:
        visitConfigBuilderDurationMethod in interface BeanDefinitionVisitor
        Parameters:
        prefix - The prefix used for the method
        returnType - The return type
        methodName - The method name
        path - The property path
        See Also:
        ConfigurationBuilder
      • visitConfigBuilderMethod

        public void visitConfigBuilderMethod​(java.lang.String prefix,
                                             ClassElement returnType,
                                             java.lang.String methodName,
                                             ClassElement paramType,
                                             java.util.Map<java.lang.String,​ClassElement> generics,
                                             java.lang.String path)
        Description copied from interface: BeanDefinitionVisitor
        Visit a configuration builder method.
        Specified by:
        visitConfigBuilderMethod in interface BeanDefinitionVisitor
        Parameters:
        prefix - The prefix used for the method
        returnType - The return type
        methodName - The method name
        paramType - The method type
        generics - The generic types of the method
        path - The property path
        See Also:
        ConfigurationBuilder
      • visitTypeArguments

        public void visitTypeArguments​(java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​ClassElement>> typeArguments)
        Description copied from interface: BeanDefinitionVisitor
        Visits the type arguments for the bean.
        Specified by:
        visitTypeArguments in interface BeanDefinitionVisitor
        Parameters:
        typeArguments - The type arguments
      • visitFieldInjectionPoint

        public void visitFieldInjectionPoint​(TypedElement declaringType,
                                             FieldElement fieldElement,
                                             boolean requiresReflection)
        Description copied from interface: BeanDefinitionVisitor
        Visits a field injection point.
        Specified by:
        visitFieldInjectionPoint in interface BeanDefinitionVisitor
        Parameters:
        declaringType - The declaring type. Either a Class or a string representing the name of the type
        fieldElement - The field element
        requiresReflection - Whether accessing the field requires reflection
      • visitAnnotationMemberPropertyInjectionPoint

        public void visitAnnotationMemberPropertyInjectionPoint​(TypedElement annotationMemberBeanType,
                                                                java.lang.String annotationMemberProperty,
                                                                @Nullable
                                                                java.lang.String requiredValue,
                                                                @Nullable
                                                                java.lang.String notEqualsValue)
        Description copied from interface: BeanDefinitionVisitor
        Visits an annotation injection point.
        Specified by:
        visitAnnotationMemberPropertyInjectionPoint in interface BeanDefinitionVisitor
        Parameters:
        annotationMemberBeanType - The type of the injected bean
        annotationMemberProperty - Required property of the injected bean
        requiredValue - Required value of the bean property for the bean to be loaded
        notEqualsValue - The bean property value which should not be equal to present value for the bean to be loaded
      • visitFieldValue

        public void visitFieldValue​(TypedElement declaringType,
                                    FieldElement fieldElement,
                                    boolean requiresReflection,
                                    boolean isOptional)
        Description copied from interface: BeanDefinitionVisitor
        Visits a field injection point.
        Specified by:
        visitFieldValue in interface BeanDefinitionVisitor
        Parameters:
        declaringType - The declaring type. Either a Class or a string representing the name of the type
        fieldElement - The field element
        requiresReflection - Whether accessing the field requires reflection
        isOptional - Is the value optional
      • getName

        public java.lang.String getName()
        Specified by:
        getName in interface Element
        Specified by:
        getName in interface io.micronaut.core.naming.Named
        Returns:
        The name of the element. For a type this represents the binary name.
      • isProtected

        public boolean isProtected()
        Specified by:
        isProtected in interface Element
        Returns:
        True if the element is protected.
      • isPublic

        public boolean isPublic()
        Specified by:
        isPublic in interface Element
        Returns:
        True if the element is public.
      • getNativeType

        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 interface Element
        Returns:
        The native type
      • isAbstract

        public boolean isAbstract()
        Specified by:
        isAbstract in interface Element
        Returns:
        True if the element is abstract.
      • annotate

        public <T extends java.lang.annotation.Annotation> Element annotate​(java.lang.String annotationType,
                                                                            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 interface Element
        Type Parameters:
        T - The annotation generic type
        Parameters:
        annotationType - The annotation type
        consumer - A function that receives the AnnotationValueBuilder
        Returns:
        This element
      • removeAnnotation

        public Element removeAnnotation​(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 interface Element
        Parameters:
        annotationType - The annotation type
        Returns:
        This element
      • removeAnnotationIf

        public <T extends java.lang.annotation.Annotation> Element removeAnnotationIf​(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 interface Element
        Type Parameters:
        T - The annotation generic type
        Parameters:
        predicate - The predicate
        Returns:
        This element
      • removeStereotype

        public Element removeStereotype​(java.lang.String annotationType)
        Description copied from interface: Element
        Removes a stereotype of the given name from the element.
        Specified by:
        removeStereotype in interface Element
        Parameters:
        annotationType - The annotation type
        Returns:
        This element
      • getScope

        public java.util.Optional<java.lang.String> getScope()
        Description copied from interface: BeanElement
        The scope of the bean.
        Specified by:
        getScope in interface BeanElement
        Returns:
        The fully qualified name of the scope or empty if no scope is defined.
      • getQualifiers

        public java.util.Collection<java.lang.String> getQualifiers()
        Specified by:
        getQualifiers in interface BeanElement
        Returns:
        One or more fully qualified qualifier types defined by the bean.
      • addAssociatedBean

        public BeanElementBuilder addAssociatedBean​(ClassElement type,
                                                    VisitorContext visitorContext)
        Description copied from interface: BeanElement
        This 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 UnsupportedOperationException will be thrown.

        Specified by:
        addAssociatedBean in interface BeanElement
        Parameters:
        type - The type of the bean
        visitorContext - The visitor context
        Returns:
        A bean builder