JVM_IR: correct RetentionPolicy annotation generation

This commit is contained in:
Georgy Bronnikov
2019-05-29 16:19:07 +03:00
parent 4f6d0ca1d1
commit d7dacef9af
6 changed files with 86 additions and 11 deletions
@@ -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<IrGetEnumValue>()?.symbol?.owner?.name?.asString()
val kotlinRetentionPolicy = kotlinRetentionPolicyName?.let { KotlinRetention.valueOf(it) }
val javaRetentionPolicy = kotlinRetentionPolicy?.let { annotationRetentionMap[it] } ?: rpRuntime
irClass.annotations.add(
IrConstructorCallImpl.fromSymbolOwner(
@@ -0,0 +1,4 @@
package test;
@Runtime @Source
class Test {}
@@ -0,0 +1,7 @@
package test
@Retention(AnnotationRetention.RUNTIME)
annotation class Runtime
@Retention(AnnotationRetention.SOURCE)
annotation class Source
@@ -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()
}
@@ -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)
@@ -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)