Add KClass.sealedSubclasses to get direct subclasses of sealed class

#KT-14657 Fixed
This commit is contained in:
Alexander Udalov
2018-08-03 13:23:53 +02:00
committed by Ilya Gorbunov
parent 1b1713a849
commit cbc92bc9a1
12 changed files with 95 additions and 0 deletions
@@ -76,6 +76,12 @@ public interface KClass<T : Any> : KDeclarationContainer, KAnnotatedElement, KCl
@SinceKotlin("1.1")
public val supertypes: List<KType>
/**
* The list of the immediate subclasses if this class is a sealed class, or an empty list otherwise.
*/
@SinceKotlin("1.3")
public val sealedSubclasses: List<KClass<out T>>
/**
* Visibility of this class, or `null` if its visibility cannot be represented in Kotlin.
*/
@@ -147,6 +147,14 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
result.compact()
}
val sealedSubclasses: List<KClass<out T>> by ReflectProperties.lazySoft {
descriptor.sealedSubclasses.mapNotNull { subclass ->
@Suppress("UNCHECKED_CAST")
val jClass = (subclass as ClassDescriptor).toJavaClass() as Class<out T>?
jClass?.let { KClassImpl(it) }
}
}
val declaredNonStaticMembers: Collection<KCallableImpl<*>>
by ReflectProperties.lazySoft { getMembers(memberScope, DECLARED) }
private val declaredStaticMembers: Collection<KCallableImpl<*>>
@@ -239,6 +247,11 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
override val supertypes: List<KType> get() = data().supertypes
/**
* The list of the immediate subclasses if this class is a sealed class, or an empty list otherwise.
*/
override val sealedSubclasses: List<KClass<out T>> get() = data().sealedSubclasses
override val visibility: KVisibility?
get() = descriptor.visibility.toKVisibility()