Change nestedClasses logic to accept classes with nested type aliases

#KT-47650 fixed
This commit is contained in:
Ilya Muradyan
2021-07-08 01:30:10 +03:00
parent ca86b7ff7d
commit 787ce6335c
8 changed files with 77 additions and 1 deletions
@@ -35521,6 +35521,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/reflection/classes/nestedClasses.kt");
}
@Test
@TestMetadata("nestedClassesInScript.kt")
public void testNestedClassesInScript() throws Exception {
runTest("compiler/testData/codegen/box/reflection/classes/nestedClassesInScript.kt");
}
@Test
@TestMetadata("nestedClassesJava.kt")
public void testNestedClassesJava() throws Exception {
@@ -0,0 +1,22 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_LIGHT_ANALYSIS
// WITH_RUNTIME
// WITH_REFLECT
// FILE: test.kt
fun box(): String {
val kClass = Script::class
val nestedClasses = kClass.nestedClasses
val nestedClass = nestedClasses.single()
return nestedClass.simpleName!!
}
// FILE: Script.kts
class OK
typealias Tazz = List<OK>
val x: Tazz = listOf()
x
@@ -35479,6 +35479,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/reflection/classes/nestedClasses.kt");
}
@Test
@TestMetadata("nestedClassesInScript.kt")
public void testNestedClassesInScript() throws Exception {
runTest("compiler/testData/codegen/box/reflection/classes/nestedClassesInScript.kt");
}
@Test
@TestMetadata("nestedClassesJava.kt")
public void testNestedClassesJava() throws Exception {
@@ -35521,6 +35521,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/reflection/classes/nestedClasses.kt");
}
@Test
@TestMetadata("nestedClassesInScript.kt")
public void testNestedClassesInScript() throws Exception {
runTest("compiler/testData/codegen/box/reflection/classes/nestedClassesInScript.kt");
}
@Test
@TestMetadata("nestedClassesJava.kt")
public void testNestedClassesJava() throws Exception {
@@ -28052,6 +28052,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/reflection/classes/nestedClasses.kt");
}
@TestMetadata("nestedClassesInScript.kt")
public void testNestedClassesInScript() throws Exception {
runTest("compiler/testData/codegen/box/reflection/classes/nestedClassesInScript.kt");
}
@TestMetadata("nestedClassesJava.kt")
public void testNestedClassesJava() throws Exception {
runTest("compiler/testData/codegen/box/reflection/classes/nestedClassesJava.kt");
@@ -99,7 +99,7 @@ internal class KClassImpl<T : Any>(
val nestedClasses: Collection<KClass<*>> by ReflectProperties.lazySoft {
descriptor.unsubstitutedInnerClassesScope.getContributedDescriptors().filterNot(DescriptorUtils::isEnumEntry)
.mapNotNull { nestedClass ->
val jClass = (nestedClass as ClassDescriptor).toJavaClass()
val jClass = (nestedClass as? ClassDescriptor)?.toJavaClass()
jClass?.let { KClassImpl(it) }
}
}
@@ -36,6 +36,8 @@ dependencies {
testImplementation(intellijCoreDep()) { includeJars("intellij-core") }
testRuntimeOnly(intellijDep()) { includeJars("jps-model", "jna") }
testImplementation(project(":kotlin-reflect"))
}
sourceSets {
@@ -6,7 +6,9 @@
package org.jetbrains.kotlin.scripting.compiler.test
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
import kotlin.reflect.KClass
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
@@ -27,6 +29,23 @@ class ScriptCompilerTest : TestCase() {
assertTrue(res.reports.none { it.message.contains("nonsense") })
}
fun testTypeAliases() {
val res = compileToClass(
"""
class Clazz
typealias Tazz = List<Clazz>
val x: Tazz = listOf()
x
""".trimIndent().toScriptSource()
)
val kclass = res.valueOrThrow()
val nestedClasses = kclass.nestedClasses.toList()
assertEquals(1, nestedClasses.size)
assertEquals("Clazz", nestedClasses[0].simpleName)
}
fun compile(
script: SourceCode,
cfgBody: ScriptCompilationConfiguration.Builder.() -> Unit
@@ -35,4 +54,14 @@ class ScriptCompilerTest : TestCase() {
val compiler = ScriptJvmCompilerIsolated(defaultJvmScriptingHostConfiguration)
return compiler.compile(script, compilationConfiguration)
}
fun compileToClass(
script: SourceCode,
evaluationConfiguration: ScriptEvaluationConfiguration = ScriptEvaluationConfiguration(),
cfgBody: ScriptCompilationConfiguration.Builder.() -> Unit = {},
): ResultWithDiagnostics<KClass<*>> {
val result = compile(script, cfgBody)
if (result is ResultWithDiagnostics.Failure) return result
return runBlocking { result.valueOrThrow().getClass(evaluationConfiguration) }
}
}