Search in sources :

Example 1 with DefaultAnnotationMetadata

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

the class AbstractClassFileWriter method buildArgumentWithGenerics.

/**
 * Builds generic type arguments recursively.
 *
 * @param owningType        The owning type
 * @param owningClassWriter The declaring writer
 * @param generatorAdapter  The generator adapter to use
 * @param argumentName      The argument name
 * @param typeReference     The type name
 * @param classElement      The class element that declares the generics
 * @param typeArguments     The nested type arguments
 * @param visitedTypes      The visited types
 * @param defaults          The annotation defaults
 * @param loadTypeMethods   The load type methods
 */
protected static void buildArgumentWithGenerics(Type owningType, ClassWriter owningClassWriter, GeneratorAdapter generatorAdapter, String argumentName, Type typeReference, ClassElement classElement, Map<String, ClassElement> typeArguments, Set<String> visitedTypes, Map<String, Integer> defaults, Map<String, GeneratorAdapter> loadTypeMethods) {
    // 1st argument: the type
    generatorAdapter.push(typeReference);
    // 2nd argument: the name
    generatorAdapter.push(argumentName);
    AnnotationMetadata annotationMetadata = classElement.getAnnotationMetadata();
    boolean hasAnnotationMetadata = annotationMetadata != AnnotationMetadata.EMPTY_METADATA;
    if (!hasAnnotationMetadata && typeArguments.isEmpty()) {
        invokeInterfaceStaticMethod(generatorAdapter, Argument.class, METHOD_CREATE_ARGUMENT_SIMPLE);
        return;
    }
    // 3rd argument: annotation metadata
    if (!hasAnnotationMetadata) {
        generatorAdapter.visitInsn(ACONST_NULL);
    } else {
        AnnotationMetadataWriter.instantiateNewMetadata(owningType, owningClassWriter, generatorAdapter, (DefaultAnnotationMetadata) annotationMetadata, defaults, loadTypeMethods);
    }
    // 4th argument, more generics
    pushTypeArgumentElements(owningType, owningClassWriter, generatorAdapter, classElement.getName(), typeArguments, visitedTypes, defaults, loadTypeMethods);
    // Argument.create( .. )
    invokeInterfaceStaticMethod(generatorAdapter, Argument.class, classElement.isTypeVariable() ? METHOD_CREATE_TYPE_VAR_WITH_ANNOTATION_METADATA_GENERICS : METHOD_CREATE_ARGUMENT_WITH_ANNOTATION_METADATA_GENERICS);
}
Also used : DefaultAnnotationMetadata(io.micronaut.inject.annotation.DefaultAnnotationMetadata) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Example 2 with DefaultAnnotationMetadata

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

the class AbstractClassFileWriter method pushCreateArgument.

/**
 * Pushes a new Argument creation.
 *
 * @param declaringTypeName    The declaring type name
 * @param owningType           The owning type
 * @param declaringClassWriter The declaring class writer
 * @param generatorAdapter     The generator adapter
 * @param argumentName         The argument name
 * @param typedElement         The typed element
 * @param annotationMetadata   The annotation metadata
 * @param typeArguments        The type arguments
 * @param defaults             The annotation defaults
 * @param loadTypeMethods      The load type methods
 */
protected static void pushCreateArgument(String declaringTypeName, Type owningType, ClassWriter declaringClassWriter, GeneratorAdapter generatorAdapter, String argumentName, TypedElement typedElement, AnnotationMetadata annotationMetadata, Map<String, ClassElement> typeArguments, Map<String, Integer> defaults, Map<String, GeneratorAdapter> loadTypeMethods) {
    Type argumentType = JavaModelUtils.getTypeReference(typedElement);
    // 1st argument: The type
    generatorAdapter.push(argumentType);
    // 2nd argument: The argument name
    generatorAdapter.push(argumentName);
    boolean hasAnnotations = !annotationMetadata.isEmpty() && annotationMetadata instanceof DefaultAnnotationMetadata;
    boolean hasTypeArguments = typeArguments != null && !typeArguments.isEmpty();
    boolean isGenericPlaceholder = typedElement instanceof GenericPlaceholderElement;
    boolean isTypeVariable = isGenericPlaceholder || ((typedElement instanceof ClassElement) && ((ClassElement) typedElement).isTypeVariable());
    String variableName = argumentName;
    if (isGenericPlaceholder) {
        variableName = ((GenericPlaceholderElement) typedElement).getVariableName();
    }
    boolean hasVariableName = !variableName.equals(argumentName);
    if (!hasAnnotations && !hasTypeArguments && !isTypeVariable) {
        invokeInterfaceStaticMethod(generatorAdapter, Argument.class, METHOD_CREATE_ARGUMENT_SIMPLE);
        return;
    }
    if (isTypeVariable && hasVariableName) {
        generatorAdapter.push(variableName);
    }
    // 3rd argument: The annotation metadata
    if (hasAnnotations) {
        AnnotationMetadataWriter.instantiateNewMetadata(owningType, declaringClassWriter, generatorAdapter, (DefaultAnnotationMetadata) annotationMetadata, defaults, loadTypeMethods);
    } else {
        generatorAdapter.visitInsn(ACONST_NULL);
    }
    // 4th argument: The generic types
    if (hasTypeArguments) {
        pushTypeArgumentElements(owningType, declaringClassWriter, generatorAdapter, declaringTypeName, typeArguments, defaults, loadTypeMethods);
    } else {
        generatorAdapter.visitInsn(ACONST_NULL);
    }
    if (isTypeVariable) {
        // Argument.create( .. )
        invokeInterfaceStaticMethod(generatorAdapter, Argument.class, hasVariableName ? METHOD_CREATE_GENERIC_PLACEHOLDER_WITH_ANNOTATION_METADATA_GENERICS : METHOD_CREATE_TYPE_VAR_WITH_ANNOTATION_METADATA_GENERICS);
    } else {
        // Argument.create( .. )
        invokeInterfaceStaticMethod(generatorAdapter, Argument.class, METHOD_CREATE_ARGUMENT_WITH_ANNOTATION_METADATA_GENERICS);
    }
}
Also used : Type(org.objectweb.asm.Type) GenericPlaceholderElement(io.micronaut.inject.ast.GenericPlaceholderElement) ClassElement(io.micronaut.inject.ast.ClassElement) DefaultAnnotationMetadata(io.micronaut.inject.annotation.DefaultAnnotationMetadata)

Example 3 with DefaultAnnotationMetadata

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

the class AbstractAnnotationMetadataWriter method initializeAnnotationMetadata.

/**
 * @param staticInit  The {@link GeneratorAdapter}
 * @param classWriter The {@link ClassWriter}
 * @param defaults    The annotation defaults
 */
protected void initializeAnnotationMetadata(GeneratorAdapter staticInit, ClassWriter classWriter, Map<String, Integer> defaults) {
    Type annotationMetadataType = Type.getType(AnnotationMetadata.class);
    classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC, FIELD_ANNOTATION_METADATA, annotationMetadataType.getDescriptor(), null, null);
    if (annotationMetadata instanceof DefaultAnnotationMetadata) {
        AnnotationMetadataWriter.instantiateNewMetadata(targetClassType, classWriter, staticInit, (DefaultAnnotationMetadata) annotationMetadata, defaults, loadTypeMethods);
    } else if (annotationMetadata instanceof AnnotationMetadataHierarchy) {
        AnnotationMetadataWriter.instantiateNewMetadataHierarchy(targetClassType, classWriter, staticInit, (AnnotationMetadataHierarchy) annotationMetadata, defaults, loadTypeMethods);
    } else {
        staticInit.getStatic(Type.getType(AnnotationMetadata.class), "EMPTY_METADATA", Type.getType(AnnotationMetadata.class));
    }
    staticInit.putStatic(targetClassType, FIELD_ANNOTATION_METADATA, annotationMetadataType);
}
Also used : Type(org.objectweb.asm.Type) DefaultAnnotationMetadata(io.micronaut.inject.annotation.DefaultAnnotationMetadata) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy)

Example 4 with DefaultAnnotationMetadata

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

the class AbstractAnnotationMetadataWriter method writeAnnotationMetadataStaticInitializer.

/**
 * @param classWriter The {@link ClassWriter}
 * @param defaults    The annotation defaults
 */
protected void writeAnnotationMetadataStaticInitializer(ClassWriter classWriter, Map<String, Integer> defaults) {
    if (!(annotationMetadata instanceof AnnotationMetadataReference)) {
        // write the static initializers for the annotation metadata
        GeneratorAdapter staticInit = visitStaticInitializer(classWriter);
        staticInit.visitCode();
        if (writeAnnotationDefault && annotationMetadata instanceof DefaultAnnotationMetadata) {
            DefaultAnnotationMetadata dam = (DefaultAnnotationMetadata) annotationMetadata;
            AnnotationMetadataWriter.writeAnnotationDefaults(targetClassType, classWriter, staticInit, dam, defaults, loadTypeMethods);
        }
        staticInit.visitLabel(new Label());
        initializeAnnotationMetadata(staticInit, classWriter, defaults);
        staticInit.visitInsn(RETURN);
        staticInit.visitMaxs(1, 1);
        staticInit.visitEnd();
    }
}
Also used : Label(org.objectweb.asm.Label) AnnotationMetadataReference(io.micronaut.inject.annotation.AnnotationMetadataReference) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) DefaultAnnotationMetadata(io.micronaut.inject.annotation.DefaultAnnotationMetadata)

Aggregations

DefaultAnnotationMetadata (io.micronaut.inject.annotation.DefaultAnnotationMetadata)4 Type (org.objectweb.asm.Type)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)1 AnnotationMetadataReference (io.micronaut.inject.annotation.AnnotationMetadataReference)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 GenericPlaceholderElement (io.micronaut.inject.ast.GenericPlaceholderElement)1 Label (org.objectweb.asm.Label)1 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)1