org.jetbrains.kotlin.load.kotlin.ModuleMapping.kt Maven / Gradle / Ivy
/*
* 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 org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.load.java.AbiVersionUtil
import org.jetbrains.kotlin.serialization.deserialization.BinaryVersion
import org.jetbrains.kotlin.serialization.jvm.JvmPackageTable
import java.io.ByteArrayInputStream
import java.io.DataInputStream
public class ModuleMapping private constructor(val packageFqName2Parts: Map) {
fun findPackageParts(packageFqName: String): PackageParts? {
return packageFqName2Parts[packageFqName]
}
companion object {
public val MAPPING_FILE_EXT: String = "kotlin_module"
public val EMPTY: ModuleMapping = ModuleMapping(emptyMap())
fun create(proto: ByteArray? = null): ModuleMapping {
if (proto == null) {
return EMPTY
}
val inputStream = DataInputStream(ByteArrayInputStream(proto))
val size = inputStream.readInt()
val abiVersion =
if (size == 1) {
// TODO: this is a temporary workaround, drop after M13
BinaryVersion.create(0, inputStream.readInt(), 0)
}
else {
BinaryVersion.create((0..size - 1).map { inputStream.readInt() }.toIntArray())
}
if (AbiVersionUtil.isAbiVersionCompatible(abiVersion)) {
val parseFrom = JvmPackageTable.PackageTable.parseFrom(inputStream)
if (parseFrom != null) {
val packageFqNameParts = hashMapOf()
parseFrom.packagePartsList.forEach {
val packageParts = PackageParts(it.packageFqName)
packageFqNameParts.put(it.packageFqName, packageParts)
it.classNameList.forEach {
packageParts.parts.add(it)
}
}
return ModuleMapping(packageFqNameParts)
}
}
return EMPTY
}
}
}
public class PackageParts(val packageFqName: String) {
val parts = linkedSetOf()
override fun equals(other: Any?) =
other is PackageParts && other.packageFqName == packageFqName && other.parts == parts
override fun hashCode() =
packageFqName.hashCode() * 31 + parts.hashCode()
companion object {
@JvmStatic
public fun PackageParts.serialize(builder: JvmPackageTable.PackageTable.Builder) {
if (this.parts.isNotEmpty()) {
val packageParts = JvmPackageTable.PackageParts.newBuilder()
packageParts.setPackageFqName(this.packageFqName)
packageParts.addAllClassName(this.parts.sorted())
builder.addPackageParts(packageParts)
}
}
}
}