Reformat modules 'metadata' and 'metadata.jvm', fix some inspections

This commit is contained in:
Alexander Udalov
2018-08-16 12:34:10 +02:00
parent 3345dc81fd
commit ddb398645b
8 changed files with 74 additions and 82 deletions
@@ -21,9 +21,9 @@ sealed class JvmMemberSignature {
}
data class Field(override val name: String, override val desc: String) : JvmMemberSignature() {
override fun asString() = name + ":" + desc
override fun asString() = "$name:$desc"
}
final override fun toString() = asString()
abstract fun asString(): String
}
}
@@ -76,36 +76,36 @@ class JvmNameResolver(
companion object {
val PREDEFINED_STRINGS = listOf(
"kotlin/Any",
"kotlin/Nothing",
"kotlin/Unit",
"kotlin/Throwable",
"kotlin/Number",
"kotlin/Any",
"kotlin/Nothing",
"kotlin/Unit",
"kotlin/Throwable",
"kotlin/Number",
"kotlin/Byte", "kotlin/Double", "kotlin/Float", "kotlin/Int",
"kotlin/Long", "kotlin/Short", "kotlin/Boolean", "kotlin/Char",
"kotlin/Byte", "kotlin/Double", "kotlin/Float", "kotlin/Int",
"kotlin/Long", "kotlin/Short", "kotlin/Boolean", "kotlin/Char",
"kotlin/CharSequence",
"kotlin/String",
"kotlin/Comparable",
"kotlin/Enum",
"kotlin/CharSequence",
"kotlin/String",
"kotlin/Comparable",
"kotlin/Enum",
"kotlin/Array",
"kotlin/ByteArray", "kotlin/DoubleArray", "kotlin/FloatArray", "kotlin/IntArray",
"kotlin/LongArray", "kotlin/ShortArray", "kotlin/BooleanArray", "kotlin/CharArray",
"kotlin/Array",
"kotlin/ByteArray", "kotlin/DoubleArray", "kotlin/FloatArray", "kotlin/IntArray",
"kotlin/LongArray", "kotlin/ShortArray", "kotlin/BooleanArray", "kotlin/CharArray",
"kotlin/Cloneable",
"kotlin/Annotation",
"kotlin/Cloneable",
"kotlin/Annotation",
"kotlin/collections/Iterable", "kotlin/collections/MutableIterable",
"kotlin/collections/Collection", "kotlin/collections/MutableCollection",
"kotlin/collections/List", "kotlin/collections/MutableList",
"kotlin/collections/Set", "kotlin/collections/MutableSet",
"kotlin/collections/Map", "kotlin/collections/MutableMap",
"kotlin/collections/Map.Entry", "kotlin/collections/MutableMap.MutableEntry",
"kotlin/collections/Iterable", "kotlin/collections/MutableIterable",
"kotlin/collections/Collection", "kotlin/collections/MutableCollection",
"kotlin/collections/List", "kotlin/collections/MutableList",
"kotlin/collections/Set", "kotlin/collections/MutableSet",
"kotlin/collections/Map", "kotlin/collections/MutableMap",
"kotlin/collections/Map.Entry", "kotlin/collections/MutableMap.MutableEntry",
"kotlin/collections/Iterator", "kotlin/collections/MutableIterator",
"kotlin/collections/ListIterator", "kotlin/collections/MutableListIterator"
"kotlin/collections/Iterator", "kotlin/collections/MutableIterator",
"kotlin/collections/ListIterator", "kotlin/collections/MutableListIterator"
)
private val PREDEFINED_STRINGS_MAP = PREDEFINED_STRINGS.withIndex().associateBy({ it.value }, { it.index })
@@ -61,16 +61,15 @@ object JvmProtoBufUtil {
// returns JVM signature in the format: "equals(Ljava/lang/Object;)Z"
fun getJvmMethodSignature(
proto: ProtoBuf.Function,
nameResolver: NameResolver,
typeTable: TypeTable
proto: ProtoBuf.Function,
nameResolver: NameResolver,
typeTable: TypeTable
): JvmMemberSignature.Method? {
val signature = proto.getExtensionOrNull(JvmProtoBuf.methodSignature)
val name = if (signature != null && signature.hasName()) signature.name else proto.name
val desc = if (signature != null && signature.hasDesc()) {
nameResolver.getString(signature.desc)
}
else {
} else {
val parameterTypes = listOfNotNull(proto.receiverType(typeTable)) + proto.valueParameterList.map { it.type(typeTable) }
val parametersDesc = parameterTypes.map { mapTypeDefault(it, nameResolver) ?: return null }
@@ -82,15 +81,14 @@ object JvmProtoBufUtil {
}
fun getJvmConstructorSignature(
proto: ProtoBuf.Constructor,
nameResolver: NameResolver,
typeTable: TypeTable
proto: ProtoBuf.Constructor,
nameResolver: NameResolver,
typeTable: TypeTable
): JvmMemberSignature.Method? {
val signature = proto.getExtensionOrNull(JvmProtoBuf.constructorSignature)
val desc = if (signature != null && signature.hasDesc()) {
nameResolver.getString(signature.desc)
}
else {
} else {
proto.valueParameterList.map {
mapTypeDefault(it.type(typeTable), nameResolver) ?: return null
}.joinToString(separator = "", prefix = "(", postfix = ")V")
@@ -99,9 +97,9 @@ object JvmProtoBufUtil {
}
fun getJvmFieldSignature(
proto: ProtoBuf.Property,
nameResolver: NameResolver,
typeTable: TypeTable
proto: ProtoBuf.Property,
nameResolver: NameResolver,
typeTable: TypeTable
): JvmMemberSignature.Field? {
val signature = proto.getExtensionOrNull(JvmProtoBuf.propertySignature) ?: return null
val field = if (signature.hasField()) signature.field else return null
@@ -21,8 +21,7 @@ class ModuleMapping private constructor(
override fun toString() = debugName
companion object {
@JvmField
val MAPPING_FILE_EXT: String = "kotlin_module"
const val MAPPING_FILE_EXT: String = "kotlin_module"
@JvmField
val EMPTY: ModuleMapping = ModuleMapping(emptyMap(), BinaryModuleData(emptyList()), "EMPTY")
@@ -44,8 +43,7 @@ class ModuleMapping private constructor(
val versionNumber = try {
IntArray(stream.readInt()) { stream.readInt() }
}
catch (e: IOException) {
} catch (e: IOException) {
return CORRUPTED
}
@@ -67,8 +65,8 @@ class ModuleMapping private constructor(
if (isJvmPackageNameSupported) {
for ((index, partShortName) in proto.classWithJvmPackageNameShortNameList.withIndex()) {
val packageId = proto.classWithJvmPackageNamePackageIdList.getOrNull(index)
?: proto.classWithJvmPackageNamePackageIdList.lastOrNull()
?: continue
?: proto.classWithJvmPackageNamePackageIdList.lastOrNull()
?: continue
val jvmPackageName = moduleProto.jvmPackageNameList.getOrNull(packageId) ?: continue
packageParts.addPart(internalNameOf(jvmPackageName, partShortName), null)
}
@@ -161,8 +159,8 @@ class PackageParts(val packageFqName: String) {
// Writes information about package parts which have a different JVM package from the Kotlin package (with the help of @JvmPackageName)
private fun JvmModuleProtoBuf.PackageParts.Builder.writePartsOutsidePackage(
parts: List<String>,
packageTableBuilder: JvmModuleProtoBuf.Module.Builder
parts: List<String>,
packageTableBuilder: JvmModuleProtoBuf.Module.Builder
) {
val packageIds = mutableListOf<Int>()
for ((packageInternalName, partsInPackage) in parts.groupBy { it.packageName }.toSortedMap()) {
@@ -198,14 +196,14 @@ class PackageParts(val packageFqName: String) {
}
override fun equals(other: Any?) =
other is PackageParts &&
other.packageFqName == packageFqName && other.packageParts == packageParts && other.metadataParts == metadataParts
other is PackageParts &&
other.packageFqName == packageFqName && other.packageParts == packageParts && other.metadataParts == metadataParts
override fun hashCode() =
(packageFqName.hashCode() * 31 + packageParts.hashCode()) * 31 + metadataParts.hashCode()
(packageFqName.hashCode() * 31 + packageParts.hashCode()) * 31 + metadataParts.hashCode()
override fun toString() =
(parts + metadataParts).toString()
(parts + metadataParts).toString()
}
fun JvmModuleProtoBuf.Module.serializeToByteArray(versionArray: IntArray): ByteArray {
@@ -35,10 +35,9 @@ fun bytesToStrings(bytes: ByteArray): Array<String> {
for (b in bytes) {
val c = b.toInt() and 0xFF // 0 <= c <= 255
buffer.append(c.toChar())
if (0 < b && b <= 127) {
if (b in 1..127) {
bytesInBuffer++
}
else {
} else {
bytesInBuffer += 2
}
@@ -30,17 +30,16 @@ open class JvmStringTable(nameResolver: JvmNameResolver? = null) : StringTable {
}
override fun getStringIndex(string: String): Int =
map.getOrPut(string) {
strings.size.apply {
strings.add(string)
map.getOrPut(string) {
strings.size.apply {
strings.add(string)
val lastRecord = records.lastOrNull()
if (lastRecord != null && lastRecord.isTrivial()) {
lastRecord.range = lastRecord.range + 1
}
else records.add(Record.newBuilder())
}
val lastRecord = records.lastOrNull()
if (lastRecord != null && lastRecord.isTrivial()) {
lastRecord.range = lastRecord.range + 1
} else records.add(Record.newBuilder())
}
}
private fun Record.Builder.isTrivial(): Boolean {
return !hasPredefinedIndex() && !hasOperation() && substringIndexCount == 0 && replaceCharCount == 0
@@ -73,15 +72,13 @@ open class JvmStringTable(nameResolver: JvmNameResolver? = null) : StringTable {
// If the class is local or any of its outer class names contains '$', store a literal string
if (isLocal || '$' in className) {
strings.add(className)
}
else {
} else {
val predefinedIndex = JvmNameResolver.getPredefinedStringIndex(className)
if (predefinedIndex != null) {
record.predefinedIndex = predefinedIndex
// TODO: move all records with predefined names to the end and do not write associated strings for them (since they are ignored)
strings.add("")
}
else {
} else {
record.operation = Record.Operation.DESC_TO_CLASS_ID
strings.add("L${className.replace('.', '$')};")
}
@@ -26,11 +26,9 @@ class TypeTable(typeTable: ProtoBuf.TypeTable) {
typeTable.typeList.mapIndexed { i, type ->
if (i >= firstNullable) {
type.toBuilder().setNullable(true).build()
}
else type
} else type
}
}
else originalTypes
} else originalTypes
}
operator fun get(index: Int) = types[index]
@@ -4,6 +4,7 @@
*/
@file:Suppress("FINITE_BOUNDS_VIOLATION_IN_JAVA")
package org.jetbrains.kotlin.metadata.serialization
import org.jetbrains.kotlin.metadata.ProtoBuf
@@ -20,9 +21,9 @@ private class TableElementWrapper<Element : GeneratedMessageLite.Builder<*, Elem
}
abstract class MutableTable<Element, Table, TableBuilder>
where Element : GeneratedMessageLite.Builder<*, Element>,
Table : GeneratedMessageLite,
TableBuilder : GeneratedMessageLite.Builder<Table, TableBuilder> {
where Element : GeneratedMessageLite.Builder<*, Element>,
Table : GeneratedMessageLite,
TableBuilder : GeneratedMessageLite.Builder<Table, TableBuilder> {
private val interner = Interner<TableElementWrapper<Element>>()
@@ -31,16 +32,16 @@ abstract class MutableTable<Element, Table, TableBuilder>
protected abstract fun addElement(builder: TableBuilder, element: Element)
operator fun get(type: Element): Int =
interner.intern(TableElementWrapper(type))
interner.intern(TableElementWrapper(type))
@Suppress("UNCHECKED_CAST")
fun serialize(): Table? =
if (interner.isEmpty) null
else createTableBuilder().apply {
for (obj in interner.allInternedObjects) {
addElement(this, obj.builder)
}
}.build() as Table
if (interner.isEmpty) null
else createTableBuilder().apply {
for (obj in interner.allInternedObjects) {
addElement(this, obj.builder)
}
}.build() as Table
}
class MutableTypeTable : MutableTable<ProtoBuf.Type.Builder, ProtoBuf.TypeTable, ProtoBuf.TypeTable.Builder>() {
@@ -51,7 +52,8 @@ class MutableTypeTable : MutableTable<ProtoBuf.Type.Builder, ProtoBuf.TypeTable,
}
}
class MutableVersionRequirementTable : MutableTable<ProtoBuf.VersionRequirement.Builder, ProtoBuf.VersionRequirementTable, ProtoBuf.VersionRequirementTable.Builder>() {
class MutableVersionRequirementTable :
MutableTable<ProtoBuf.VersionRequirement.Builder, ProtoBuf.VersionRequirementTable, ProtoBuf.VersionRequirementTable.Builder>() {
override fun createTableBuilder(): ProtoBuf.VersionRequirementTable.Builder = ProtoBuf.VersionRequirementTable.newBuilder()
override fun addElement(builder: ProtoBuf.VersionRequirementTable.Builder, element: ProtoBuf.VersionRequirement.Builder) {