Search in sources :

Example 1 with GenericPlaceholderElement

use of io.micronaut.inject.ast.GenericPlaceholderElement 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 2 with GenericPlaceholderElement

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

the class AbstractClassFileWriter method buildArgument.

/**
 * Builds an argument instance.
 *
 * @param generatorAdapter The generator adapter.
 * @param argumentName     The argument name
 * @param objectType       The object type
 */
protected static void buildArgument(GeneratorAdapter generatorAdapter, String argumentName, ClassElement objectType) {
    // 1st argument: the type
    generatorAdapter.push(getTypeReference(objectType));
    // 2nd argument: the name
    generatorAdapter.push(argumentName);
    boolean isTypeVariable = objectType instanceof GenericPlaceholderElement || objectType.isTypeVariable();
    if (isTypeVariable) {
        String variableName = argumentName;
        if (objectType instanceof GenericPlaceholderElement) {
            GenericPlaceholderElement gpe = (GenericPlaceholderElement) objectType;
            variableName = gpe.getVariableName();
        }
        boolean hasVariable = !variableName.equals(argumentName);
        if (hasVariable) {
            generatorAdapter.push(variableName);
        }
        // Argument.create( .. )
        invokeInterfaceStaticMethod(generatorAdapter, Argument.class, hasVariable ? METHOD_GENERIC_PLACEHOLDER_SIMPLE : METHOD_CREATE_TYPE_VARIABLE_SIMPLE);
    } else {
        // Argument.create( .. )
        invokeInterfaceStaticMethod(generatorAdapter, Argument.class, METHOD_CREATE_ARGUMENT_SIMPLE);
    }
}
Also used : GenericPlaceholderElement(io.micronaut.inject.ast.GenericPlaceholderElement)

Aggregations

GenericPlaceholderElement (io.micronaut.inject.ast.GenericPlaceholderElement)2 DefaultAnnotationMetadata (io.micronaut.inject.annotation.DefaultAnnotationMetadata)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 Type (org.objectweb.asm.Type)1