From 5cda676c9ff79ec9c9b6cc99606b96f1b7c87439 Mon Sep 17 00:00:00 2001 From: Sergey Rostov Date: Wed, 9 Jan 2019 14:55:27 +0300 Subject: [PATCH] JPS: Print components of CacheVersion, add tests --- .../jps/incremental/CacheVersionTest.kt | 57 +++++++++++++++++++ .../jps/incremental/CacheVersionManager.kt | 46 +++++++++++---- .../CompositeLookupsCacheAttributes.kt | 7 +-- 3 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CacheVersionTest.kt diff --git a/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CacheVersionTest.kt b/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CacheVersionTest.kt new file mode 100644 index 00000000000..c48a5354140 --- /dev/null +++ b/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CacheVersionTest.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2010-2019 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.jps.incremental + +import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmBytecodeBinaryVersion +import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmMetadataVersion +import org.junit.Assert.assertEquals +import org.junit.Test + +class CacheVersionTest { + @Test + fun testConstruct() { + assertEquals( + 3011001, + CacheVersion( + 3, + JvmBytecodeBinaryVersion(1, 0, 3), + JvmMetadataVersion(1, 1, 13) + ).intValue + ) + } + + @Test + fun testDeconstruct() { + assertEquals( + "CacheVersion(caches: 3, bytecode: 1.0, metadata: 1.1)", + CacheVersion(3011001).toString() + ) + } + + @Test + fun testConstructDeconstruct() { + val version = CacheVersion( + 1, + JvmBytecodeBinaryVersion(2, 3), + JvmMetadataVersion(4, 5) + ) + + assertEquals(1024305, version.intValue) + assertEquals("CacheVersion(caches: 1, bytecode: 2.3, metadata: 4.5)", version.toString()) + } + + @Test + fun testMaxValues() { + assertEquals( + "CacheVersion(caches: 2146, bytecode: 9.9, metadata: 9.99)", + CacheVersion( + 2146, + JvmBytecodeBinaryVersion(9, 9), + JvmMetadataVersion(9, 99) + ).toString() + ) + } +} \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt index 5f0192aabb2..efd4f2434d3 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt @@ -23,16 +23,7 @@ class CacheVersionManager( ) : CacheAttributesManager { override val expected: CacheVersion? = if (expectedOwnVersion == null) null - else { - val metadata = JvmMetadataVersion.INSTANCE - val bytecode = JvmBytecodeBinaryVersion.INSTANCE - - CacheVersion( - expectedOwnVersion * 1000000 + - bytecode.major * 10000 + bytecode.minor * 100 + - metadata.major * 1000 + metadata.minor - ) - } + else CacheVersion(expectedOwnVersion, JvmBytecodeBinaryVersion.INSTANCE, JvmMetadataVersion.INSTANCE) override fun loadActual(): CacheVersion? = if (!versionFile.exists()) null @@ -48,7 +39,7 @@ class CacheVersionManager( if (values == null) versionFile.delete() else { versionFile.parentFile.mkdirs() - versionFile.writeText(values.version.toString()) + versionFile.writeText(values.intValue.toString()) } } @@ -57,4 +48,35 @@ class CacheVersionManager( get() = versionFile } -data class CacheVersion(val version: Int) \ No newline at end of file +fun CacheVersion(own: Int, bytecode: JvmBytecodeBinaryVersion, metadata: JvmMetadataVersion): CacheVersion { + require(own in 0..(Int.MAX_VALUE / 1000000 - 1)) + require(bytecode.major in 0..9) + require(bytecode.minor in 0..9) + require(metadata.major in 0..9) + require(metadata.minor in 0..99) + + return CacheVersion( + own * 1000000 + + bytecode.major * 10000 + bytecode.minor * 100 + + metadata.major * 1000 + metadata.minor + ) +} + +data class CacheVersion(val intValue: Int) { + val own: Int + get() = intValue / 1000000 + + val bytecode: JvmBytecodeBinaryVersion + get() = JvmBytecodeBinaryVersion( + intValue / 10000 % 10, + intValue / 100 % 10 + ) + + val metadata: JvmMetadataVersion + get() = JvmMetadataVersion( + intValue / 1000 % 10, + intValue / 1 % 100 + ) + + override fun toString(): String = "CacheVersion(caches: $own, bytecode: $bytecode, metadata: $metadata)" +} \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt index 864d9c22f96..ccd6b6e5dcc 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt @@ -6,9 +6,6 @@ package org.jetbrains.kotlin.jps.incremental import org.jetbrains.annotations.TestOnly -import org.jetbrains.kotlin.jps.incremental.CacheAttributesManager -import org.jetbrains.kotlin.jps.incremental.CacheVersion -import org.jetbrains.kotlin.jps.incremental.lookupsCacheVersionManager import java.io.File import java.io.IOException @@ -31,7 +28,7 @@ class CompositeLookupsCacheAttributesManager( override val expected: CompositeLookupsCacheAttributes? = if (expectedComponents.isEmpty()) null - else CompositeLookupsCacheAttributes(versionManager.expected!!.version, expectedComponents) + else CompositeLookupsCacheAttributes(versionManager.expected!!.intValue, expectedComponents) override fun loadActual(): CompositeLookupsCacheAttributes? { val version = versionManager.loadActual() ?: return null @@ -44,7 +41,7 @@ class CompositeLookupsCacheAttributesManager( return null } - return CompositeLookupsCacheAttributes(version.version, components) + return CompositeLookupsCacheAttributes(version.intValue, components) } override fun writeVersion(values: CompositeLookupsCacheAttributes?) {