Load definitions of symbols from .kotlin_metadata files
Extract AbstractDeserializedPackageFragmentProvider out of JvmBuiltInsPackageFragmentProvider and implement it a little bit differently in MetadataPackageFragmentProvider. The main difference is in how the package fragment scope is constructed: for built-ins, it's just a single scope that loads everything from one protobuf message. For metadata, package scope can consist of many files, some of which store information about classes and others are similar to package parts on JVM, so a ChainedMemberScope instance is created. Introduce a bunch of interfaces/methods to deliver the needed behavior to the 'deserialization' module which is not JVM-specific and does not depend on the compiler code: MetadataFinderFactory, PackagePartProvider#findMetadataPackageParts, KotlinMetadataFinder#findMetadata. Note that these declarations are currently only implemented in the compiler; no metadata package parts/fragments will be found in IDE or reflection
This commit is contained in:
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClassFinder
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragment
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
import java.io.InputStream
|
||||
|
||||
@@ -35,6 +36,12 @@ class JvmCliVirtualFileFinder(
|
||||
override fun findVirtualFileWithHeader(classId: ClassId): VirtualFile? =
|
||||
findBinaryClass(classId, classId.relativeClassName.asString().replace('.', '$') + ".class")
|
||||
|
||||
override fun findMetadata(classId: ClassId): InputStream? {
|
||||
assert(!classId.isNestedClass) { "Nested classes are not supported here: $classId" }
|
||||
|
||||
return findBinaryClass(classId, classId.shortClassName.asString() + MetadataPackageFragment.METADATA_FILE_EXTENSION)?.inputStream
|
||||
}
|
||||
|
||||
override fun findBuiltInsData(packageFqName: FqName): InputStream? {
|
||||
// "<builtins-metadata>" is just a made-up name
|
||||
// JvmDependenciesIndex requires the ClassId of the class which we're searching for, to cache the last request+result
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
import org.jetbrains.kotlin.descriptors.PackagePartProvider
|
||||
import org.jetbrains.kotlin.load.kotlin.ModuleMapping
|
||||
import org.jetbrains.kotlin.load.kotlin.PackageParts
|
||||
import java.io.EOFException
|
||||
|
||||
class JvmPackagePartProvider(
|
||||
@@ -38,11 +39,16 @@ class JvmPackagePartProvider(
|
||||
|
||||
private val loadedModules: MutableList<ModuleMapping> = SmartList()
|
||||
|
||||
@Synchronized
|
||||
override fun findPackageParts(packageFqName: String): List<String> {
|
||||
processNotLoadedRelevantRoots(packageFqName)
|
||||
override fun findPackageParts(packageFqName: String): List<String> =
|
||||
getPackageParts(packageFqName).flatMap(PackageParts::parts).distinct()
|
||||
|
||||
return loadedModules.flatMap { it.findPackageParts(packageFqName)?.parts ?: emptySet<String>() }.distinct()
|
||||
override fun findMetadataPackageParts(packageFqName: String): List<String> =
|
||||
getPackageParts(packageFqName).flatMap(PackageParts::metadataParts).distinct()
|
||||
|
||||
@Synchronized
|
||||
private fun getPackageParts(packageFqName: String): List<PackageParts> {
|
||||
processNotLoadedRelevantRoots(packageFqName)
|
||||
return loadedModules.mapNotNull { it.findPackageParts(packageFqName) }
|
||||
}
|
||||
|
||||
private fun processNotLoadedRelevantRoots(packageFqName: String) {
|
||||
|
||||
@@ -86,6 +86,7 @@ import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
|
||||
import org.jetbrains.kotlin.idea.KotlinFileType
|
||||
import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinderFactory
|
||||
import org.jetbrains.kotlin.load.kotlin.KotlinBinaryClassCache
|
||||
import org.jetbrains.kotlin.load.kotlin.MetadataFinderFactory
|
||||
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.isValidJavaFqName
|
||||
@@ -160,7 +161,9 @@ class KotlinCoreEnvironment private constructor(
|
||||
(ServiceManager.getService(project, CoreJavaFileManager::class.java)
|
||||
as KotlinCliJavaFileManagerImpl).initIndex(rootsIndex)
|
||||
|
||||
project.registerService(JvmVirtualFileFinderFactory::class.java, JvmCliVirtualFileFinderFactory(rootsIndex))
|
||||
val finderFactory = JvmCliVirtualFileFinderFactory(rootsIndex)
|
||||
project.registerService(MetadataFinderFactory::class.java, finderFactory)
|
||||
project.registerService(JvmVirtualFileFinderFactory::class.java, finderFactory)
|
||||
|
||||
ExpressionCodegenExtension.registerExtensionPoint(project)
|
||||
ClassBuilderInterceptorExtension.registerExtensionPoint(project)
|
||||
|
||||
Reference in New Issue
Block a user