Minimize dependencies of ModuleMapping
Prepare it to be moved to metadata.jvm
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.load.kotlin
|
||||
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
class BinaryModuleData(val annotations: List<ClassId>)
|
||||
/**
|
||||
* @param annotations list of module annotations, in the format: "org/foo/bar/Baz.Inner" (see [ClassId.fromString])
|
||||
*/
|
||||
class BinaryModuleData(val annotations: List<String>)
|
||||
|
||||
@@ -18,11 +18,6 @@ package org.jetbrains.kotlin.load.kotlin
|
||||
|
||||
import org.jetbrains.kotlin.metadata.deserialization.NameResolverImpl
|
||||
import org.jetbrains.kotlin.metadata.jvm.JvmModuleProtoBuf
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializationConfiguration
|
||||
import org.jetbrains.kotlin.serialization.deserialization.getClassId
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.DataInputStream
|
||||
import java.io.IOException
|
||||
@@ -48,10 +43,12 @@ class ModuleMapping private constructor(
|
||||
@JvmField
|
||||
val CORRUPTED: ModuleMapping = ModuleMapping(emptyMap(), BinaryModuleData(emptyList()), "CORRUPTED")
|
||||
|
||||
fun create(
|
||||
bytes: ByteArray?,
|
||||
debugName: String,
|
||||
configuration: DeserializationConfiguration
|
||||
fun loadModuleMapping(
|
||||
bytes: ByteArray?,
|
||||
debugName: String,
|
||||
isVersionCompatible: (IntArray) -> Boolean,
|
||||
skipMetadataVersionCheck: Boolean,
|
||||
isJvmPackageNameSupported: Boolean
|
||||
): ModuleMapping {
|
||||
if (bytes == null) {
|
||||
return EMPTY
|
||||
@@ -66,9 +63,7 @@ class ModuleMapping private constructor(
|
||||
return CORRUPTED
|
||||
}
|
||||
|
||||
val version = JvmMetadataVersion(*versionNumber)
|
||||
|
||||
if (configuration.skipMetadataVersionCheck || version.isCompatible()) {
|
||||
if (skipMetadataVersionCheck || isVersionCompatible(versionNumber)) {
|
||||
val moduleProto = JvmModuleProtoBuf.Module.parseFrom(stream) ?: return EMPTY
|
||||
val result = linkedMapOf<String, PackageParts>()
|
||||
|
||||
@@ -83,7 +78,7 @@ class ModuleMapping private constructor(
|
||||
packageParts.addPart(internalNameOf(packageFqName, partShortName), facadeInternalName)
|
||||
}
|
||||
|
||||
if (configuration.isJvmPackageNameSupported) {
|
||||
if (isJvmPackageNameSupported) {
|
||||
for ((index, partShortName) in proto.classWithJvmPackageNameShortNameList.withIndex()) {
|
||||
val packageId = proto.classWithJvmPackageNamePackageIdList.getOrNull(index)
|
||||
?: proto.classWithJvmPackageNamePackageIdList.lastOrNull()
|
||||
@@ -101,7 +96,7 @@ class ModuleMapping private constructor(
|
||||
|
||||
// TODO: read arguments of module annotations
|
||||
val nameResolver = NameResolverImpl(moduleProto.stringTable, moduleProto.qualifiedNameTable)
|
||||
val annotations = moduleProto.annotationList.map { proto -> nameResolver.getClassId(proto.id) }
|
||||
val annotations = moduleProto.annotationList.map { proto -> nameResolver.getQualifiedClassName(proto.id) }
|
||||
|
||||
return ModuleMapping(result, BinaryModuleData(annotations), debugName)
|
||||
} else {
|
||||
@@ -114,7 +109,8 @@ class ModuleMapping private constructor(
|
||||
}
|
||||
|
||||
private fun internalNameOf(packageFqName: String, className: String): String =
|
||||
JvmClassName.byFqNameWithoutInnerClasses(FqName(packageFqName).child(Name.identifier(className))).internalName
|
||||
if (packageFqName.isEmpty()) className
|
||||
else packageFqName.replace('.', '/') + "/" + className
|
||||
|
||||
class PackageParts(val packageFqName: String) {
|
||||
// JVM internal name of package part -> JVM internal name of the corresponding multifile facade (or null, if it's not a multifile part)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.load.kotlin
|
||||
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializationConfiguration
|
||||
|
||||
fun ModuleMapping.Companion.loadModuleMapping(
|
||||
bytes: ByteArray?,
|
||||
debugName: String,
|
||||
configuration: DeserializationConfiguration
|
||||
): ModuleMapping =
|
||||
loadModuleMapping(
|
||||
bytes,
|
||||
debugName,
|
||||
{ version -> JvmMetadataVersion(*version).isCompatible() },
|
||||
configuration.skipMetadataVersionCheck,
|
||||
configuration.isJvmPackageNameSupported
|
||||
)
|
||||
+3
-3
@@ -18,6 +18,7 @@ package kotlin.reflect.jvm.internal.components
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.PackagePartProvider
|
||||
import org.jetbrains.kotlin.load.kotlin.ModuleMapping
|
||||
import org.jetbrains.kotlin.load.kotlin.loadModuleMapping
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializationConfiguration
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
@@ -29,10 +30,9 @@ class RuntimePackagePartProvider(private val classLoader: ClassLoader) : Package
|
||||
val mapping = try {
|
||||
val resourcePath = "META-INF/$moduleName.${ModuleMapping.MAPPING_FILE_EXT}"
|
||||
classLoader.getResourceAsStream(resourcePath)?.use { stream ->
|
||||
ModuleMapping.create(stream.readBytes(), resourcePath, DeserializationConfiguration.Default)
|
||||
ModuleMapping.loadModuleMapping(stream.readBytes(), resourcePath, DeserializationConfiguration.Default)
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
} catch (e: Exception) {
|
||||
// TODO: do not swallow this exception?
|
||||
null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user