Class BeanInjectionUtils

java.lang.Object
io.micronaut.inject.utils.BeanInjectionUtils

public class BeanInjectionUtils extends Object
Utility methods for creating BeanDefinitionInjectionPoint instances.
Since:
5.1.0
Author:
Denis Stepanov
  • Constructor Details

    • BeanInjectionUtils

      public BeanInjectionUtils()
  • Method Details

    • createFieldDefinition

      public static FieldDefinition<ClassElement, FieldElement> createFieldDefinition(ClassElement beanType, FieldElement fieldElement, boolean requiresReflection, VisitorContext visitorContext)
      Creates a FieldDefinition describing injection for the supplied field.
      Parameters:
      beanType - The declaring bean type
      fieldElement - The field element
      requiresReflection - Whether reflective access is required
      visitorContext - The visitor context
      Returns:
      The field definition
    • createMethodDefinition

      public static MethodDefinition<ClassElement, MethodElement> createMethodDefinition(ClassElement beanType, MethodElement methodElement, AnnotationMetadata annotationMetadata, boolean requiresReflection, VisitorContext visitorContext)
      Creates a MethodDefinition describing injection for the supplied method.
      Parameters:
      beanType - The declaring bean type
      methodElement - The method element
      annotationMetadata - The method annotation metadata
      requiresReflection - Whether reflective access is required
      visitorContext - The visitor context
      Returns:
      The method definition
    • createConstructorDefinition

      public static ConstructorDefinition<ClassElement, MethodElement> createConstructorDefinition(MethodElement constructorElement, VisitorContext visitorContext)
      Creates a ConstructorDefinition for the supplied constructor.
      Parameters:
      constructorElement - The constructor element
      visitorContext - The visitor context
      Returns:
      The constructor definition
    • findBeanConstructor

      public static Optional<MethodElement> findBeanConstructor(ClassElement classElement)
      Finds the constructor a bean of the given type is instantiated with.

      This is ClassElement.getPrimaryConstructor() extended to private constructors, which that lookup never selects. A private constructor annotated with AnnotationUtil.INJECT or Creator takes precedence over an unannotated accessible one when it is annotated with ReflectiveAccess, and a type whose only constructor is private is instantiated with it only when it is annotated with ReflectiveAccess. A private constructor is invoked with reflection, as private injected fields and methods are.

      Parameters:
      classElement - The bean type
      Returns:
      The constructor, or a static creator method, if one is found
      Since:
      5.2.1
    • validateBeanConstructor

      public static void validateBeanConstructor(ClassElement classElement)
      Validates that a constructor selected for injection can be invoked.

      A private constructor is invoked with reflection, which has to be opted into with ReflectiveAccess. Without it findBeanConstructor(ClassElement) skips the constructor, so the injection the annotation asks for would silently not happen.

      Parameters:
      classElement - The bean type
      Throws:
      ProcessingException - if a constructor asks for injection but cannot be invoked
      Since:
      5.2.1
    • createConstructorDefinition

      public static ConstructorDefinition<ClassElement, MethodElement> createConstructorDefinition(MethodElement constructorElement, VisitorContext visitorContext, boolean requiresReflection)
      Creates a ConstructorDefinition for the supplied constructor.
      Parameters:
      constructorElement - The constructor element
      visitorContext - The visitor context
      requiresReflection - Whether reflective access is required
      Returns:
      The constructor definition
    • createConstructorDefinition

      public static ConstructorDefinition<ClassElement, MethodElement> createConstructorDefinition(MethodElement constructorElement, AnnotationMetadata annotationMetadata, VisitorContext visitorContext, boolean requiresReflection)
      Creates a ConstructorDefinition using the provided annotation metadata.
      Parameters:
      constructorElement - The constructor element
      annotationMetadata - The annotation metadata
      visitorContext - The visitor context
      requiresReflection - Whether reflective access is required
      Returns:
      The constructor definition
    • createValueInjectionPoint

      public static BeanDefinitionInjectionPoint<ClassElement> createValueInjectionPoint(ClassElement beanType, ClassElement genericType, AnnotationMetadata annotationMetadata, boolean requiresReflection, String name, VisitorContext visitorContext)
      Creates an injection point for Value or Property-driven injection.
      Parameters:
      beanType - The bean type declaring the injection point
      genericType - The injected type
      annotationMetadata - The associated annotation metadata
      requiresReflection - Whether reflective access is required
      name - The element name
      visitorContext - The visitor context
      Returns:
      The injection point
    • getInjectionPoint

      public static BeanDefinitionInjectionPoint<ClassElement> getInjectionPoint(ClassElement beanType, ClassElement genericType, AnnotationMetadata annotationMetadata, String parameterName, VisitorContext visitorContext)
      Resolves the appropriate BeanDefinitionInjectionPoint for the given element.
      Parameters:
      beanType - The bean type declaring the injection point
      genericType - The injected type
      annotationMetadata - The associated annotation metadata
      parameterName - The element name
      visitorContext - The visitor context
      Returns:
      The injection point