From 0dd3abba2af82be50e50b6054c05d664f65541bf Mon Sep 17 00:00:00 2001 From: strangepleasures Date: Mon, 15 Jan 2024 10:24:38 +0000 Subject: [PATCH] [KAPT] Support type annotations in K2 KAPT. Fixes KT-64479 --- .../converter/typeAnnotations.fir.txt | 86 +++++++++++++++++++ .../testData/converter/typeAnnotations.kt | 10 +++ .../testData/converter/typeAnnotations.txt | 85 ++++++++++++++++++ ...ileToSourceStubConverterTestGenerated.java | 6 ++ .../jetbrains/kotlin/kapt4/StubGenerator.kt | 11 +-- .../KotlinKapt4ContextTestGenerated.java | 6 ++ 6 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.fir.txt create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.kt create mode 100644 plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.txt diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.fir.txt b/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.fir.txt new file mode 100644 index 00000000000..4d94266a550 --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.fir.txt @@ -0,0 +1,86 @@ +package org.jetbrains; + +/** + * public final annotation class org/jetbrains/A : kotlin/Annotation { + * + * // signature: ()V + * public constructor() + * + * // module name: main + * } + */ +@kotlin.Metadata() +@kotlin.annotation.Target(allowedTargets = {kotlin.annotation.AnnotationTarget.TYPE, kotlin.annotation.AnnotationTarget.TYPE_PARAMETER}) +@java.lang.annotation.Target(value = {java.lang.annotation.ElementType.TYPE_USE, java.lang.annotation.ElementType.TYPE_PARAMETER}) +@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME) +public abstract @interface A { +} + +//////////////////// + +package org.jetbrains; + +/** + * public final class org/jetbrains/Outer : kotlin/Any { + * + * // signature: ()V + * public constructor() + * + * // nested class: Inner + * + * // module name: main + * } + */ +@kotlin.Metadata() +public final class Outer { + + public Outer() { + super(); + } + + /** + * public final class org/jetbrains/Outer.Inner : kotlin/Any { + * + * // signature: ()V + * public constructor() + * + * // module name: main + * } + */ + @kotlin.Metadata() + public static final class Inner { + + public Inner() { + super(); + } + } +} + +//////////////////// + +package org.jetbrains; + +/** + * package { + * + * // signature: foo(Ljava/util/List;Lorg/jetbrains/Outer$Inner;I)Ljava/lang/String; + * public final fun foo(x: @org/jetbrains/A kotlin/collections/List<@org/jetbrains/A kotlin/String>, y: @org/jetbrains/A org/jetbrains/Outer.Inner, z: @org/jetbrains/A kotlin/Int): @org/jetbrains/A kotlin/String + * + * // module name: main + * } + */ +@kotlin.Metadata() +public final class TypeAnnotationsKt { + + public TypeAnnotationsKt() { + super(); + } + + @org.jetbrains.annotations.NotNull() + public static final java.lang.@org.jetbrains.A() String foo(@org.jetbrains.annotations.NotNull() + java.util.@org.jetbrains.A() List x, @org.jetbrains.annotations.NotNull() + org.jetbrains.Outer.@org.jetbrains.A() Inner y, @org.jetbrains.A() + int z) { + return null; + } +} diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.kt b/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.kt new file mode 100644 index 00000000000..4a3d7f4e30e --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.kt @@ -0,0 +1,10 @@ +package org.jetbrains + +@Target(allowedTargets = [AnnotationTarget.TYPE, AnnotationTarget.TYPE_PARAMETER]) +annotation class A + +class Outer { + class Inner +} + +fun foo(x: @A List<@A String>, y: @A Outer.Inner, z: @A Int): @A String = "" \ No newline at end of file diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.txt b/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.txt new file mode 100644 index 00000000000..643b2e1d02d --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.txt @@ -0,0 +1,85 @@ +package org.jetbrains; + +@kotlin.annotation.Target(allowedTargets = {kotlin.annotation.AnnotationTarget.TYPE, kotlin.annotation.AnnotationTarget.TYPE_PARAMETER}) +@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME) +@java.lang.annotation.Target(value = {}) +/** + * public final annotation class org/jetbrains/A : kotlin/Annotation { + * + * // signature: ()V + * public constructor() + * + * // module name: main + * } + */ +@kotlin.Metadata() +public abstract @interface A { +} + +//////////////////// + +package org.jetbrains; + +/** + * public final class org/jetbrains/Outer : kotlin/Any { + * + * // signature: ()V + * public constructor() + * + * // nested class: Inner + * + * // module name: main + * } + */ +@kotlin.Metadata() +public final class Outer { + + public Outer() { + super(); + } + + /** + * public final class org/jetbrains/Outer.Inner : kotlin/Any { + * + * // signature: ()V + * public constructor() + * + * // module name: main + * } + */ + @kotlin.Metadata() + public static final class Inner { + + public Inner() { + super(); + } + } +} + +//////////////////// + +package org.jetbrains; + +/** + * package { + * + * // signature: foo(Ljava/util/List;Lorg/jetbrains/Outer$Inner;I)Ljava/lang/String; + * public final fun foo(x: @org/jetbrains/A kotlin/collections/List<@org/jetbrains/A kotlin/String>, y: @org/jetbrains/A org/jetbrains/Outer.Inner, z: @org/jetbrains/A kotlin/Int): @org/jetbrains/A kotlin/String + * + * // module name: main + * } + */ +@kotlin.Metadata() +public final class TypeAnnotationsKt { + + public TypeAnnotationsKt() { + super(); + } + + @org.jetbrains.annotations.NotNull() + public static final java.lang.String foo(@org.jetbrains.annotations.NotNull() + java.util.List x, @org.jetbrains.annotations.NotNull() + org.jetbrains.Outer.Inner y, int z) { + return null; + } +} diff --git a/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java b/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java index f8800b07185..20815bcd9c3 100644 --- a/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java +++ b/plugins/kapt3/kapt3-compiler/tests-gen/org/jetbrains/kotlin/kapt3/test/runners/IrClassFileToSourceStubConverterTestGenerated.java @@ -715,6 +715,12 @@ public class IrClassFileToSourceStubConverterTestGenerated extends AbstractIrCla runTest("plugins/kapt3/kapt3-compiler/testData/converter/topLevel.kt"); } + @Test + @TestMetadata("typeAnnotations.kt") + public void testTypeAnnotations() throws Exception { + runTest("plugins/kapt3/kapt3-compiler/testData/converter/typeAnnotations.kt"); + } + @Test @TestMetadata("unresolvedDelegateExpression.kt") public void testUnresolvedDelegateExpression() throws Exception { diff --git a/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/StubGenerator.kt b/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/StubGenerator.kt index 81359ff91f4..f017d3ba53d 100644 --- a/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/StubGenerator.kt +++ b/plugins/kapt4/src/org/jetbrains/kotlin/kapt4/StubGenerator.kt @@ -392,7 +392,7 @@ private class StubGenerator( print(method.name, "(") method.parameterList.parameters.forEachIndexed{ index, parameter -> if (index > 0) print(", ") - printTypeSignature(parameter.type) + printTypeSignature(parameter.type, false) } print(")") } @@ -430,14 +430,11 @@ private class StubGenerator( private fun Printer.printType(type: PsiType) { recordErrorTypes(type) - type.annotations.forEach { - printAnnotation(it, false) - } - printTypeSignature(type) + printTypeSignature(type, true) } - private fun Printer.printTypeSignature(type: PsiType) { - printWithNoIndent((if (type is PsiClassType && isErroneous(type)) type.rawType() else type).canonicalText.replace('$', '.')) + private fun Printer.printTypeSignature(type: PsiType, annotated: Boolean) { + printWithNoIndent((if (type is PsiClassType && isErroneous(type)) type.rawType() else type).getCanonicalText(annotated).replace('$', '.')) } private fun Printer.printTypeParams(typeParameters: Array) { diff --git a/plugins/kapt4/tests-gen/org/jetbrains/kotlin/kapt4/KotlinKapt4ContextTestGenerated.java b/plugins/kapt4/tests-gen/org/jetbrains/kotlin/kapt4/KotlinKapt4ContextTestGenerated.java index 63c7e441c8f..68c1896b4f0 100644 --- a/plugins/kapt4/tests-gen/org/jetbrains/kotlin/kapt4/KotlinKapt4ContextTestGenerated.java +++ b/plugins/kapt4/tests-gen/org/jetbrains/kotlin/kapt4/KotlinKapt4ContextTestGenerated.java @@ -715,6 +715,12 @@ public class KotlinKapt4ContextTestGenerated extends AbstractKotlinKapt4ContextT runTest("plugins/kapt4/../kapt3/kapt3-compiler/testData/converter/topLevel.kt"); } + @Test + @TestMetadata("typeAnnotations.kt") + public void testTypeAnnotations() throws Exception { + runTest("plugins/kapt4/../kapt3/kapt3-compiler/testData/converter/typeAnnotations.kt"); + } + @Test @TestMetadata("unresolvedDelegateExpression.kt") public void testUnresolvedDelegateExpression() throws Exception {