From d7dacef9aff13403db50fe132b661e47b818a9a5 Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Wed, 29 May 2019 16:19:07 +0300 Subject: [PATCH] JVM_IR: correct RetentionPolicy annotation generation --- .../AdditionalClassAnnotationLowering.kt | 19 +++++----- .../annotation/retention.java | 4 +++ .../annotation/retention.kt | 7 ++++ .../annotation/retention.txt | 13 +++++++ ...CompileJavaAgainstKotlinTestGenerated.java | 36 +++++++++++++++++++ ...CompileJavaAgainstKotlinTestGenerated.java | 18 ++++++++++ 6 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 compiler/testData/compileJavaAgainstKotlin/annotation/retention.java create mode 100644 compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt create mode 100644 compiler/testData/compileJavaAgainstKotlin/annotation/retention.txt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AdditionalClassAnnotationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AdditionalClassAnnotationLowering.kt index 6774a5cdbfd..82249e91a20 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AdditionalClassAnnotationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AdditionalClassAnnotationLowering.kt @@ -35,12 +35,10 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrEnumEntrySymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl import org.jetbrains.kotlin.ir.types.typeWith -import org.jetbrains.kotlin.ir.util.defaultType -import org.jetbrains.kotlin.ir.util.getAnnotation -import org.jetbrains.kotlin.ir.util.hasAnnotation -import org.jetbrains.kotlin.ir.util.isAnnotationClass +import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.utils.addToStdlib.safeAs internal val additionalClassAnnotationPhase = makeIrFilePhase( ::AdditionalClassAnnotationLowering, @@ -114,7 +112,7 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC private val etField = buildEnumEntry(elementTypeEnum, "FIELD") private val etLocalVariable = buildEnumEntry(elementTypeEnum, "LOCAL_VARIABLE") private val etMethod = buildEnumEntry(elementTypeEnum, "METHOD") - private val tModule = buildEnumEntry(elementTypeEnum, "MODULE") + private val etModule = buildEnumEntry(elementTypeEnum, "MODULE") private val etPackage = buildEnumEntry(elementTypeEnum, "PACKAGE") private val etParameter = buildEnumEntry(elementTypeEnum, "PARAMETER") private val etType = buildEnumEntry(elementTypeEnum, "TYPE") @@ -156,12 +154,11 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC private fun generateRetentionAnnotation(irClass: IrClass) { if (irClass.hasAnnotation(FqName("java.lang.annotation.Retention"))) return - val kotlinRetentionPolicy = irClass.getAnnotation(FqName("kotlin.annotation.Retention")) - val javaRetentionPolicy = if (kotlinRetentionPolicy is KotlinRetention) { - annotationRetentionMap[kotlinRetentionPolicy] ?: rpRuntime - } else { - rpRuntime - } + val kotlinRetentionPolicyCall = irClass.getAnnotation(FqName("kotlin.annotation.Retention")) + val kotlinRetentionPolicyName = + kotlinRetentionPolicyCall?.getValueArgument(0)?.safeAs()?.symbol?.owner?.name?.asString() + val kotlinRetentionPolicy = kotlinRetentionPolicyName?.let { KotlinRetention.valueOf(it) } + val javaRetentionPolicy = kotlinRetentionPolicy?.let { annotationRetentionMap[it] } ?: rpRuntime irClass.annotations.add( IrConstructorCallImpl.fromSymbolOwner( diff --git a/compiler/testData/compileJavaAgainstKotlin/annotation/retention.java b/compiler/testData/compileJavaAgainstKotlin/annotation/retention.java new file mode 100644 index 00000000000..62feaf69e55 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/annotation/retention.java @@ -0,0 +1,4 @@ +package test; + +@Runtime @Source +class Test {} \ No newline at end of file diff --git a/compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt b/compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt new file mode 100644 index 00000000000..c7012268df0 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt @@ -0,0 +1,7 @@ +package test + +@Retention(AnnotationRetention.RUNTIME) +annotation class Runtime + +@Retention(AnnotationRetention.SOURCE) +annotation class Source diff --git a/compiler/testData/compileJavaAgainstKotlin/annotation/retention.txt b/compiler/testData/compileJavaAgainstKotlin/annotation/retention.txt new file mode 100644 index 00000000000..8fe5aa68ee1 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/annotation/retention.txt @@ -0,0 +1,13 @@ +package test + +@kotlin.annotation.Retention(value = AnnotationRetention.RUNTIME) public final annotation class Runtime : kotlin.Annotation { + public constructor Runtime() +} + +@kotlin.annotation.Retention(value = AnnotationRetention.SOURCE) public final annotation class Source : kotlin.Annotation { + public constructor Source() +} + +@test.Runtime public/*package*/ open class Test { + public/*package*/ constructor Test() +} diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java index 4ed6b462bb3..0b9f6bf5a33 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java @@ -31,6 +31,24 @@ public class CompileJavaAgainstKotlinTestGenerated extends AbstractCompileJavaAg KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/annotation") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Annotation extends AbstractCompileJavaAgainstKotlinTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithoutJavac, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInAnnotation() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin/annotation"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("retention.kt") + public void testRetention() throws Exception { + runTest("compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt"); + } + } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/callableReference") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -656,6 +674,24 @@ public class CompileJavaAgainstKotlinTestGenerated extends AbstractCompileJavaAg KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/annotation") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Annotation extends AbstractCompileJavaAgainstKotlinTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithJavac, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInAnnotation() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin/annotation"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("retention.kt") + public void testRetention() throws Exception { + runTest("compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt"); + } + } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/callableReference") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ir/IrCompileJavaAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ir/IrCompileJavaAgainstKotlinTestGenerated.java index e23ba0f6f1f..c5ca148398a 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ir/IrCompileJavaAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/ir/IrCompileJavaAgainstKotlinTestGenerated.java @@ -29,6 +29,24 @@ public class IrCompileJavaAgainstKotlinTestGenerated extends AbstractIrCompileJa KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/annotation") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Annotation extends AbstractIrCompileJavaAgainstKotlinTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithoutJavac, TargetBackend.JVM_IR, testDataFilePath); + } + + public void testAllFilesPresentInAnnotation() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin/annotation"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); + } + + @TestMetadata("retention.kt") + public void testRetention() throws Exception { + runTest("compiler/testData/compileJavaAgainstKotlin/annotation/retention.kt"); + } + } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/callableReference") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)