Search in sources :

Example 6 with AnnotationMetadataHierarchy

use of io.micronaut.inject.annotation.AnnotationMetadataHierarchy in project micronaut-core by micronaut-projects.

the class JavaBeanDefinitionBuilder method createChildBean.

@Override
protected AbstractBeanDefinitionBuilder createChildBean(MethodElement producerMethod) {
    final ClassElement parentType = getBeanType();
    return new JavaBeanDefinitionBuilder(JavaBeanDefinitionBuilder.this.getOriginatingElement(), producerMethod.getGenericReturnType(), JavaBeanDefinitionBuilder.this.metadataBuilder, (JavaVisitorContext) JavaBeanDefinitionBuilder.this.visitorContext) {

        @Override
        public Element getProducingElement() {
            return producerMethod;
        }

        @Override
        public ClassElement getDeclaringElement() {
            return producerMethod.getDeclaringType();
        }

        @Override
        protected BeanDefinitionWriter createBeanDefinitionWriter() {
            final BeanDefinitionWriter writer = super.createBeanDefinitionWriter();
            final JavaElementFactory elementFactory = ((JavaVisitorContext) visitorContext).getElementFactory();
            final ExecutableElement variableElement = (ExecutableElement) producerMethod.getNativeType();
            writer.visitBeanFactoryMethod(parentType, elementFactory.newMethodElement(parentType, variableElement, new AnnotationMetadataHierarchy(parentType.getDeclaredMetadata(), producerMethod.getDeclaredMetadata())));
            return writer;
        }
    };
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ClassElement(io.micronaut.inject.ast.ClassElement) BeanDefinitionWriter(io.micronaut.inject.writer.BeanDefinitionWriter) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy)

Example 7 with AnnotationMetadataHierarchy

use of io.micronaut.inject.annotation.AnnotationMetadataHierarchy in project micronaut-core by micronaut-projects.

the class GroovyBeanDefinitionBuilder method createChildBean.

@Override
protected AbstractBeanDefinitionBuilder createChildBean(FieldElement producerField) {
    final ClassElement parentType = getBeanType();
    return new GroovyBeanDefinitionBuilder(GroovyBeanDefinitionBuilder.this.getOriginatingElement(), producerField.getGenericField().getType(), GroovyBeanDefinitionBuilder.this.metadataBuilder, GroovyBeanDefinitionBuilder.this.visitorContext) {

        @Override
        public Element getProducingElement() {
            return producerField;
        }

        @Override
        public ClassElement getDeclaringElement() {
            return producerField.getDeclaringType();
        }

        @Override
        protected BeanDefinitionWriter createBeanDefinitionWriter() {
            final BeanDefinitionWriter writer = super.createBeanDefinitionWriter();
            final GroovyElementFactory elementFactory = ((GroovyVisitorContext) visitorContext).getElementFactory();
            final FieldNode fieldNode = (FieldNode) producerField.getNativeType();
            writer.visitBeanFactoryField(parentType, elementFactory.newFieldElement(parentType, fieldNode, new AnnotationMetadataHierarchy(parentType.getDeclaredMetadata(), producerField.getDeclaredMetadata())));
            return writer;
        }
    };
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ClassElement(io.micronaut.inject.ast.ClassElement) BeanDefinitionWriter(io.micronaut.inject.writer.BeanDefinitionWriter) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy)

Example 8 with AnnotationMetadataHierarchy

use of io.micronaut.inject.annotation.AnnotationMetadataHierarchy in project micronaut-core by micronaut-projects.

the class GroovyBeanDefinitionBuilder method createChildBean.

@Override
protected AbstractBeanDefinitionBuilder createChildBean(MethodElement producerMethod) {
    final ClassElement parentType = getBeanType();
    return new GroovyBeanDefinitionBuilder(GroovyBeanDefinitionBuilder.this.getOriginatingElement(), producerMethod.getGenericReturnType().getType(), GroovyBeanDefinitionBuilder.this.metadataBuilder, GroovyBeanDefinitionBuilder.this.visitorContext) {

        @Override
        public Element getProducingElement() {
            return producerMethod;
        }

        @Override
        public ClassElement getDeclaringElement() {
            return producerMethod.getDeclaringType();
        }

        @Override
        protected BeanDefinitionWriter createBeanDefinitionWriter() {
            final BeanDefinitionWriter writer = super.createBeanDefinitionWriter();
            final GroovyElementFactory elementFactory = ((GroovyVisitorContext) visitorContext).getElementFactory();
            final MethodNode methodNode = (MethodNode) producerMethod.getNativeType();
            writer.visitBeanFactoryMethod(parentType, elementFactory.newMethodElement(parentType, methodNode, new AnnotationMetadataHierarchy(parentType.getDeclaredMetadata(), producerMethod.getDeclaredMetadata())));
            return writer;
        }
    };
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ClassElement(io.micronaut.inject.ast.ClassElement) BeanDefinitionWriter(io.micronaut.inject.writer.BeanDefinitionWriter) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy)

Example 9 with AnnotationMetadataHierarchy

use of io.micronaut.inject.annotation.AnnotationMetadataHierarchy in project micronaut-core by micronaut-projects.

the class BeanDefinitionWriter method isConstructorIntercepted.

private boolean isConstructorIntercepted(Element constructor) {
    // a constructor is intercepted when this bean is an advised type but not proxied
    // and any AROUND_CONSTRUCT annotations are present
    AnnotationMetadataHierarchy annotationMetadata = new AnnotationMetadataHierarchy(this.annotationMetadata, constructor.getAnnotationMetadata());
    final String interceptType = "AROUND_CONSTRUCT";
    return isInterceptedLifeCycleByType(annotationMetadata, interceptType);
}
Also used : AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy)

Example 10 with AnnotationMetadataHierarchy

use of io.micronaut.inject.annotation.AnnotationMetadataHierarchy in project micronaut-core by micronaut-projects.

the class BeanDefinitionWriter method visitSetterValue.

@Override
public void visitSetterValue(TypedElement declaringType, MethodElement methodElement, boolean requiresReflection, boolean isOptional) {
    if (!requiresReflection) {
        ParameterElement parameter = methodElement.getParameters()[0];
        AnnotationMetadataHierarchy annotationMetadata = new AnnotationMetadataHierarchy(parameter.getAnnotationMetadata(), methodElement.getAnnotationMetadata());
        Label falseCondition = isOptional ? pushPropertyContainsCheck(injectMethodVisitor, parameter.getType(), parameter.getName(), annotationMetadata) : null;
        AnnotationMetadata currentAnnotationMetadata = methodElement.getAnnotationMetadata();
        ClassElement genericType = parameter.getGenericType();
        if (isConfigurationProperties && isValueType(currentAnnotationMetadata)) {
            injectMethodVisitor.loadLocal(injectInstanceLocalVarIndex, beanType);
            Optional<String> property = currentAnnotationMetadata.stringValue(Property.class, "name");
            Optional<String> valueValue = parameter.stringValue(Value.class);
            if (isInnerType(genericType)) {
                boolean isArray = genericType.isArray();
                boolean isCollection = genericType.isAssignable(Collection.class);
                if (isCollection || isArray) {
                    ClassElement typeArgument = genericType.isArray() ? genericType.fromArray() : genericType.getFirstTypeArgument().orElse(null);
                    if (typeArgument != null && !typeArgument.isPrimitive()) {
                        pushInvokeGetBeansOfTypeForSetter(injectMethodVisitor, methodElement.getName(), parameter);
                    } else {
                        pushInvokeGetBeanForSetter(injectMethodVisitor, methodElement.getName(), parameter);
                    }
                } else {
                    pushInvokeGetBeanForSetter(injectMethodVisitor, methodElement.getName(), parameter);
                }
            } else if (property.isPresent()) {
                pushInvokeGetPropertyValueForSetter(injectMethodVisitor, methodElement.getName(), parameter, property.get());
            } else if (valueValue.isPresent()) {
                pushInvokeGetPropertyPlaceholderValueForSetter(injectMethodVisitor, methodElement.getName(), parameter, valueValue.get());
            } else {
                throw new IllegalStateException();
            }
            Type declaringTypeRef = JavaModelUtils.getTypeReference(declaringType);
            String methodDescriptor = getMethodDescriptor(methodElement.getReturnType(), Arrays.asList(methodElement.getParameters()));
            injectMethodVisitor.visitMethodInsn(isInterface ? INVOKEINTERFACE : INVOKEVIRTUAL, declaringTypeRef.getInternalName(), methodElement.getName(), methodDescriptor, isInterface);
            if (methodElement.getReturnType() != PrimitiveElement.VOID) {
                injectMethodVisitor.pop();
            }
            if (keepConfPropInjectPoints) {
                final MethodVisitData methodVisitData = new MethodVisitData(declaringType, methodElement, false);
                methodInjectionPoints.add(methodVisitData);
                allMethodVisits.add(methodVisitData);
                currentMethodIndex++;
            }
        } else {
            final MethodVisitData methodVisitData = new MethodVisitData(declaringType, methodElement, false);
            visitMethodInjectionPointInternal(methodVisitData, injectMethodVisitor, injectInstanceLocalVarIndex);
            methodInjectionPoints.add(methodVisitData);
            allMethodVisits.add(methodVisitData);
            currentMethodIndex++;
        }
        if (falseCondition != null) {
            injectMethodVisitor.visitLabel(falseCondition);
        }
    } else {
        final MethodVisitData methodVisitData = new MethodVisitData(declaringType, methodElement, false);
        methodInjectionPoints.add(methodVisitData);
        allMethodVisits.add(methodVisitData);
        currentMethodIndex++;
    }
}
Also used : Type(org.objectweb.asm.Type) AdvisedBeanType(io.micronaut.inject.AdvisedBeanType) Label(org.objectweb.asm.Label) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy) MutableAnnotationMetadata(io.micronaut.inject.annotation.MutableAnnotationMetadata) DefaultAnnotationMetadata(io.micronaut.inject.annotation.DefaultAnnotationMetadata) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Aggregations

AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)12 ClassElement (io.micronaut.inject.ast.ClassElement)7 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)5 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)4 DefaultAnnotationMetadata (io.micronaut.inject.annotation.DefaultAnnotationMetadata)4 BeanDefinitionWriter (io.micronaut.inject.writer.BeanDefinitionWriter)4 Type (org.objectweb.asm.Type)4 Internal (io.micronaut.core.annotation.Internal)3 NonNull (io.micronaut.core.annotation.NonNull)3 Argument (io.micronaut.core.type.Argument)3 ParameterElement (io.micronaut.inject.ast.ParameterElement)3 BeanRegistration (io.micronaut.context.BeanRegistration)2 DefaultBeanContext (io.micronaut.context.DefaultBeanContext)2 Nullable (io.micronaut.core.annotation.Nullable)2 CollectionUtils (io.micronaut.core.util.CollectionUtils)2 AdvisedBeanType (io.micronaut.inject.AdvisedBeanType)2 MutableAnnotationMetadata (io.micronaut.inject.annotation.MutableAnnotationMetadata)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2