Determine automatically which types from java.lang is aliased in default imported kotlin packages and exclude them from imported java.lang.* scope.

This commit is contained in:
Ilya Gorbunov
2016-09-22 21:51:49 +03:00
parent 45d42bdff8
commit 5042158df5
6 changed files with 45 additions and 25 deletions
@@ -62,16 +62,4 @@ private val DEFAULT_IMPORTS_FOR_JVM: List<ImportPath> = ArrayList<ImportPath>().
}
}
private val EXCLUDED_IMPORTS_FOR_JVM: List<FqName> = listOf(
"Error",
"Exception",
"RuntimeException",
"IllegalArgumentException",
"IllegalStateException",
"IndexOutOfBoundsException",
"UnsupportedOperationException",
"NumberFormatException",
"NullPointerException",
"ClassCastException",
"AssertionError"
).map { FqName("java.lang.$it") }
private val EXCLUDED_IMPORTS_FOR_JVM: List<FqName> = emptyList()
@@ -95,7 +95,7 @@ class FileScopeFactory(
val allUnderImportResolver = createImportResolver(AllUnderImportsIndexed(imports), bindingTrace) // TODO: should we count excludedImports here also?
val defaultExplicitImportResolver = createImportResolver(ExplicitImportsIndexed(defaultImportsFiltered), tempTrace)
val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace, moduleDescriptor.excludedImports)
val defaultAllUnderImportResolver = createImportResolver(AllUnderImportsIndexed(defaultImportsFiltered), tempTrace, moduleDescriptor.effectivelyExcludedImports)
val dummyContainerDescriptor = DummyContainerDescriptor(file, packageFragment)
@@ -44,6 +44,8 @@ interface ModuleDescriptor : DeclarationDescriptor, ModuleParameters {
fun <T> getCapability(capability: Capability<T>): T?
class Capability<T>(val name: String)
val effectivelyExcludedImports: List<FqName>
}
interface ModuleParameters {
@@ -17,14 +17,16 @@
package org.jetbrains.kotlin.descriptors.impl
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.ModuleParameters
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.isSubpackageOf
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.utils.addToStdlib.check
import org.jetbrains.kotlin.utils.sure
import java.lang.IllegalArgumentException
@@ -48,6 +50,22 @@ class ModuleDescriptorImpl @JvmOverloads constructor(
fqName: FqName -> LazyPackageViewDescriptorImpl(this, fqName, storageManager)
}
override val effectivelyExcludedImports: List<FqName> by storageManager.createLazyValue {
val packagesWithAliases = listOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME, KotlinBuiltIns.COLLECTIONS_PACKAGE_FQ_NAME)
val dependencies = this.dependencies.sure { "Dependencies of module $id were not set" }
val builtinTypeAliases = dependencies.allDependencies.filter { it != this }.flatMap {
System.out.flush()
packagesWithAliases.map(it::getPackage).flatMap {
it.memberScope.getContributedDescriptors(DescriptorKindFilter.TYPE_ALIASES).filterIsInstance<TypeAliasDescriptor>()
}
}
val nonKotlinDefaultImportedPackages = defaultImports.filter { it.isAllUnder }.mapNotNull { it.fqnPart().check { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) } }
val nonKotlinAliasedTypeFqNames = builtinTypeAliases.mapNotNull { it.expandedType.constructor.declarationDescriptor?.fqNameSafe }.filter { nonKotlinDefaultImportedPackages.any(it::isSubpackageOf) }
excludedImports + nonKotlinAliasedTypeFqNames
}
override fun getPackage(fqName: FqName): PackageViewDescriptor = packages(fqName)
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName> {
@@ -114,6 +114,7 @@ class DescriptorKindFilter(
private fun DeclarationDescriptor.kind(): Int {
return when (this) {
is ClassDescriptor -> if (this.kind.isSingleton) SINGLETON_CLASSIFIERS_MASK else NON_SINGLETON_CLASSIFIERS_MASK
is TypeAliasDescriptor -> TYPE_ALIASES_MASK
is ClassifierDescriptor -> NON_SINGLETON_CLASSIFIERS_MASK
is PackageFragmentDescriptor, is PackageViewDescriptor -> PACKAGES_MASK
is FunctionDescriptor -> FUNCTIONS_MASK
@@ -123,14 +124,18 @@ class DescriptorKindFilter(
}
companion object {
val NON_SINGLETON_CLASSIFIERS_MASK: Int = 0x01
val SINGLETON_CLASSIFIERS_MASK: Int = 0x02
val PACKAGES_MASK: Int = 0x04
val FUNCTIONS_MASK: Int = 0x08
val VARIABLES_MASK: Int = 0x10
private var nextMaskValue: Int = 0x01
private fun nextMask() = nextMaskValue.apply { nextMaskValue = nextMaskValue shl 1 }
val ALL_KINDS_MASK: Int = 0x1F
val CLASSIFIERS_MASK: Int = NON_SINGLETON_CLASSIFIERS_MASK or SINGLETON_CLASSIFIERS_MASK
val NON_SINGLETON_CLASSIFIERS_MASK: Int = nextMask()
val SINGLETON_CLASSIFIERS_MASK: Int = nextMask()
val TYPE_ALIASES_MASK: Int = nextMask()
val PACKAGES_MASK: Int = nextMask()
val FUNCTIONS_MASK: Int = nextMask()
val VARIABLES_MASK: Int = nextMask()
val ALL_KINDS_MASK: Int = nextMask() - 1
val CLASSIFIERS_MASK: Int = NON_SINGLETON_CLASSIFIERS_MASK or SINGLETON_CLASSIFIERS_MASK or TYPE_ALIASES_MASK
val VALUES_MASK: Int = SINGLETON_CLASSIFIERS_MASK or FUNCTIONS_MASK or VARIABLES_MASK
val CALLABLES_MASK: Int = FUNCTIONS_MASK or VARIABLES_MASK
@@ -138,6 +143,7 @@ class DescriptorKindFilter(
@JvmField val CALLABLES: DescriptorKindFilter = DescriptorKindFilter(CALLABLES_MASK)
@JvmField val NON_SINGLETON_CLASSIFIERS: DescriptorKindFilter = DescriptorKindFilter(NON_SINGLETON_CLASSIFIERS_MASK)
@JvmField val SINGLETON_CLASSIFIERS: DescriptorKindFilter = DescriptorKindFilter(SINGLETON_CLASSIFIERS_MASK)
@JvmField val TYPE_ALIASES: DescriptorKindFilter = DescriptorKindFilter(TYPE_ALIASES_MASK)
@JvmField val CLASSIFIERS: DescriptorKindFilter = DescriptorKindFilter(CLASSIFIERS_MASK)
@JvmField val PACKAGES: DescriptorKindFilter = DescriptorKindFilter(PACKAGES_MASK)
@JvmField val FUNCTIONS: DescriptorKindFilter = DescriptorKindFilter(FUNCTIONS_MASK)
@@ -68,6 +68,12 @@ public class ErrorUtils {
return emptyList();
}
@NotNull
@Override
public List<FqName> getEffectivelyExcludedImports() {
return emptyList();
}
@NotNull
@Override
public Annotations getAnnotations() {