Refactor BinaryVersion and subclasses, get rid of static factories
This commit is contained in:
@@ -146,6 +146,6 @@ public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
|
||||
int[] version = (int[]) CodegenTestUtil.getAnnotationAttribute(annotation, VERSION_FIELD_NAME);
|
||||
assertNotNull(version);
|
||||
assertTrue("Annotation " + annotationFqName + " is written with an unsupported format",
|
||||
JvmMetadataVersion.create(version).isCompatible());
|
||||
new JvmMetadataVersion(version).isCompatible());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,19 +26,15 @@ class KotlinJavascriptMetadata(val abiVersion: Int, val moduleName: String, val
|
||||
}
|
||||
|
||||
// TODO: move to JS modules
|
||||
class JsBinaryVersion protected constructor(
|
||||
major: Int, minor: Int, patch: Int, rest: List<Int>
|
||||
) : BinaryVersion(major, minor, patch, rest) {
|
||||
class JsBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
|
||||
override fun isCompatible() = this.isCompatibleTo(INSTANCE)
|
||||
|
||||
companion object {
|
||||
@JvmField val INSTANCE = create(0, 3, 0)
|
||||
@JvmField
|
||||
val INSTANCE = JsBinaryVersion(0, 3, 0)
|
||||
|
||||
@JvmField val INVALID_VERSION = JsBinaryVersion.create(IntArray(0))
|
||||
|
||||
@JvmStatic fun create(version: IntArray) = create(version, ::JsBinaryVersion)
|
||||
|
||||
@JvmStatic fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::JsBinaryVersion)
|
||||
@JvmField
|
||||
val INVALID_VERSION = JsBinaryVersion()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-11
@@ -22,22 +22,14 @@ import org.jetbrains.kotlin.serialization.deserialization.BinaryVersion
|
||||
* The version of conventions used in bytecode of generated .class files, such as default method naming & signatures,
|
||||
* internal member name mangling specifics, property getter/setter names, etc.
|
||||
*/
|
||||
class JvmBytecodeBinaryVersion protected constructor(
|
||||
major: Int, minor: Int, patch: Int, rest: List<Int>
|
||||
) : BinaryVersion(major, minor, patch, rest) {
|
||||
class JvmBytecodeBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
|
||||
override fun isCompatible() = this.isCompatibleTo(INSTANCE)
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val INSTANCE = create(1, 0, 2)
|
||||
val INSTANCE = JvmBytecodeBinaryVersion(1, 0, 2)
|
||||
|
||||
@JvmField
|
||||
val INVALID_VERSION = JvmBytecodeBinaryVersion.create(IntArray(0))
|
||||
|
||||
@JvmStatic
|
||||
fun create(version: IntArray) = create(version, ::JvmBytecodeBinaryVersion)
|
||||
|
||||
@JvmStatic
|
||||
fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::JvmBytecodeBinaryVersion)
|
||||
val INVALID_VERSION = JvmBytecodeBinaryVersion()
|
||||
}
|
||||
}
|
||||
|
||||
+3
-11
@@ -22,22 +22,14 @@ import org.jetbrains.kotlin.serialization.deserialization.BinaryVersion
|
||||
* The version of the metadata serialized by the compiler and deserialized by the compiler and reflection.
|
||||
* This version includes the version of the core protobuf messages (descriptors.proto) as well as JVM extensions (jvm_descriptors.proto).
|
||||
*/
|
||||
class JvmMetadataVersion protected constructor(
|
||||
major: Int, minor: Int, patch: Int, rest: List<Int>
|
||||
) : BinaryVersion(major, minor, patch, rest) {
|
||||
class JvmMetadataVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
|
||||
override fun isCompatible() = this.isCompatibleTo(INSTANCE)
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val INSTANCE = create(1, 0, 2)
|
||||
val INSTANCE = JvmMetadataVersion(1, 0, 2)
|
||||
|
||||
@JvmField
|
||||
val INVALID_VERSION = JvmMetadataVersion.create(IntArray(0))
|
||||
|
||||
@JvmStatic
|
||||
fun create(version: IntArray) = create(version, ::JvmMetadataVersion)
|
||||
|
||||
@JvmStatic
|
||||
fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::JvmMetadataVersion)
|
||||
val INVALID_VERSION = JvmMetadataVersion()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class ModuleMapping private constructor(val packageFqName2Parts: Map<String, Pac
|
||||
}
|
||||
|
||||
val stream = DataInputStream(ByteArrayInputStream(proto))
|
||||
val version = JvmMetadataVersion.create(IntArray(stream.readInt()) { stream.readInt() })
|
||||
val version = JvmMetadataVersion(*IntArray(stream.readInt()) { stream.readInt() })
|
||||
|
||||
if (version.isCompatible()) {
|
||||
val parseFrom = JvmPackageTable.PackageTable.parseFrom(stream)
|
||||
|
||||
+4
-4
@@ -134,12 +134,12 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
|
||||
}
|
||||
else if (METADATA_VERSION_FIELD_NAME.equals(string)) {
|
||||
if (value instanceof int[]) {
|
||||
metadataVersion = JvmMetadataVersion.create((int[]) value);
|
||||
metadataVersion = new JvmMetadataVersion((int[]) value);
|
||||
}
|
||||
}
|
||||
else if (BYTECODE_VERSION_FIELD_NAME.equals(string)) {
|
||||
if (value instanceof int[]) {
|
||||
bytecodeVersion = JvmBytecodeBinaryVersion.create((int[]) value);
|
||||
bytecodeVersion = new JvmBytecodeBinaryVersion((int[]) value);
|
||||
}
|
||||
}
|
||||
else if (SYNTHETIC_CLASS_KIND_FIELD_NAME.equals(string)) {
|
||||
@@ -212,11 +212,11 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
|
||||
String string = name.asString();
|
||||
if (VERSION_FIELD_NAME.equals(string)) {
|
||||
if (value instanceof int[]) {
|
||||
metadataVersion = JvmMetadataVersion.create((int[]) value);
|
||||
metadataVersion = new JvmMetadataVersion((int[]) value);
|
||||
|
||||
// If there's no bytecode binary version in the class file, we assume it to be equal to the metadata version
|
||||
if (bytecodeVersion == null) {
|
||||
bytecodeVersion = JvmBytecodeBinaryVersion.create((int[]) value);
|
||||
bytecodeVersion = new JvmBytecodeBinaryVersion((int[]) value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,19 +22,11 @@ import org.jetbrains.kotlin.serialization.deserialization.BinaryVersion
|
||||
* The version of the format in which the .kotlin_builtins file is stored. This version also includes the version
|
||||
* of the core protobuf messages (descriptors.proto).
|
||||
*/
|
||||
class BuiltInsBinaryVersion protected constructor(
|
||||
major: Int, minor: Int, patch: Int, rest: List<Int>
|
||||
) : BinaryVersion(major, minor, patch, rest) {
|
||||
class BuiltInsBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
|
||||
override fun isCompatible() = this.isCompatibleTo(INSTANCE)
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val INSTANCE = create(1, 0, 0)
|
||||
|
||||
@JvmStatic
|
||||
fun create(version: IntArray) = create(version, ::BuiltInsBinaryVersion)
|
||||
|
||||
@JvmStatic
|
||||
fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::BuiltInsBinaryVersion)
|
||||
val INSTANCE = BuiltInsBinaryVersion(1, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ class BuiltinsPackageFragment(
|
||||
) : DeserializedPackageFragment(fqName, storageManager, module, BuiltInsSerializedResourcePaths, loadResource) {
|
||||
val builtinsMessage = loadResource(BuiltInsSerializedResourcePaths.getBuiltInsFilePath(fqName))?.let { stream ->
|
||||
val dataInput = DataInputStream(stream)
|
||||
val version = BuiltInsBinaryVersion.create((1..dataInput.readInt()).map { dataInput.readInt() }.toIntArray())
|
||||
val version = BuiltInsBinaryVersion(*(1..dataInput.readInt()).map { dataInput.readInt() }.toIntArray())
|
||||
|
||||
if (!version.isCompatible()) {
|
||||
// TODO: report a proper diagnostic
|
||||
|
||||
+7
-26
@@ -25,16 +25,15 @@ package org.jetbrains.kotlin.serialization.deserialization
|
||||
* - Patch version can be increased freely and is only supposed to be used for debugging. Increase the patch version when you
|
||||
* make a change to binaries which is both forward- and backward compatible.
|
||||
*/
|
||||
abstract class BinaryVersion protected constructor(
|
||||
val major: Int,
|
||||
val minor: Int,
|
||||
val patch: Int,
|
||||
val rest: List<Int>
|
||||
) {
|
||||
abstract class BinaryVersion(vararg val numbers: Int) {
|
||||
val major: Int = numbers.getOrNull(0) ?: UNKNOWN
|
||||
val minor: Int = numbers.getOrNull(1) ?: UNKNOWN
|
||||
val patch: Int = numbers.getOrNull(2) ?: UNKNOWN
|
||||
val rest: List<Int> = if (numbers.size > 3) numbers.asList().subList(3, numbers.size).toList() else emptyList()
|
||||
|
||||
abstract fun isCompatible(): Boolean
|
||||
|
||||
fun toArray(): IntArray =
|
||||
intArrayOf(major, minor, patch, *rest.toIntArray())
|
||||
fun toArray(): IntArray = numbers
|
||||
|
||||
/**
|
||||
* Returns true if this version of some format loaded from some binaries is compatible
|
||||
@@ -66,23 +65,5 @@ abstract class BinaryVersion protected constructor(
|
||||
|
||||
companion object {
|
||||
private val UNKNOWN = -1
|
||||
|
||||
@JvmStatic
|
||||
fun <T : BinaryVersion> create(
|
||||
version: IntArray,
|
||||
factory: (major: Int, minor: Int, patch: Int, rest: List<Int>) -> T
|
||||
): T {
|
||||
return factory(
|
||||
version.getOrNull(0) ?: UNKNOWN,
|
||||
version.getOrNull(1) ?: UNKNOWN,
|
||||
version.getOrNull(2) ?: UNKNOWN,
|
||||
if (version.size > 3) version.asList().subList(3, version.size).toList() else emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun <T : BinaryVersion> create(
|
||||
major: Int, minor: Int, patch: Int, factory: (major: Int, minor: Int, patch: Int, rest: List<Int>) -> T
|
||||
): T = factory(major, minor, patch, emptyList())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils
|
||||
import java.util.*
|
||||
|
||||
object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinJavaScriptAbiVersionIndex>(
|
||||
KotlinJavaScriptAbiVersionIndex::class.java, { JsBinaryVersion.create(it) }
|
||||
KotlinJavaScriptAbiVersionIndex::class.java, { JsBinaryVersion(*it) }
|
||||
) {
|
||||
override fun getIndexer() = INDEXER
|
||||
|
||||
@@ -47,7 +47,7 @@ object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinJavaScr
|
||||
KotlinJavascriptMetadataUtils.parseMetadata(text, metadataList)
|
||||
for (metadata in metadataList) {
|
||||
val version = if (KotlinJavascriptMetadataUtils.isAbiVersionCompatible(metadata.abiVersion)) {
|
||||
JsBinaryVersion.create(0, metadata.abiVersion, 0)
|
||||
JsBinaryVersion(0, metadata.abiVersion, 0)
|
||||
}
|
||||
else {
|
||||
// Version is set to something weird
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.jetbrains.org.objectweb.asm.ClassVisitor
|
||||
import org.jetbrains.org.objectweb.asm.Opcodes
|
||||
|
||||
object KotlinMetadataVersionIndex : KotlinAbiVersionIndexBase<KotlinMetadataVersionIndex>(
|
||||
KotlinMetadataVersionIndex::class.java, { JvmMetadataVersion.create(it) }
|
||||
KotlinMetadataVersionIndex::class.java, { JvmMetadataVersion(*it) }
|
||||
) {
|
||||
override fun getIndexer() = INDEXER
|
||||
|
||||
@@ -61,7 +61,7 @@ object KotlinMetadataVersionIndex : KotlinAbiVersionIndexBase<KotlinMetadataVers
|
||||
return object : AnnotationVisitor(Opcodes.ASM5) {
|
||||
override fun visit(name: String, value: Any) {
|
||||
if (name == VERSION_FIELD_NAME && value is IntArray) {
|
||||
version = JvmMetadataVersion.create(value)
|
||||
version = JvmMetadataVersion(*value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user