[KPM] WIP deepCopy

KT-51262
KT-51220
This commit is contained in:
sebastian.sellmair
2022-02-17 09:13:53 +01:00
committed by Space
parent c56bf6b0e3
commit 63578b701f
15 changed files with 245 additions and 15 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.gradle.kpm
import org.jetbrains.kotlin.gradle.kpm.idea.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.kpm.idea.Interner
import java.io.Serializable
sealed class KotlinExternalModelContainer : Serializable {
@@ -15,6 +16,23 @@ sealed class KotlinExternalModelContainer : Serializable {
fun <T : Any> getOrThrow(key: KotlinExternalModelKey<T>): T = get(key)
?: throw NoSuchElementException("Missing external model for ${key.id}")
fun isEmpty(): Boolean = ids.isEmpty()
fun isNotEmpty(): Boolean = ids.isNotEmpty()
override fun equals(other: Any?): Boolean {
if (other !is KotlinExternalModelContainer) return false
if (this.isEmpty() && other.isEmpty()) return true
return super.equals(other)
}
// TODO CHECK WITH EMPTY INTANCE
override fun hashCode(): Int {
if (this.isEmpty()) return 0
return super.hashCode()
}
internal abstract fun copy(): KotlinExternalModelContainer
@InternalKotlinGradlePluginApi
companion object {
fun mutable(): KotlinMutableExternalModelContainer = KotlinMutableExternalModelContainerImpl()
@@ -24,6 +42,7 @@ sealed class KotlinExternalModelContainer : Serializable {
override val ids: Set<KotlinExternalModelId<*>> = emptySet()
override fun <T : Any> contains(key: KotlinExternalModelKey<T>): Boolean = false
override fun <T : Any> get(key: KotlinExternalModelKey<T>): T? = null
override fun copy(): KotlinExternalModelContainer = this
}
override fun toString(): String {
@@ -7,8 +7,11 @@ package org.jetbrains.kotlin.gradle.kpm
import java.io.Serializable
internal class KotlinMutableExternalModelContainerImpl : KotlinMutableExternalModelContainer(), Serializable {
private val values = mutableMapOf<KotlinExternalModelKey<*>, Any>()
internal class KotlinMutableExternalModelContainerImpl private constructor(
private val values: MutableMap<KotlinExternalModelKey<*>, Any>
) : KotlinMutableExternalModelContainer(), Serializable {
constructor() : this(mutableMapOf())
override val ids: Set<KotlinExternalModelId<*>>
@Synchronized get() = values.keys.map { it.id }.toSet()
@@ -29,6 +32,18 @@ internal class KotlinMutableExternalModelContainerImpl : KotlinMutableExternalMo
return values[key]?.let { it as T }
}
override fun copy(): KotlinMutableExternalModelContainerImpl {
val copiedValues = values.mapValuesTo(mutableMapOf()) { (key, value) ->
val serializer = key.serializer
if (key.serializer != null) {
@Suppress("UNCHECKED_CAST")
serializer as KotlinExternalModelSerializer<Any>
serializer.deserialize(serializer.serialize(value))
} else value
}
return KotlinMutableExternalModelContainerImpl(copiedValues)
}
@Synchronized
private fun writeReplace(): Any {
return SerializedKotlinExternalModelContainerCarrier(serialize(values))
@@ -64,6 +79,21 @@ private class SerializedKotlinExternalModelContainer(
return deserializedValue
}
override fun copy(): KotlinExternalModelContainer {
val copiedValues = deserializedValues.mapValuesTo(mutableMapOf()) { (key, value) ->
/* Serializer has to be present, otherwise where would the value be coming from ¯\_(ツ)_/¯ */
@Suppress("unchecked_cast")
val serializer = key.serializer as KotlinExternalModelSerializer<Any>
serializer.serialize(value)
}.mapKeys { (key, _) -> key.id }
/**
* [serializedValues] is all private. [ByteArray] will never be mutated and can't be referenced from anywhere
* [KotlinExternalModelKey] is immutable. We therefore only need to copy already deserialized values here.
*/
return SerializedKotlinExternalModelContainer(serializedValues.plus(copiedValues).toMutableMap())
}
@Synchronized
private fun writeReplace(): Any {
return SerializedKotlinExternalModelContainerCarrier(serializedValues + serialize(deserializedValues))
@@ -13,6 +13,13 @@ interface IdeaKotlinCompilationOutput : Serializable {
val resourcesDir: File?
}
fun IdeaKotlinCompilationOutput.deepCopy(interner: Interner = Interner.default()): IdeaKotlinCompilationOutput {
return IdeaKotlinCompilationOutputImpl(
classesDirs = interner.internSet(classesDirs),
resourcesDir = interner.intern(resourcesDir)
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinCompilationOutputImpl(
override val classesDirs: Set<File>,
@@ -19,6 +19,18 @@ interface IdeaKotlinFragment : Serializable {
val external: KotlinExternalModelContainer
}
fun IdeaKotlinFragment.deepCopy(interner: Interner = Interner.default()): IdeaKotlinFragment {
return IdeaKotlinFragmentImpl(
name = interner.intern(name),
languageSettings = interner.intern(languageSettings?.deepCopy(interner)),
dependencies = interner.internList(dependencies.map { it.deepCopy(interner) }),
directRefinesDependencies = interner.internList(directRefinesDependencies.map { it.deepCopy(interner) }),
sourceDirectories = interner.internList(sourceDirectories.map { it.deepCopy(interner) }),
resourceDirectories = interner.internList(resourceDirectories.map { it.deepCopy(interner) }),
external = external.copy()
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinFragmentImpl(
override val name: String,
@@ -8,3 +8,8 @@ package org.jetbrains.kotlin.gradle.kpm.idea
import java.io.Serializable
interface IdeaKotlinFragmentDependency : Serializable
fun IdeaKotlinFragmentDependency.deepCopy(interner: Interner = Interner.default()): IdeaKotlinFragmentDependency {
// TODO
return interner.intern(this)
}
@@ -19,6 +19,19 @@ interface IdeaKotlinLanguageSettings : Serializable {
val freeCompilerArgs: List<String>
}
fun IdeaKotlinLanguageSettings.deepCopy(interner: Interner = Interner.default()): IdeaKotlinLanguageSettings {
return IdeaKotlinLanguageSettingsImpl(
languageVersion = interner.intern(languageVersion),
apiVersion = interner.intern(apiVersion),
isProgressiveMode = isProgressiveMode,
enabledLanguageFeatures = interner.internSet(enabledLanguageFeatures),
optInAnnotationsInUse = interner.internSet(optInAnnotationsInUse),
compilerPluginArguments = interner.internList(compilerPluginArguments),
compilerPluginClasspath = interner.internList(compilerPluginClasspath),
freeCompilerArgs = interner.internList(freeCompilerArgs)
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinLanguageSettingsImpl(
override val languageVersion: String?,
@@ -10,7 +10,14 @@ import java.io.Serializable
interface IdeaKotlinModule : Serializable {
val moduleIdentifier: IdeaKotlinModuleIdentifier
val fragments: List<IdeaKotlinFragment>
}
}
fun IdeaKotlinModule.deepCopy(interner: Interner): IdeaKotlinModule {
return IdeaKotlinModuleImpl(
moduleIdentifier = interner.intern(moduleIdentifier.deepCopy(interner)),
fragments = interner.internList(fragments.map { it.deepCopy(interner) })
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinModuleImpl(
@@ -21,6 +21,30 @@ interface IdeaKotlinMavenModuleIdentifier : IdeaKotlinModuleIdentifier {
val name: String
}
fun IdeaKotlinModuleIdentifier.deepCopy(interner: Interner = Interner.default()): IdeaKotlinModuleIdentifier {
return when (this) {
is IdeaKotlinLocalModuleIdentifier -> this.deepCopy(interner)
is IdeaKotlinMavenModuleIdentifier -> this.deepCopy(interner)
else -> throw IllegalArgumentException("Unexpected ${IdeaKotlinMavenModuleIdentifier::class.java.simpleName}: $this")
}
}
fun IdeaKotlinLocalModuleIdentifier.deepCopy(interner: Interner = Interner.default()): IdeaKotlinLocalModuleIdentifier {
return IdeaKotlinLocalModuleIdentifierImpl(
moduleClassifier = interner.intern(moduleClassifier),
buildId = interner.intern(buildId),
projectId = interner.intern(projectId)
)
}
fun IdeaKotlinMavenModuleIdentifier.deepCopy(interner: Interner = Interner.default()): IdeaKotlinMavenModuleIdentifier {
return IdeaKotlinMavenModuleIdentifierImpl(
moduleClassifier = interner.intern(moduleClassifier),
group = interner.intern(group),
name = interner.intern(name)
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinLocalModuleIdentifierImpl(
override val moduleClassifier: String?,
@@ -16,6 +16,16 @@ interface IdeaKotlinProjectModel : Serializable {
val modules: List<IdeaKotlinModule>
}
fun IdeaKotlinProjectModel.deepCopy(interner: Interner = Interner.default()): IdeaKotlinProjectModel {
return IdeaKotlinProjectModelImpl(
gradlePluginVersion = interner.intern(gradlePluginVersion),
coreLibrariesVersion = interner.intern(coreLibrariesVersion),
explicitApiModeCliOption = interner.intern(explicitApiModeCliOption),
kotlinNativeHome = interner.intern(kotlinNativeHome),
modules = interner.internList(modules.map { it.deepCopy(interner) })
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinProjectModelImpl(
override val gradlePluginVersion: String,
@@ -12,6 +12,12 @@ interface IdeaKotlinResourceDirectory : Serializable {
val file: File
}
fun IdeaKotlinResourceDirectory.deepCopy(interner: Interner): IdeaKotlinResourceDirectory {
return IdeaKotlinResourceDirectoryImpl(
file = interner.intern(file)
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinResourceDirectoryImpl(
override val file: File
@@ -12,6 +12,12 @@ interface IdeaKotlinSourceDirectory : Serializable {
val file: File
}
fun IdeaKotlinSourceDirectory.deepCopy(interner: Interner): IdeaKotlinSourceDirectory {
return IdeaKotlinSourceDirectoryImpl(
file = interner.intern(file)
)
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinSourceDirectoryImpl(
override val file: File,
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.gradle.kpm.idea
interface Interner {
fun <T> intern(value: T): T
companion object {
fun default(): Interner = DefaultInterner()
fun none(): Interner = NoneInterner
}
}
internal fun <T> Interner.internSet(set: Set<T>): Set<T> {
return if (set.isEmpty()) return emptySet()
else intern(set.map { intern(it) }.toSet())
}
internal fun <T> Interner.internList(list: List<T>): List<T> {
return if (list.isEmpty()) emptyList()
else intern(list.map { intern(it) })
}
private class DefaultInterner : Interner {
private val values = mutableMapOf<Any, Any>()
override fun <T> intern(value: T): T {
if (value == null) return value
@Suppress("unchecked_cast")
return values.getOrPut(value) { value } as T
}
}
object NoneInterner : Interner {
override fun <T> intern(value: T): T = value
}
@@ -0,0 +1,24 @@
import org.gradle.api.Project
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.configurationcache.extensions.serviceOf
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKotlinProjectModel
import org.jetbrains.kotlin.gradle.plugin.KotlinPm20PluginWrapper
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
fun Project.buildIdeaKotlinProjectModel(): IdeaKotlinProjectModel {
return serviceOf<ToolingModelBuilderRegistry>().getBuilder(IdeaKotlinProjectModel::class.java.name)
.buildAll(IdeaKotlinProjectModel::class.java.name, this) as IdeaKotlinProjectModel
}
fun createKpmProject(): Pair<ProjectInternal, KotlinPm20ProjectExtension> {
val project = ProjectBuilder.builder().build() as ProjectInternal
project.plugins.apply(KotlinPm20PluginWrapper::class.java)
return project to project.extensions.getByType(KotlinPm20ProjectExtension::class.java)
}
@@ -7,6 +7,8 @@
package org.jetbrains.kotlin.gradle.kpm.idea
import buildIdeaKotlinProjectModel
import createKpmProject
import createProxyInstance
import deserialize
import org.gradle.api.Project
@@ -46,11 +48,7 @@ class BackwardsCompatibilityDeserializationTest {
@Test
fun `test - simple project`() {
val project = ProjectBuilder.builder().build() as ProjectInternal
project.plugins.apply(KotlinPm20PluginWrapper::class.java)
/* Setup example project */
val kotlinExtension = project.extensions.getByType(KotlinPm20ProjectExtension::class.java)
val (project, kotlinExtension) = createKpmProject()
kotlinExtension.mainAndTest {
jvm
val native = fragments.create("native")
@@ -62,7 +60,7 @@ class BackwardsCompatibilityDeserializationTest {
}
project.evaluate()
val model = buildModel(project)
val model = project.buildIdeaKotlinProjectModel()
val deserializedModel = deserializeModelWithBackwardsCompatibleClasses(model)
/* Use proxy instances to assert the deserialized model */
@@ -102,7 +100,7 @@ class BackwardsCompatibilityDeserializationTest {
kotlinExtension.main.common.external[retainedModelKey] = RetainedModel(2411)
kotlinExtension.main.common.external[unretainedModelKey] = UnretainedModel(510)
val model = buildModel(project)
val model = project.buildIdeaKotlinProjectModel()
val deserializedModel = deserializeModelWithBackwardsCompatibleClasses(model)
val deserializedModelProxy = createProxyInstance<IdeaKotlinProjectModel>(deserializedModel)
@@ -135,11 +133,6 @@ private fun getClasspathForBackwardsCompatibilityTest(): List<File> {
.onEach { file -> if (!file.exists()) println("[WARNING] Missing $file") }
}
private fun buildModel(project: Project): IdeaKotlinProjectModel {
return project.serviceOf<ToolingModelBuilderRegistry>().getBuilder(IdeaKotlinProjectModel::class.java.name)
.buildAll(IdeaKotlinProjectModel::class.java.name, project) as IdeaKotlinProjectModel
}
private fun deserializeModelWithBackwardsCompatibleClasses(model: IdeaKotlinProjectModel): Any {
val backwardsCompatibilityClassLoader = getClassLoaderForBackwardsCompatibilityTest()
val backwardsCompatibilityModel = model.serialize().deserialize(backwardsCompatibilityClassLoader)
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.gradle.kpm.idea
import buildIdeaKotlinProjectModel
import createKpmProject
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.testfixtures.ProjectBuilder
import org.jetbrains.kotlin.gradle.plugin.KotlinPm20PluginWrapper
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinLinuxX64Variant
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.jvm
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotSame
class DeepCopyTest {
@Test
fun `test - deep copy for model build from project`() {
val (project, kotlin) = createKpmProject()
kotlin.mainAndTest {
jvm
fragments.create("linux", KotlinLinuxX64Variant::class.java)
}
val model = project.buildIdeaKotlinProjectModel()
val copy = model.deepCopy()
assertNotSame(model, copy)
assertEquals(model, copy)
}
}