JVM: Allow the JvmSynthetic annotation on file classes (KT-41884)

This commit is contained in:
Steven Schäfer
2020-10-09 14:04:35 +02:00
committed by Alexander Udalov
parent 66bc142f92
commit 4e03b1e05f
6 changed files with 40 additions and 13 deletions
@@ -42,9 +42,11 @@ import org.jetbrains.org.objectweb.asm.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static org.jetbrains.kotlin.codegen.DescriptorAsmUtil.writeAnnotationData;
import static org.jetbrains.kotlin.load.java.JvmAnnotationNames.METADATA_PACKAGE_NAME_FIELD_NAME;
import static org.jetbrains.kotlin.resolve.jvm.annotations.JvmAnnotationUtilKt.JVM_SYNTHETIC_ANNOTATION_FQ_NAME;
import static org.jetbrains.org.objectweb.asm.Opcodes.*;
public class PackagePartCodegen extends MemberCodegen<KtFile> {
@@ -63,8 +65,20 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
@Override
protected void generateDeclaration() {
boolean isSynthetic = false;
List<AnnotationDescriptor> fileAnnotationDescriptors = new ArrayList<>();
for (KtAnnotationEntry annotationEntry : element.getAnnotationEntries()) {
AnnotationDescriptor annotationDescriptor = state.getBindingContext().get(BindingContext.ANNOTATION, annotationEntry);
if (annotationDescriptor != null) {
fileAnnotationDescriptors.add(annotationDescriptor);
if (Objects.equals(annotationDescriptor.getFqName(), JVM_SYNTHETIC_ANNOTATION_FQ_NAME)) {
isSynthetic = true;
}
}
}
v.defineClass(element, state.getClassFileVersion(),
ACC_PUBLIC | ACC_FINAL | ACC_SUPER,
ACC_PUBLIC | ACC_FINAL | ACC_SUPER | (isSynthetic ? ACC_SYNTHETIC : 0),
packagePartType.getInternalName(),
null,
"java/lang/Object",
@@ -74,17 +88,6 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
generatePropertyMetadataArrayFieldIfNeeded(packagePartType);
generateAnnotationsForPartClass();
}
private void generateAnnotationsForPartClass() {
List<AnnotationDescriptor> fileAnnotationDescriptors = new ArrayList<>();
for (KtAnnotationEntry annotationEntry : element.getAnnotationEntries()) {
AnnotationDescriptor annotationDescriptor = state.getBindingContext().get(BindingContext.ANNOTATION, annotationEntry);
if (annotationDescriptor != null) {
fileAnnotationDescriptors.add(annotationDescriptor);
}
}
Annotated annotatedFile = new AnnotatedImpl(Annotations.Companion.create(fileAnnotationDescriptors));
AnnotationCodegen.forClass(v.getVisitor(), this, state).genAnnotations(annotatedFile, null, null);
}