Search in sources :

Example 1 with BeanElementBuilder

use of io.micronaut.inject.ast.beans.BeanElementBuilder in project micronaut-core by micronaut-projects.

the class AbstractBeanDefinitionBuilder method produceBeans.

@Override
public <E extends MemberElement> BeanElementBuilder produceBeans(ElementQuery<E> methodsOrFields, Consumer<BeanElementBuilder> childBeanBuilder) {
    methodsOrFields = methodsOrFields.onlyConcrete().onlyInstance().modifiers((modifiers) -> modifiers.contains(ElementModifier.PUBLIC));
    final List<E> enclosedElements = this.beanType.getEnclosedElements(methodsOrFields);
    for (E enclosedElement : enclosedElements) {
        if (enclosedElement instanceof FieldElement) {
            FieldElement fe = (FieldElement) enclosedElement;
            final ClassElement type = fe.getGenericField().getType();
            if (type.isPublic() && !type.isPrimitive()) {
                return addChildBean(fe, childBeanBuilder);
            }
        }
        if (enclosedElement instanceof MethodElement && !(enclosedElement instanceof ConstructorElement)) {
            MethodElement me = (MethodElement) enclosedElement;
            final ClassElement type = me.getGenericReturnType().getType();
            if (type.isPublic() && !type.isPrimitive()) {
                return addChildBean(me, childBeanBuilder);
            }
        }
    }
    return this;
}
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) ConstructorElement(io.micronaut.inject.ast.ConstructorElement) BeanConstructorElement(io.micronaut.inject.ast.beans.BeanConstructorElement) 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)

Example 2 with BeanElementBuilder

use of io.micronaut.inject.ast.beans.BeanElementBuilder in project micronaut-core by micronaut-projects.

the class TestBeanFactoryDefiningVisitor method visitClass.

@Override
public void visitClass(ClassElement element, VisitorContext context) {
    if (element.hasAnnotation(Prototype.class)) {
        context.getClassElement(TestBeanProducer.class).ifPresent((producer) -> {
            final BeanElementBuilder beanElementBuilder = element.addAssociatedBean(producer);
            final ElementQuery<MethodElement> query = ElementQuery.ALL_METHODS.annotated((am) -> am.hasAnnotation(TestProduces.class));
            beanElementBuilder.produceBeans(query);
            final ElementQuery<FieldElement> fields = ElementQuery.ALL_FIELDS.annotated((am) -> am.hasAnnotation(TestProduces.class));
            beanElementBuilder.produceBeans(fields);
        });
    }
}
Also used : BeanElementBuilder(io.micronaut.inject.ast.beans.BeanElementBuilder) MethodElement(io.micronaut.inject.ast.MethodElement) FieldElement(io.micronaut.inject.ast.FieldElement)

Aggregations

FieldElement (io.micronaut.inject.ast.FieldElement)2 MethodElement (io.micronaut.inject.ast.MethodElement)2 BeanElementBuilder (io.micronaut.inject.ast.beans.BeanElementBuilder)2 Bean (io.micronaut.context.annotation.Bean)1 Property (io.micronaut.context.annotation.Property)1 Value (io.micronaut.context.annotation.Value)1 AnnotationClassValue (io.micronaut.core.annotation.AnnotationClassValue)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 AnnotationValueBuilder (io.micronaut.core.annotation.AnnotationValueBuilder)1 Internal (io.micronaut.core.annotation.Internal)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 ArrayUtils (io.micronaut.core.util.ArrayUtils)1 MutableAnnotationMetadata (io.micronaut.inject.annotation.MutableAnnotationMetadata)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 ConstructorElement (io.micronaut.inject.ast.ConstructorElement)1 Element (io.micronaut.inject.ast.Element)1 ElementFactory (io.micronaut.inject.ast.ElementFactory)1 ElementModifier (io.micronaut.inject.ast.ElementModifier)1