Search in sources :

Example 26 with ClassElement

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

the class AsyncTypeElementVisitor method visitMethod.

@Override
public void visitMethod(MethodElement element, VisitorContext context) {
    ClassElement returnType = element.getReturnType();
    boolean isValid = returnType != null && (returnType.isAssignable(CompletionStage.class) || returnType.isAssignable(void.class) || Publishers.getKnownReactiveTypes().stream().anyMatch(returnType::isAssignable));
    if (!isValid) {
        context.fail("Method must return void or a subtype of CompletionStage", element);
    }
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement)

Example 27 with ClassElement

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

the class AopProxyWriter method buildMethodDelegate.

private void buildMethodDelegate(MethodElement methodElement, MethodElement overriddenBy, boolean isVoidReturn) {
    String desc = getMethodDescriptor(methodElement.getReturnType().getType(), Arrays.asList(methodElement.getSuspendParameters()));
    MethodVisitor overridden = classWriter.visitMethod(ACC_PUBLIC, methodElement.getName(), desc, null, null);
    GeneratorAdapter overriddenMethodGenerator = new GeneratorAdapter(overridden, ACC_PUBLIC, methodElement.getName(), desc);
    overriddenMethodGenerator.loadThis();
    int i = 0;
    for (ParameterElement param : methodElement.getSuspendParameters()) {
        overriddenMethodGenerator.loadArg(i++);
        pushCastToType(overriddenMethodGenerator, param.getGenericType());
    }
    overriddenMethodGenerator.visitMethodInsn(INVOKESPECIAL, proxyType.getInternalName(), overriddenBy.getName(), getMethodDescriptor(overriddenBy.getReturnType().getType(), Arrays.asList(overriddenBy.getSuspendParameters())), this.isInterface && overriddenBy.isDefault());
    if (isVoidReturn) {
        overriddenMethodGenerator.returnValue();
    } else {
        ClassElement returnType = overriddenBy.getReturnType();
        pushCastToType(overriddenMethodGenerator, returnType);
        pushReturnValue(overriddenMethodGenerator, overriddenBy.getReturnType());
    }
    overriddenMethodGenerator.visitMaxs(DEFAULT_MAX_STACK, 1);
    overriddenMethodGenerator.visitEnd();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 28 with ClassElement

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

the class AopProxyWriter method initConstructor.

private void initConstructor(MethodElement constructor) {
    final ClassElement interceptorList = ClassElement.of(List.class, AnnotationMetadata.EMPTY_METADATA, Collections.singletonMap("E", ClassElement.of(BeanRegistration.class, AnnotationMetadata.EMPTY_METADATA, Collections.singletonMap("T", ClassElement.of(Interceptor.class)))));
    this.interceptorParameter = ParameterElement.of(interceptorList, "$interceptors");
    this.qualifierParameter = ParameterElement.of(Qualifier.class, "$qualifier");
    ClassElement proxyClass = ClassElement.of(proxyType.getClassName());
    ParameterElement[] constructorParameters = constructor.getParameters();
    List<ParameterElement> newConstructorParameters = new ArrayList<>(constructorParameters.length + 4);
    newConstructorParameters.addAll(Arrays.asList(constructorParameters));
    newConstructorParameters.add(ParameterElement.of(BeanResolutionContext.class, "$beanResolutionContext"));
    newConstructorParameters.add(ParameterElement.of(BeanContext.class, "$beanContext"));
    newConstructorParameters.add(qualifierParameter);
    newConstructorParameters.add(interceptorParameter);
    this.newConstructor = MethodElement.of(proxyClass, constructor.getAnnotationMetadata(), proxyClass, proxyClass, "<init>", newConstructorParameters.toArray(new ParameterElement[0]));
    this.beanResolutionContextArgumentIndex = constructorParameters.length;
    this.beanContextArgumentIndex = constructorParameters.length + 1;
    this.qualifierIndex = constructorParameters.length + 2;
    this.interceptorArgumentIndex = constructorParameters.length + 3;
}
Also used : BeanContext(io.micronaut.context.BeanContext) DefaultBeanContext(io.micronaut.context.DefaultBeanContext) ArrayList(java.util.ArrayList) ClassElement(io.micronaut.inject.ast.ClassElement) Qualifier(io.micronaut.context.Qualifier) ParameterElement(io.micronaut.inject.ast.ParameterElement) BeanResolutionContext(io.micronaut.context.BeanResolutionContext)

Example 29 with ClassElement

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

the class AopProxyWriter method visitDefaultConstructor.

@Override
public void visitDefaultConstructor(AnnotationMetadata annotationMetadata, VisitorContext visitorContext) {
    this.constructorRequiresReflection = false;
    ClassElement classElement = ClassElement.of(proxyType.getClassName());
    this.declaredConstructor = MethodElement.of(classElement, annotationMetadata, classElement, classElement, "<init>");
}
Also used : ClassElement(io.micronaut.inject.ast.ClassElement)

Example 30 with ClassElement

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

the class BeanDefinitionWriter method processConfigurationConstructorParameter.

private void processConfigurationConstructorParameter(ParameterElement parameter, AnnotationMetadata annotationMetadata) {
    ClassElement parameterType = parameter.getGenericType();
    if (!parameterType.hasStereotype(AnnotationUtil.SCOPE)) {
        final PropertyMetadata pm = metadataBuilder.visitProperty(parameterType.getName(), parameter.getName(), parameter.getDocumentation().orElse(null), annotationMetadata.stringValue(Bindable.class, "defaultValue").orElse(null));
        parameter.annotate(Property.class, (builder) -> builder.member("name", pm.getPath()));
    }
}
Also used : PropertyMetadata(io.micronaut.inject.configuration.PropertyMetadata) ClassElement(io.micronaut.inject.ast.ClassElement)

Aggregations

ClassElement (io.micronaut.inject.ast.ClassElement)55 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)15 NonNull (io.micronaut.core.annotation.NonNull)15 ParameterElement (io.micronaut.inject.ast.ParameterElement)14 Map (java.util.Map)13 MethodElement (io.micronaut.inject.ast.MethodElement)12 Type (org.objectweb.asm.Type)12 LinkedHashMap (java.util.LinkedHashMap)10 DefaultAnnotationMetadata (io.micronaut.inject.annotation.DefaultAnnotationMetadata)9 ArrayList (java.util.ArrayList)9 List (java.util.List)8 HashMap (java.util.HashMap)7 Internal (io.micronaut.core.annotation.Internal)6 Argument (io.micronaut.core.type.Argument)6 AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)6 MutableAnnotationMetadata (io.micronaut.inject.annotation.MutableAnnotationMetadata)6 Collections (java.util.Collections)6 Set (java.util.Set)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)6 NameUtils (io.micronaut.core.naming.NameUtils)5