Search in sources :

Example 6 with FieldElement

use of io.micronaut.inject.ast.FieldElement 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

FieldElement (io.micronaut.inject.ast.FieldElement)6 MethodElement (io.micronaut.inject.ast.MethodElement)6 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Internal (io.micronaut.core.annotation.Internal)4 NonNull (io.micronaut.core.annotation.NonNull)4 ClassElement (io.micronaut.inject.ast.ClassElement)4 ConstructorElement (io.micronaut.inject.ast.ConstructorElement)4 ElementModifier (io.micronaut.inject.ast.ElementModifier)4 ElementQuery (io.micronaut.inject.ast.ElementQuery)4 ArrayList (java.util.ArrayList)4 BeanElementBuilder (io.micronaut.inject.ast.beans.BeanElementBuilder)3 Collections (java.util.Collections)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Set (java.util.Set)3 Predicate (java.util.function.Predicate)3 AnnotationUtils (io.micronaut.annotation.processing.AnnotationUtils)2 ModelUtils (io.micronaut.annotation.processing.ModelUtils)2