kotlin.reflect.jvm.internal.impl.resolve.SealedClassInheritorsProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.reflect.jvm.internal.impl.resolve
import kotlin.reflect.jvm.internal.impl.descriptors.ClassDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.Modality
import kotlin.reflect.jvm.internal.impl.descriptors.PackageFragmentDescriptor
import kotlin.reflect.jvm.internal.impl.resolve.descriptorUtil.parents
import kotlin.reflect.jvm.internal.impl.resolve.scopes.DescriptorKindFilter
import kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
abstract class SealedClassInheritorsProvider {
/**
* This method may be called by compiler only for classes/interfaces with sealed modality
*/
abstract fun computeSealedSubclasses(
sealedClass: ClassDescriptor,
allowSealedInheritorsInDifferentFilesOfSamePackage: Boolean
): Collection
}
object CliSealedClassInheritorsProvider : SealedClassInheritorsProvider() {
// Note this is a generic and slow implementation which would work almost for any subclass of ClassDescriptor.
// Please avoid using it in new code.
// TODO: do something more clever instead at call sites of this function
override fun computeSealedSubclasses(
sealedClass: ClassDescriptor,
allowSealedInheritorsInDifferentFilesOfSamePackage: Boolean
): Collection {
if (sealedClass.modality != Modality.SEALED) return emptyList()
val result = linkedSetOf()
fun collectSubclasses(scope: MemberScope, collectNested: Boolean) {
for (descriptor in scope.getContributedDescriptors(DescriptorKindFilter.CLASSIFIERS)) {
if (descriptor !is ClassDescriptor) continue
if (DescriptorUtils.isDirectSubclass(descriptor, sealedClass)) {
result.add(descriptor)
}
if (collectNested) {
collectSubclasses(descriptor.unsubstitutedInnerClassesScope, collectNested)
}
}
}
val container = if (!allowSealedInheritorsInDifferentFilesOfSamePackage) {
sealedClass.containingDeclaration
} else {
sealedClass.parents.firstOrNull { it is PackageFragmentDescriptor }
}
if (container is PackageFragmentDescriptor) {
collectSubclasses(
container.getMemberScope(),
collectNested = allowSealedInheritorsInDifferentFilesOfSamePackage
)
}
collectSubclasses(sealedClass.unsubstitutedInnerClassesScope, collectNested = true)
return result
}
}