Search in sources :

Example 6 with VisitorContext

use of io.micronaut.inject.visitor.VisitorContext in project micronaut-core by micronaut-projects.

the class AbstractBeanDefinitionBuilder method build.

/**
 * Build the bean definition writer.
 * @return The writer, possibly null if it wasn't possible to build it
 */
@Nullable
public BeanDefinitionWriter build() {
    if (exposedTypes != null) {
        final AnnotationClassValue[] annotationClassValues = Arrays.stream(exposedTypes).map(ce -> new AnnotationClassValue<>(ce.getName())).toArray(AnnotationClassValue[]::new);
        annotate(Bean.class, (builder) -> builder.member("typed", annotationClassValues));
    }
    final BeanDefinitionWriter beanDefinitionWriter = createBeanDefinitionWriter();
    if (typeArguments != null) {
        beanDefinitionWriter.visitTypeArguments(this.typeArguments);
    }
    if (constructorElement == null) {
        constructorElement = initConstructor(beanType);
    }
    if (constructorElement == null) {
        visitorContext.fail("Cannot create associated bean with no accessible primary constructor. Consider supply the constructor with createWith(..)", originatingElement);
        return null;
    } else {
        beanDefinitionWriter.visitBeanDefinitionConstructor(constructorElement, !constructorElement.isPublic(), visitorContext);
    }
    Map<ClassElement, List<MemberElement>> sortedInjections = new LinkedHashMap<>();
    List<MemberElement> allInjected = new ArrayList<>();
    allInjected.addAll(injectedFields);
    allInjected.addAll(injectedMethods);
    allInjected.sort(SORTER);
    for (MemberElement memberElement : allInjected) {
        final List<MemberElement> list = sortedInjections.computeIfAbsent(memberElement.getDeclaringType(), classElement -> new ArrayList<>());
        list.add(memberElement);
    }
    for (List<MemberElement> members : sortedInjections.values()) {
        members.sort((o1, o2) -> {
            if (o1 instanceof FieldElement && o2 instanceof MethodElement) {
                return 1;
            } else if (o1 instanceof MethodElement && o1 instanceof FieldElement) {
                return -1;
            }
            return 0;
        });
    }
    for (List<MemberElement> list : sortedInjections.values()) {
        for (MemberElement memberElement : list) {
            if (memberElement instanceof FieldElement) {
                InternalBeanElementField ibf = (InternalBeanElementField) memberElement;
                ibf.<InternalBeanElementField>with((element) -> visitField(beanDefinitionWriter, element, element));
            } else {
                InternalBeanElementMethod ibm = (InternalBeanElementMethod) memberElement;
                ibm.<InternalBeanElementMethod>with((element) -> beanDefinitionWriter.visitMethodInjectionPoint(ibm.getDeclaringType(), ibm, ibm.isRequiresReflection(), visitorContext));
            }
        }
    }
    for (BeanMethodElement executableMethod : executableMethods) {
        beanDefinitionWriter.visitExecutableMethod(beanType, executableMethod, visitorContext);
    }
    for (BeanMethodElement postConstructMethod : postConstructMethods) {
        if (postConstructMethod.getDeclaringType().equals(beanType)) {
            beanDefinitionWriter.visitPostConstructMethod(beanType, postConstructMethod, ((InternalBeanElementMethod) postConstructMethod).isRequiresReflection(), visitorContext);
        }
    }
    for (BeanMethodElement preDestroyMethod : preDestroyMethods) {
        if (preDestroyMethod.getDeclaringType().equals(beanType)) {
            beanDefinitionWriter.visitPreDestroyMethod(beanType, preDestroyMethod, ((InternalBeanElementMethod) preDestroyMethod).isRequiresReflection(), visitorContext);
        }
    }
    beanDefinitionWriter.visitBeanDefinitionEnd();
    return beanDefinitionWriter;
}
Also used : ElementQuery(io.micronaut.inject.ast.ElementQuery) Arrays(java.util.Arrays) ArrayUtils(io.micronaut.core.util.ArrayUtils) FieldElement(io.micronaut.inject.ast.FieldElement) BeanFieldElement(io.micronaut.inject.ast.beans.BeanFieldElement) ClassElement(io.micronaut.inject.ast.ClassElement) HashMap(java.util.HashMap) Internal(io.micronaut.core.annotation.Internal) ConstructorElement(io.micronaut.inject.ast.ConstructorElement) BeanMethodElement(io.micronaut.inject.ast.beans.BeanMethodElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) PreDestroy(javax.annotation.PreDestroy) ElementFactory(io.micronaut.inject.ast.ElementFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) ElementModifier(io.micronaut.inject.ast.ElementModifier) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) Property(io.micronaut.context.annotation.Property) Element(io.micronaut.inject.ast.Element) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Set(java.util.Set) IOException(java.io.IOException) Value(io.micronaut.context.annotation.Value) BeanParameterElement(io.micronaut.inject.ast.beans.BeanParameterElement) MemberElement(io.micronaut.inject.ast.MemberElement) Bean(io.micronaut.context.annotation.Bean) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) VisitorContext(io.micronaut.inject.visitor.VisitorContext) List(java.util.List) BeanConstructorElement(io.micronaut.inject.ast.beans.BeanConstructorElement) AnnotationValueBuilder(io.micronaut.core.annotation.AnnotationValueBuilder) BeanElementBuilder(io.micronaut.inject.ast.beans.BeanElementBuilder) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) MethodElement(io.micronaut.inject.ast.MethodElement) ConfigurationMetadataBuilder(io.micronaut.inject.configuration.ConfigurationMetadataBuilder) Annotation(java.lang.annotation.Annotation) PostConstruct(javax.annotation.PostConstruct) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Comparator(java.util.Comparator) Collections(java.util.Collections) MutableAnnotationMetadata(io.micronaut.inject.annotation.MutableAnnotationMetadata) ArrayList(java.util.ArrayList) FieldElement(io.micronaut.inject.ast.FieldElement) BeanFieldElement(io.micronaut.inject.ast.beans.BeanFieldElement) BeanMethodElement(io.micronaut.inject.ast.beans.BeanMethodElement) MethodElement(io.micronaut.inject.ast.MethodElement) ClassElement(io.micronaut.inject.ast.ClassElement) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) LinkedHashMap(java.util.LinkedHashMap) BeanMethodElement(io.micronaut.inject.ast.beans.BeanMethodElement) ArrayList(java.util.ArrayList) List(java.util.List) MemberElement(io.micronaut.inject.ast.MemberElement) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

VisitorContext (io.micronaut.inject.visitor.VisitorContext)6 NonNull (io.micronaut.core.annotation.NonNull)4 io.micronaut.context.annotation (io.micronaut.context.annotation)3 Bean (io.micronaut.context.annotation.Bean)3 io.micronaut.core.annotation (io.micronaut.core.annotation)3 NameUtils (io.micronaut.core.naming.NameUtils)3 CollectionUtils (io.micronaut.core.util.CollectionUtils)3 StringUtils (io.micronaut.core.util.StringUtils)3 IOException (java.io.IOException)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Import (io.micronaut.context.annotation.Import)2 BeanImportVisitor (io.micronaut.context.visitor.BeanImportVisitor)2 AnnotationUtil (io.micronaut.core.annotation.AnnotationUtil)2 Creator (io.micronaut.core.annotation.Creator)2 Nullable (io.micronaut.core.annotation.Nullable)2