Add KClass.sealedSubclasses to get direct subclasses of sealed class
#KT-14657 Fixed
This commit is contained in:
committed by
Ilya Gorbunov
parent
1b1713a849
commit
cbc92bc9a1
@@ -0,0 +1,41 @@
|
||||
// IGNORE_BACKEND: JS, JS_IR, JVM_IR, NATIVE
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.jvm.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
// --
|
||||
|
||||
sealed class SealedClassWithTopLevelSubclasses {
|
||||
class NotASealedSubclass : TL2()
|
||||
}
|
||||
object TL1 : SealedClassWithTopLevelSubclasses()
|
||||
open class TL2 : SealedClassWithTopLevelSubclasses()
|
||||
|
||||
// --
|
||||
|
||||
sealed class SealedClassWithNestedSubclasses {
|
||||
data class N1(val x: Unit) : SealedClassWithNestedSubclasses()
|
||||
object N2 : SealedClassWithNestedSubclasses()
|
||||
}
|
||||
|
||||
// --
|
||||
|
||||
sealed class SealedClassWithNoSubclasses
|
||||
|
||||
// --
|
||||
|
||||
fun sealedSubclassNames(c: KClass<*>) = c.sealedSubclasses.map { it.simpleName ?: throw AssertionError("Unnamed class: ${it.java}") }.sorted()
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(listOf("TL1", "TL2"), sealedSubclassNames(SealedClassWithTopLevelSubclasses::class))
|
||||
assertEquals(listOf("N1", "N2"), sealedSubclassNames(SealedClassWithNestedSubclasses::class))
|
||||
assertEquals(emptyList(), sealedSubclassNames(SealedClassWithNoSubclasses::class))
|
||||
|
||||
assertEquals(emptyList(), sealedSubclassNames(String::class))
|
||||
assertEquals(emptyList(), sealedSubclassNames(Thread::class))
|
||||
assertEquals(emptyList(), sealedSubclassNames(FloatArray::class))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+5
@@ -17844,6 +17844,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/qualifiedNameOfStandardClasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("sealedSubclasses.kt")
|
||||
public void testSealedSubclasses() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/sealedSubclasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("starProjectedType.kt")
|
||||
public void testStarProjectedType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/starProjectedType.kt");
|
||||
|
||||
+5
@@ -17844,6 +17844,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/qualifiedNameOfStandardClasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("sealedSubclasses.kt")
|
||||
public void testSealedSubclasses() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/sealedSubclasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("starProjectedType.kt")
|
||||
public void testStarProjectedType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/starProjectedType.kt");
|
||||
|
||||
+5
@@ -17844,6 +17844,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/qualifiedNameOfStandardClasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("sealedSubclasses.kt")
|
||||
public void testSealedSubclasses() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/sealedSubclasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("starProjectedType.kt")
|
||||
public void testStarProjectedType() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/classes/starProjectedType.kt");
|
||||
|
||||
Reference in New Issue
Block a user