Fix reflection class lookup in default package

Do not append "." to the name of the class in the default package.
This commit is contained in:
Alexander Udalov
2022-09-29 01:33:24 +02:00
committed by Space Team
parent e290fa5f7b
commit 51ce829b96
6 changed files with 57 additions and 3 deletions
@@ -41679,6 +41679,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayKClass.kt");
}
@Test
@TestMetadata("arrayTypeInDefaultPackage.kt")
public void testArrayTypeInDefaultPackage() throws Exception {
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayTypeInDefaultPackage.kt");
}
@Test
@TestMetadata("classLiteralWithExpectedType.kt")
public void testClassLiteralWithExpectedType() throws Exception {
@@ -0,0 +1,20 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.KClass
import kotlin.test.assertEquals
import kotlin.test.assertTrue
annotation class Nested(val value: String)
@Target(AnnotationTarget.TYPE)
annotation class Anno(
val aa: Array<Nested>,
)
fun f(): @Anno([Nested("OK")]) Unit {}
fun box(): String {
val anno = ::f.returnType.annotations.single() as Anno
return anno.aa[0].value
}
@@ -40245,6 +40245,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayKClass.kt");
}
@Test
@TestMetadata("arrayTypeInDefaultPackage.kt")
public void testArrayTypeInDefaultPackage() throws Exception {
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayTypeInDefaultPackage.kt");
}
@Test
@TestMetadata("classLiteralWithExpectedType.kt")
public void testClassLiteralWithExpectedType() throws Exception {
@@ -41679,6 +41679,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayKClass.kt");
}
@Test
@TestMetadata("arrayTypeInDefaultPackage.kt")
public void testArrayTypeInDefaultPackage() throws Exception {
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayTypeInDefaultPackage.kt");
}
@Test
@TestMetadata("classLiteralWithExpectedType.kt")
public void testClassLiteralWithExpectedType() throws Exception {
@@ -32100,6 +32100,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayKClass.kt");
}
@TestMetadata("arrayTypeInDefaultPackage.kt")
public void testArrayTypeInDefaultPackage() throws Exception {
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/arrayTypeInDefaultPackage.kt");
}
@TestMetadata("classLiteralWithExpectedType.kt")
public void testClassLiteralWithExpectedType() throws Exception {
runTest("compiler/testData/codegen/box/reflection/annotations/onTypes/classLiteralWithExpectedType.kt");
@@ -97,9 +97,20 @@ private fun loadClass(classLoader: ClassLoader, packageName: String, className:
}
}
var fqName = "$packageName.${className.replace('.', '$')}"
if (arrayDimensions > 0) {
fqName = "[".repeat(arrayDimensions) + "L$fqName;"
val fqName = buildString {
if (arrayDimensions > 0) {
repeat(arrayDimensions) {
append("[")
}
append("L")
}
if (packageName.isNotEmpty()) {
append("$packageName.")
}
append(className.replace('.', '$'))
if (arrayDimensions > 0) {
append(";")
}
}
return classLoader.tryLoadClass(fqName)