From 5c13e02e9c827eb526ead0ca831b2f0bf97fe4a6 Mon Sep 17 00:00:00 2001 From: Dmitry Savvinov Date: Wed, 10 Jul 2019 14:04:07 +0300 Subject: [PATCH] [Misc] Clean-up and deduplicate code in 'findClassInModule.kt' --- .../kotlin/descriptors/findClassInModule.kt | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt b/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt index 0be95d2dce3..ee39a8cc374 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt @@ -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 }