package io.micronaut.inject.writer;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.inject.annotation.AnnotationMetadataHierarchy;
import io.micronaut.core.annotation.Internal;
import io.micronaut.inject.annotation.AnnotationMetadataReference;
import io.micronaut.inject.annotation.AnnotationMetadataWriter;
import io.micronaut.inject.annotation.DefaultAnnotationMetadata;
import io.micronaut.inject.ast.Element;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.HashMap;
import java.util.Map;
@Internal
public abstract class AbstractAnnotationMetadataWriter extends AbstractClassFileWriter {
public static final String FIELD_ANNOTATION_METADATA = "$ANNOTATION_METADATA";
protected final Type targetClassType;
protected final AnnotationMetadata annotationMetadata;
protected final Map<String, GeneratorAdapter> loadTypeMethods = new HashMap<>();
private final boolean writeAnnotationDefault;
protected AbstractAnnotationMetadataWriter(
String className,
OriginatingElements originatingElements,
AnnotationMetadata annotationMetadata,
boolean writeAnnotationDefaults) {
super(originatingElements);
this.targetClassType = getTypeReference(className);
this.annotationMetadata = annotationMetadata;
this.writeAnnotationDefault = writeAnnotationDefaults;
}
protected AbstractAnnotationMetadataWriter(
String className,
Element originatingElement,
AnnotationMetadata annotationMetadata,
boolean writeAnnotationDefaults) {
super(new Element[]{ originatingElement });
this.targetClassType = getTypeReference(className);
this.annotationMetadata = annotationMetadata;
this.writeAnnotationDefault = writeAnnotationDefaults;
}
protected void writeGetAnnotationMetadataMethod(ClassWriter classWriter) {
GeneratorAdapter annotationMetadataMethod = beginAnnotationMetadataMethod(classWriter);
annotationMetadataMethod.loadThis();
if (annotationMetadata == AnnotationMetadata.EMPTY_METADATA) {
annotationMetadataMethod.getStatic(Type.getType(AnnotationMetadata.class), "EMPTY_METADATA", Type.getType(AnnotationMetadata.class));
} else if (annotationMetadata instanceof AnnotationMetadataReference) {
AnnotationMetadataReference reference = (AnnotationMetadataReference) annotationMetadata;
String className = reference.getClassName();
annotationMetadataMethod.getStatic(getTypeReference(className), AbstractAnnotationMetadataWriter.FIELD_ANNOTATION_METADATA, Type.getType(AnnotationMetadata.class));
} else {
annotationMetadataMethod.getStatic(targetClassType, AbstractAnnotationMetadataWriter.FIELD_ANNOTATION_METADATA, Type.getType(AnnotationMetadata.class));
}
annotationMetadataMethod.returnValue();
annotationMetadataMethod.visitMaxs(1, 1);
annotationMetadataMethod.visitEnd();
}
protected @NonNull
GeneratorAdapter beginAnnotationMetadataMethod(ClassWriter classWriter) {
return startPublicMethod(classWriter, "getAnnotationMetadata", AnnotationMetadata.class.getName());
}
protected void writeAnnotationMetadataStaticInitializer(ClassWriter classWriter) {
if (!(annotationMetadata instanceof AnnotationMetadataReference)) {
GeneratorAdapter staticInit = visitStaticInitializer(classWriter);
staticInit.visitCode();
staticInit.visitLabel(new Label());
initializeAnnotationMetadata(staticInit, classWriter);
if (writeAnnotationDefault && annotationMetadata instanceof DefaultAnnotationMetadata) {
DefaultAnnotationMetadata dam = (DefaultAnnotationMetadata) annotationMetadata;
AnnotationMetadataWriter.writeAnnotationDefaults(
targetClassType,
classWriter,
staticInit,
dam,
loadTypeMethods
);
}
staticInit.visitInsn(RETURN);
staticInit.visitMaxs(1, 1);
staticInit.visitEnd();
}
}
protected void initializeAnnotationMetadata(GeneratorAdapter staticInit, ClassWriter classWriter) {
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,
loadTypeMethods
);
} else if (annotationMetadata instanceof AnnotationMetadataHierarchy) {
AnnotationMetadataWriter.instantiateNewMetadataHierarchy(
targetClassType,
classWriter,
staticInit,
(AnnotationMetadataHierarchy) annotationMetadata,
loadTypeMethods
);
} else {
staticInit.getStatic(Type.getType(AnnotationMetadata.class), "EMPTY_METADATA", Type.getType(AnnotationMetadata.class));
}
staticInit.putStatic(targetClassType, FIELD_ANNOTATION_METADATA, annotationMetadataType);
}
}