JVM_IR: Maintain annotations in SAM conversion.
Copy method and parameter annotations to the generated SAM implementation method.
This commit is contained in:
+10
-3
@@ -19,9 +19,7 @@ import org.jetbrains.kotlin.descriptors.Modality
|
|||||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||||
import org.jetbrains.kotlin.ir.builders.*
|
import org.jetbrains.kotlin.ir.builders.*
|
||||||
import org.jetbrains.kotlin.ir.builders.declarations.*
|
import org.jetbrains.kotlin.ir.builders.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
|
||||||
import org.jetbrains.kotlin.ir.types.IrType
|
import org.jetbrains.kotlin.ir.types.IrType
|
||||||
@@ -204,6 +202,15 @@ class SingleAbstractMethodLowering(val context: CommonBackendContext) : ClassLow
|
|||||||
|
|
||||||
private fun IrBlockBuilder.createFunctionProxyInstance(superType: IrType, reference: IrFunctionReference): IrExpression {
|
private fun IrBlockBuilder.createFunctionProxyInstance(superType: IrType, reference: IrFunctionReference): IrExpression {
|
||||||
val implementation = createFunctionProxy(superType, reference)
|
val implementation = createFunctionProxy(superType, reference)
|
||||||
|
if (reference.origin == IrStatementOrigin.ANONYMOUS_FUNCTION || reference.origin == IrStatementOrigin.LAMBDA) {
|
||||||
|
val target = reference.symbol.owner
|
||||||
|
implementation.functions.single().apply {
|
||||||
|
annotations.addAll(target.annotations)
|
||||||
|
valueParameters.forEachIndexed { i, p ->
|
||||||
|
p.annotations.addAll(target.valueParameters[i].annotations)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
localImplementations += implementation
|
localImplementations += implementation
|
||||||
return irCall(implementation.constructors.single()).apply {
|
return irCall(implementation.constructors.single()).apply {
|
||||||
reference.arguments.filterNotNull().forEachIndexed(::putValueArgument)
|
reference.arguments.filterNotNull().forEachIndexed(::putValueArgument)
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
|
// WITH_RUNTIME
|
||||||
|
// FILE: Test.java
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
public static Class<?> apply(Runnable x) {
|
||||||
|
return x.getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static interface ABC {
|
||||||
|
void apply(String x1, String x2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class<?> applyABC(ABC x) {
|
||||||
|
return x.getClass();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FILE: test.kt
|
||||||
|
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
annotation class Ann(val x: String)
|
||||||
|
|
||||||
|
fun testMethodNoAnnotations(method: Method, name: String) {
|
||||||
|
assertEquals(0, method.getDeclaredAnnotations().size, "No method annotations expected `$name`")
|
||||||
|
for ((index, annotations) in method.getParameterAnnotations().withIndex()) {
|
||||||
|
assertEquals(0, annotations.size, "No parameter $index annotations expected `$name`")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testClass(clazz: Class<*>, name: String) {
|
||||||
|
val invokes = clazz.getDeclaredMethods().single() { !it.isBridge() }
|
||||||
|
testMethodNoAnnotations(invokes, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ann("OK")
|
||||||
|
fun annotatedABC(@Ann("OK0") x: String, @Ann("OK1") y: String) {}
|
||||||
|
|
||||||
|
@Ann("OK")
|
||||||
|
fun annotatedRunnable() {}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
testClass(Test.applyABC(::annotatedABC), "1")
|
||||||
|
testClass(Test.apply(::annotatedRunnable), "2")
|
||||||
|
val abcReference = ::annotatedABC
|
||||||
|
testClass(Test.applyABC(abcReference), "3")
|
||||||
|
val runnableReference = ::annotatedRunnable
|
||||||
|
testClass(Test.apply(runnableReference), "4")
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|||||||
+5
@@ -213,6 +213,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunExpression.kt");
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunExpression.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("samFunReference.kt")
|
||||||
|
public void testSamFunReference() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunReference.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("samLambda.kt")
|
@TestMetadata("samLambda.kt")
|
||||||
public void testSamLambda() throws Exception {
|
public void testSamLambda() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samLambda.kt");
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samLambda.kt");
|
||||||
|
|||||||
+5
@@ -213,6 +213,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
|||||||
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunExpression.kt");
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunExpression.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("samFunReference.kt")
|
||||||
|
public void testSamFunReference() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunReference.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("samLambda.kt")
|
@TestMetadata("samLambda.kt")
|
||||||
public void testSamLambda() throws Exception {
|
public void testSamLambda() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samLambda.kt");
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samLambda.kt");
|
||||||
|
|||||||
+5
@@ -213,6 +213,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
|||||||
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunExpression.kt");
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunExpression.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("samFunReference.kt")
|
||||||
|
public void testSamFunReference() throws Exception {
|
||||||
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samFunReference.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("samLambda.kt")
|
@TestMetadata("samLambda.kt")
|
||||||
public void testSamLambda() throws Exception {
|
public void testSamLambda() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samLambda.kt");
|
runTest("compiler/testData/codegen/box/annotations/annotatedLambda/samLambda.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user