[Misc] Clean-up and deduplicate code in 'findClassInModule.kt'

This commit is contained in:
Dmitry Savvinov
2019-07-10 14:04:07 +03:00
parent a0f339ffc7
commit 5c13e02e9c
@@ -19,17 +19,26 @@ package org.jetbrains.kotlin.descriptors
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.ClassId
fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? {
fun ModuleDescriptor.findClassifierAcrossModuleDependencies(classId: ClassId): ClassifierDescriptor? {
val packageViewDescriptor = getPackage(classId.packageFqName)
val segments = classId.relativeClassName.pathSegments()
val topLevelClass = packageViewDescriptor.memberScope.getContributedClassifier(segments.first(), NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor ?: return null
val topLevelClass = packageViewDescriptor.memberScope.getContributedClassifier(
segments.first(),
NoLookupLocation.FROM_DESERIALIZATION
) ?: return null
var result = topLevelClass
for (name in segments.subList(1, segments.size)) {
result = result.unsubstitutedInnerClassesScope.getContributedClassifier(name, NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor ?: return null
if (result !is ClassDescriptor) return null
result = result.unsubstitutedInnerClassesScope
.getContributedClassifier(name, NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor
?: return null
}
return result
}
fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? =
findClassifierAcrossModuleDependencies(classId) as? ClassDescriptor
// Returns a mock class descriptor if no existing class is found.
// NB: the returned class has no type parameters and thus cannot be given arguments in types
fun ModuleDescriptor.findNonGenericClassAcrossDependencies(classId: ClassId, notFoundClasses: NotFoundClasses): ClassDescriptor {
@@ -43,19 +52,6 @@ fun ModuleDescriptor.findNonGenericClassAcrossDependencies(classId: ClassId, not
}
fun ModuleDescriptor.findTypeAliasAcrossModuleDependencies(classId: ClassId): TypeAliasDescriptor? {
// TODO refactor with findClassAcrossModuleDependencies
// TODO what if typealias becomes a class / interface?
val packageViewDescriptor = getPackage(classId.packageFqName)
val segments = classId.relativeClassName.pathSegments()
val lastNameIndex = segments.size - 1
val topLevelClassifier = packageViewDescriptor.memberScope.getContributedClassifier(segments.first(), NoLookupLocation.FROM_DESERIALIZATION)
if (lastNameIndex == 0) return topLevelClassifier as? TypeAliasDescriptor
var currentClass = topLevelClassifier as? ClassDescriptor ?: return null
for (name in segments.subList(1, lastNameIndex)) {
currentClass = currentClass.unsubstitutedInnerClassesScope.getContributedClassifier(name, NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor ?: return null
}
val lastName = segments[lastNameIndex]
return currentClass.unsubstitutedMemberScope.getContributedClassifier(lastName, NoLookupLocation.FROM_DESERIALIZATION) as? TypeAliasDescriptor
return findClassifierAcrossModuleDependencies(classId) as? TypeAliasDescriptor
}