Refactor BinaryVersion and subclasses, get rid of static factories

This commit is contained in:
Alexander Udalov
2016-01-18 00:13:55 +03:00
parent e37bd4eba6
commit d4c4515944
11 changed files with 31 additions and 78 deletions
@@ -146,6 +146,6 @@ public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
int[] version = (int[]) CodegenTestUtil.getAnnotationAttribute(annotation, VERSION_FIELD_NAME); int[] version = (int[]) CodegenTestUtil.getAnnotationAttribute(annotation, VERSION_FIELD_NAME);
assertNotNull(version); assertNotNull(version);
assertTrue("Annotation " + annotationFqName + " is written with an unsupported format", 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 // TODO: move to JS modules
class JsBinaryVersion protected constructor( class JsBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
major: Int, minor: Int, patch: Int, rest: List<Int>
) : BinaryVersion(major, minor, patch, rest) {
override fun isCompatible() = this.isCompatibleTo(INSTANCE) override fun isCompatible() = this.isCompatibleTo(INSTANCE)
companion object { companion object {
@JvmField val INSTANCE = create(0, 3, 0) @JvmField
val INSTANCE = JsBinaryVersion(0, 3, 0)
@JvmField val INVALID_VERSION = JsBinaryVersion.create(IntArray(0)) @JvmField
val INVALID_VERSION = JsBinaryVersion()
@JvmStatic fun create(version: IntArray) = create(version, ::JsBinaryVersion)
@JvmStatic fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::JsBinaryVersion)
} }
} }
@@ -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, * 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. * internal member name mangling specifics, property getter/setter names, etc.
*/ */
class JvmBytecodeBinaryVersion protected constructor( class JvmBytecodeBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
major: Int, minor: Int, patch: Int, rest: List<Int>
) : BinaryVersion(major, minor, patch, rest) {
override fun isCompatible() = this.isCompatibleTo(INSTANCE) override fun isCompatible() = this.isCompatibleTo(INSTANCE)
companion object { companion object {
@JvmField @JvmField
val INSTANCE = create(1, 0, 2) val INSTANCE = JvmBytecodeBinaryVersion(1, 0, 2)
@JvmField @JvmField
val INVALID_VERSION = JvmBytecodeBinaryVersion.create(IntArray(0)) val INVALID_VERSION = JvmBytecodeBinaryVersion()
@JvmStatic
fun create(version: IntArray) = create(version, ::JvmBytecodeBinaryVersion)
@JvmStatic
fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::JvmBytecodeBinaryVersion)
} }
} }
@@ -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. * 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). * This version includes the version of the core protobuf messages (descriptors.proto) as well as JVM extensions (jvm_descriptors.proto).
*/ */
class JvmMetadataVersion protected constructor( class JvmMetadataVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
major: Int, minor: Int, patch: Int, rest: List<Int>
) : BinaryVersion(major, minor, patch, rest) {
override fun isCompatible() = this.isCompatibleTo(INSTANCE) override fun isCompatible() = this.isCompatibleTo(INSTANCE)
companion object { companion object {
@JvmField @JvmField
val INSTANCE = create(1, 0, 2) val INSTANCE = JvmMetadataVersion(1, 0, 2)
@JvmField @JvmField
val INVALID_VERSION = JvmMetadataVersion.create(IntArray(0)) val INVALID_VERSION = JvmMetadataVersion()
@JvmStatic
fun create(version: IntArray) = create(version, ::JvmMetadataVersion)
@JvmStatic
fun create(major: Int, minor: Int, patch: Int) = create(major, minor, patch, ::JvmMetadataVersion)
} }
} }
@@ -39,7 +39,7 @@ class ModuleMapping private constructor(val packageFqName2Parts: Map<String, Pac
} }
val stream = DataInputStream(ByteArrayInputStream(proto)) 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()) { if (version.isCompatible()) {
val parseFrom = JvmPackageTable.PackageTable.parseFrom(stream) val parseFrom = JvmPackageTable.PackageTable.parseFrom(stream)
@@ -134,12 +134,12 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
} }
else if (METADATA_VERSION_FIELD_NAME.equals(string)) { else if (METADATA_VERSION_FIELD_NAME.equals(string)) {
if (value instanceof int[]) { if (value instanceof int[]) {
metadataVersion = JvmMetadataVersion.create((int[]) value); metadataVersion = new JvmMetadataVersion((int[]) value);
} }
} }
else if (BYTECODE_VERSION_FIELD_NAME.equals(string)) { else if (BYTECODE_VERSION_FIELD_NAME.equals(string)) {
if (value instanceof int[]) { if (value instanceof int[]) {
bytecodeVersion = JvmBytecodeBinaryVersion.create((int[]) value); bytecodeVersion = new JvmBytecodeBinaryVersion((int[]) value);
} }
} }
else if (SYNTHETIC_CLASS_KIND_FIELD_NAME.equals(string)) { else if (SYNTHETIC_CLASS_KIND_FIELD_NAME.equals(string)) {
@@ -212,11 +212,11 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
String string = name.asString(); String string = name.asString();
if (VERSION_FIELD_NAME.equals(string)) { if (VERSION_FIELD_NAME.equals(string)) {
if (value instanceof int[]) { 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 there's no bytecode binary version in the class file, we assume it to be equal to the metadata version
if (bytecodeVersion == null) { 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 * 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). * of the core protobuf messages (descriptors.proto).
*/ */
class BuiltInsBinaryVersion protected constructor( class BuiltInsBinaryVersion(vararg numbers: Int) : BinaryVersion(*numbers) {
major: Int, minor: Int, patch: Int, rest: List<Int>
) : BinaryVersion(major, minor, patch, rest) {
override fun isCompatible() = this.isCompatibleTo(INSTANCE) override fun isCompatible() = this.isCompatibleTo(INSTANCE)
companion object { companion object {
@JvmField @JvmField
val INSTANCE = create(1, 0, 0) val INSTANCE = BuiltInsBinaryVersion(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)
} }
} }
@@ -36,7 +36,7 @@ class BuiltinsPackageFragment(
) : DeserializedPackageFragment(fqName, storageManager, module, BuiltInsSerializedResourcePaths, loadResource) { ) : DeserializedPackageFragment(fqName, storageManager, module, BuiltInsSerializedResourcePaths, loadResource) {
val builtinsMessage = loadResource(BuiltInsSerializedResourcePaths.getBuiltInsFilePath(fqName))?.let { stream -> val builtinsMessage = loadResource(BuiltInsSerializedResourcePaths.getBuiltInsFilePath(fqName))?.let { stream ->
val dataInput = DataInputStream(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()) { if (!version.isCompatible()) {
// TODO: report a proper diagnostic // TODO: report a proper diagnostic
@@ -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 * - 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. * make a change to binaries which is both forward- and backward compatible.
*/ */
abstract class BinaryVersion protected constructor( abstract class BinaryVersion(vararg val numbers: Int) {
val major: Int, val major: Int = numbers.getOrNull(0) ?: UNKNOWN
val minor: Int, val minor: Int = numbers.getOrNull(1) ?: UNKNOWN
val patch: Int, val patch: Int = numbers.getOrNull(2) ?: UNKNOWN
val rest: List<Int> val rest: List<Int> = if (numbers.size > 3) numbers.asList().subList(3, numbers.size).toList() else emptyList()
) {
abstract fun isCompatible(): Boolean abstract fun isCompatible(): Boolean
fun toArray(): IntArray = fun toArray(): IntArray = numbers
intArrayOf(major, minor, patch, *rest.toIntArray())
/** /**
* Returns true if this version of some format loaded from some binaries is compatible * 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 { companion object {
private val UNKNOWN = -1 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.* import java.util.*
object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinJavaScriptAbiVersionIndex>( object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinJavaScriptAbiVersionIndex>(
KotlinJavaScriptAbiVersionIndex::class.java, { JsBinaryVersion.create(it) } KotlinJavaScriptAbiVersionIndex::class.java, { JsBinaryVersion(*it) }
) { ) {
override fun getIndexer() = INDEXER override fun getIndexer() = INDEXER
@@ -47,7 +47,7 @@ object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinJavaScr
KotlinJavascriptMetadataUtils.parseMetadata(text, metadataList) KotlinJavascriptMetadataUtils.parseMetadata(text, metadataList)
for (metadata in metadataList) { for (metadata in metadataList) {
val version = if (KotlinJavascriptMetadataUtils.isAbiVersionCompatible(metadata.abiVersion)) { val version = if (KotlinJavascriptMetadataUtils.isAbiVersionCompatible(metadata.abiVersion)) {
JsBinaryVersion.create(0, metadata.abiVersion, 0) JsBinaryVersion(0, metadata.abiVersion, 0)
} }
else { else {
// Version is set to something weird // Version is set to something weird
@@ -30,7 +30,7 @@ import org.jetbrains.org.objectweb.asm.ClassVisitor
import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.Opcodes
object KotlinMetadataVersionIndex : KotlinAbiVersionIndexBase<KotlinMetadataVersionIndex>( object KotlinMetadataVersionIndex : KotlinAbiVersionIndexBase<KotlinMetadataVersionIndex>(
KotlinMetadataVersionIndex::class.java, { JvmMetadataVersion.create(it) } KotlinMetadataVersionIndex::class.java, { JvmMetadataVersion(*it) }
) { ) {
override fun getIndexer() = INDEXER override fun getIndexer() = INDEXER
@@ -61,7 +61,7 @@ object KotlinMetadataVersionIndex : KotlinAbiVersionIndexBase<KotlinMetadataVers
return object : AnnotationVisitor(Opcodes.ASM5) { return object : AnnotationVisitor(Opcodes.ASM5) {
override fun visit(name: String, value: Any) { override fun visit(name: String, value: Any) {
if (name == VERSION_FIELD_NAME && value is IntArray) { if (name == VERSION_FIELD_NAME && value is IntArray) {
version = JvmMetadataVersion.create(value) version = JvmMetadataVersion(*value)
} }
} }
} }