AnnotationMetadataWriter.java

/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.inject.annotation;

import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationMetadataProvider;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.reflect.ReflectionUtils;
import io.micronaut.sourcegen.bytecode.ByteCodeWriter;
import io.micronaut.sourcegen.model.ClassDef;
import io.micronaut.sourcegen.model.ClassTypeDef;
import io.micronaut.sourcegen.model.ExpressionDef;
import io.micronaut.sourcegen.model.FieldDef;
import io.micronaut.sourcegen.model.MethodDef;
import io.micronaut.sourcegen.model.StatementDef;
import io.micronaut.sourcegen.model.TypeDef;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Responsible for writing class files that are instances of {@link AnnotationMetadataProvider}.
 *
 * @author Graeme Rocher
 * @author Denis Stepanov
 * @since 1.0
 */
@Internal
public class AnnotationMetadataWriter {

    private static final Method GET_ANNOTATION_METADATA_METHOD = ReflectionUtils.getRequiredMethod(AnnotationMetadataProvider.class, "getAnnotationMetadata");

    /**
     * Create a new {@link AnnotationMetadataProvider} class that is including the annotation metadata.
     *
     * @param className          The class name
     * @param annotationMetadata The metadata
     * @return The generated bytecode
     */
    public static byte[] write(String className, AnnotationMetadata annotationMetadata) {
        Map<String, MethodDef> loadTypeMethods = new LinkedHashMap<>();
        ClassTypeDef type = ClassTypeDef.of(className + AnnotationMetadata.CLASS_NAME_SUFFIX);
        Function<String, ExpressionDef> loadClassValueExpressionFn = AnnotationMetadataGenUtils.createLoadClassValueExpressionFn(type, loadTypeMethods);
        FieldDef annotationMetadataField = AnnotationMetadataGenUtils.createAnnotationMetadataFieldAndInitialize(annotationMetadata, loadClassValueExpressionFn);

        List<StatementDef> statements = new ArrayList<>();
        AnnotationMetadataGenUtils.addAnnotationDefaults(statements,  annotationMetadata, loadClassValueExpressionFn);

        ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(type.getName())
            .synthetic()
            .addSuperinterface(TypeDef.of(AnnotationMetadataProvider.class))
            .addField(annotationMetadataField)
            .addStaticInitializer(StatementDef.multi(statements))
            .addMethod(MethodDef.override(GET_ANNOTATION_METADATA_METHOD)
                .build((aThis, methodParameters) -> aThis.type().getStaticField(annotationMetadataField).returning())
            );
        loadTypeMethods.values().forEach(classDefBuilder::addMethod);
        ClassDef classDef = classDefBuilder.build();
        return new ByteCodeWriter().write(classDef);
    }
}