kotlin.reflect.jvm.internal.impl.descriptors.impl.ModuleDescriptorImpl.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-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.reflect.jvm.internal.impl.descriptors.impl
import kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns
import kotlin.reflect.jvm.internal.impl.descriptors.*
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.Annotations
import kotlin.reflect.jvm.internal.impl.name.FqName
import kotlin.reflect.jvm.internal.impl.name.Name
import kotlin.reflect.jvm.internal.impl.platform.TargetPlatform
import kotlin.reflect.jvm.internal.impl.storage.StorageManager
import kotlin.reflect.jvm.internal.impl.types.checker.REFINER_CAPABILITY
import kotlin.reflect.jvm.internal.impl.types.checker.Ref
import kotlin.reflect.jvm.internal.impl.types.refinement.TypeRefinement
import kotlin.reflect.jvm.internal.impl.utils.sure
class ModuleDescriptorImpl @JvmOverloads constructor(
moduleName: Name,
private val storageManager: StorageManager,
override val builtIns: KotlinBuiltIns,
// May be null in compiler context, should be not-null in IDE context
override val platform: TargetPlatform? = null,
capabilities: Map, Any?> = emptyMap(),
override val stableName: Name? = null,
) : DeclarationDescriptorImpl(Annotations.EMPTY, moduleName), ModuleDescriptor {
private val capabilities: Map, Any?>
init {
if (!moduleName.isSpecial) {
throw IllegalArgumentException("Module name must be special: $moduleName")
}
this.capabilities = capabilities.toMutableMap()
@OptIn(TypeRefinement::class)
this.capabilities[REFINER_CAPABILITY] = Ref(null)
}
private var dependencies: ModuleDependencies? = null
private var packageFragmentProviderForModuleContent: PackageFragmentProvider? = null
val packageFragmentProviderForModuleContentWithoutDependencies: PackageFragmentProvider
get() = packageFragmentProviderForModuleContent
?: throw IllegalStateException("Module $id was not initialized by the time it's content without dependencies was queried")
override var isValid: Boolean = true
override fun assertValid() {
if (!isValid) {
throw InvalidModuleException("Accessing invalid module descriptor $this")
}
}
private val packages = storageManager.createMemoizedFunction { fqName: FqName ->
LazyPackageViewDescriptorImpl(this, fqName, storageManager)
}
@Deprecated("This method is not going to be supported. Please do not use it")
val testOnly_AllDependentModules: List
get() = this.dependencies!!.allDependencies
override val allDependencyModules: List
get() = this.dependencies.sure { "Dependencies of module $id were not set" }.allDependencies.filter { it != this }
override val expectedByModules: List
get() = this.dependencies.sure { "Dependencies of module $id were not set" }.expectedByDependencies
override fun getPackage(fqName: FqName): PackageViewDescriptor {
assertValid()
return packages(fqName)
}
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection {
assertValid()
return packageFragmentProvider.getSubPackagesOf(fqName, nameFilter)
}
private val packageFragmentProviderForWholeModuleWithDependencies by lazy {
val moduleDependencies = dependencies.sure { "Dependencies of module $id were not set before querying module content" }
val dependenciesDescriptors = moduleDependencies.allDependencies
assert(this in dependenciesDescriptors) { "Module $id is not contained in his own dependencies, this is probably a misconfiguration" }
dependenciesDescriptors.forEach { dependency ->
assert(dependency.isInitialized) {
"Dependency module ${dependency.id} was not initialized by the time contents of dependent module ${this.id} were queried"
}
}
CompositePackageFragmentProvider(dependenciesDescriptors.map {
it.packageFragmentProviderForModuleContent!!
})
}
private val isInitialized: Boolean
get() = packageFragmentProviderForModuleContent != null
fun setDependencies(dependencies: ModuleDependencies) {
assert(this.dependencies == null) { "Dependencies of $id were already set" }
this.dependencies = dependencies
}
fun setDependencies(vararg descriptors: ModuleDescriptorImpl) {
setDependencies(descriptors.toList())
}
fun setDependencies(descriptors: List) {
setDependencies(descriptors, emptySet())
}
fun setDependencies(descriptors: List, friends: Set) {
setDependencies(ModuleDependenciesImpl(descriptors, friends, emptyList()))
}
override fun shouldSeeInternalsOf(targetModule: ModuleDescriptor): Boolean {
if (this == targetModule) return true
if (targetModule in dependencies!!.modulesWhoseInternalsAreVisible) return true
if (targetModule in expectedByModules) return true
if (this in targetModule.expectedByModules) return true
return false
}
private val id: String
get() = name.toString()
/*
* Call initialize() to set module contents. Uninitialized module cannot be queried for its contents.
*/
fun initialize(providerForModuleContent: PackageFragmentProvider) {
assert(!isInitialized) { "Attempt to initialize module $id twice" }
this.packageFragmentProviderForModuleContent = providerForModuleContent
}
val packageFragmentProvider: PackageFragmentProvider
get() {
assertValid()
return packageFragmentProviderForWholeModuleWithDependencies
}
@Suppress("UNCHECKED_CAST")
override fun getCapability(capability: ModuleCapability) = capabilities[capability] as? T
}
interface ModuleDependencies {
val allDependencies: List
val modulesWhoseInternalsAreVisible: Set
val expectedByDependencies: List
}
class ModuleDependenciesImpl(
override val allDependencies: List,
override val modulesWhoseInternalsAreVisible: Set,
override val expectedByDependencies: List
) : ModuleDependencies