[Reflection] Support java records in kotlin reflection

^KT-47760
This commit is contained in:
Dmitriy Novozhilov
2021-07-16 10:55:43 +03:00
committed by teamcityserver
parent 8dad8fa813
commit 17fc1da719
5 changed files with 138 additions and 6 deletions
@@ -0,0 +1,37 @@
// WITH_REFLECT
// ATTACH_DEBUGGERp
// ISSUE: KT-47760
// FILE: MyRecord.java
public record MyRecord(String stringField) {}
// FILE: main.kt
import kotlin.reflect.full.*
import kotlin.reflect.KVisibility
import kotlin.reflect.jvm.isAccessible
fun box(): String {
val expectedValue = "Hello"
val obj = MyRecord(expectedValue)
// stringField() function
val function = MyRecord::class.functions.single { it.name == "stringField" }
val functionValue = function.call(obj)
if (functionValue != expectedValue) {
return "Fail: stringField() call returned $functionValue, expected $expectedValue"
}
// stringField field
val property = MyRecord::class.memberProperties.single { it.name == "stringField" }
if (property.visibility != KVisibility.PRIVATE) {
return "Fail: field stringField is not private"
}
val getter = property.getter
getter.isAccessible = true
val propertyValue = getter.call(obj)
if (propertyValue != expectedValue) {
return "Fail: stringField field returned $propertyValue, expected $expectedValue"
}
return "OK"
}
@@ -129,6 +129,12 @@ public class BlackBoxModernJdkCodegenTestGenerated extends AbstractBlackBoxCodeg
runTest("compiler/testData/codegen/boxModernJdk/testsWithJava17/compiledJavaSealedInterface.kt");
}
@Test
@TestMetadata("javaRecordsViaKotlinReflection.kt")
public void testJavaRecordsViaKotlinReflection() throws Exception {
runTest("compiler/testData/codegen/boxModernJdk/testsWithJava17/javaRecordsViaKotlinReflection.kt");
}
@Test
@TestMetadata("javaSealedClass.kt")
public void testJavaSealedClass() throws Exception {
@@ -129,6 +129,12 @@ public class IrBlackBoxModernJdkCodegenTestGenerated extends AbstractIrBlackBoxC
runTest("compiler/testData/codegen/boxModernJdk/testsWithJava17/compiledJavaSealedInterface.kt");
}
@Test
@TestMetadata("javaRecordsViaKotlinReflection.kt")
public void testJavaRecordsViaKotlinReflection() throws Exception {
runTest("compiler/testData/codegen/boxModernJdk/testsWithJava17/javaRecordsViaKotlinReflection.kt");
}
@Test
@TestMetadata("javaSealedClass.kt")
public void testJavaSealedClass() throws Exception {