[Misc] Clean-up and deduplicate code in 'findClassInModule.kt'
This commit is contained in:
@@ -19,17 +19,26 @@ package org.jetbrains.kotlin.descriptors
|
|||||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.name.ClassId
|
import org.jetbrains.kotlin.name.ClassId
|
||||||
|
|
||||||
fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? {
|
fun ModuleDescriptor.findClassifierAcrossModuleDependencies(classId: ClassId): ClassifierDescriptor? {
|
||||||
val packageViewDescriptor = getPackage(classId.packageFqName)
|
val packageViewDescriptor = getPackage(classId.packageFqName)
|
||||||
val segments = classId.relativeClassName.pathSegments()
|
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
|
var result = topLevelClass
|
||||||
for (name in segments.subList(1, segments.size)) {
|
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
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? =
|
||||||
|
findClassifierAcrossModuleDependencies(classId) as? ClassDescriptor
|
||||||
|
|
||||||
// Returns a mock class descriptor if no existing class is found.
|
// 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
|
// NB: the returned class has no type parameters and thus cannot be given arguments in types
|
||||||
fun ModuleDescriptor.findNonGenericClassAcrossDependencies(classId: ClassId, notFoundClasses: NotFoundClasses): ClassDescriptor {
|
fun ModuleDescriptor.findNonGenericClassAcrossDependencies(classId: ClassId, notFoundClasses: NotFoundClasses): ClassDescriptor {
|
||||||
@@ -43,19 +52,6 @@ fun ModuleDescriptor.findNonGenericClassAcrossDependencies(classId: ClassId, not
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ModuleDescriptor.findTypeAliasAcrossModuleDependencies(classId: ClassId): TypeAliasDescriptor? {
|
fun ModuleDescriptor.findTypeAliasAcrossModuleDependencies(classId: ClassId): TypeAliasDescriptor? {
|
||||||
// TODO refactor with findClassAcrossModuleDependencies
|
|
||||||
// TODO what if typealias becomes a class / interface?
|
// TODO what if typealias becomes a class / interface?
|
||||||
|
return findClassifierAcrossModuleDependencies(classId) as? TypeAliasDescriptor
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user