[KPM] kotlin-gradle-plugin-idea: Remove deepCopy functions
KT-51262 KT-51220
This commit is contained in:
committed by
Space
parent
63578b701f
commit
522915b6dc
+4
@@ -1,9 +1,13 @@
|
||||
public abstract class org/jetbrains/kotlin/gradle/kpm/KotlinExternalModelContainer : java/io/Serializable {
|
||||
public static final field Companion Lorg/jetbrains/kotlin/gradle/kpm/KotlinExternalModelContainer$Companion;
|
||||
public abstract fun contains (Lorg/jetbrains/kotlin/gradle/kpm/KotlinExternalModelKey;)Z
|
||||
public fun equals (Ljava/lang/Object;)Z
|
||||
public abstract fun get (Lorg/jetbrains/kotlin/gradle/kpm/KotlinExternalModelKey;)Ljava/lang/Object;
|
||||
public abstract fun getIds ()Ljava/util/Set;
|
||||
public final fun getOrThrow (Lorg/jetbrains/kotlin/gradle/kpm/KotlinExternalModelKey;)Ljava/lang/Object;
|
||||
public fun hashCode ()I
|
||||
public final fun isEmpty ()Z
|
||||
public final fun isNotEmpty ()Z
|
||||
public fun toString ()Ljava/lang/String;
|
||||
}
|
||||
|
||||
|
||||
-5
@@ -6,7 +6,6 @@
|
||||
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 {
|
||||
@@ -25,14 +24,11 @@ sealed class KotlinExternalModelContainer : Serializable {
|
||||
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()
|
||||
@@ -42,7 +38,6 @@ 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 {
|
||||
|
||||
-27
@@ -32,18 +32,6 @@ internal class KotlinMutableExternalModelContainerImpl private constructor(
|
||||
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))
|
||||
@@ -79,21 +67,6 @@ 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))
|
||||
|
||||
-7
@@ -13,13 +13,6 @@ 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>,
|
||||
|
||||
-12
@@ -19,18 +19,6 @@ 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,
|
||||
|
||||
-5
@@ -8,8 +8,3 @@ 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)
|
||||
}
|
||||
|
||||
-13
@@ -19,19 +19,6 @@ 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?,
|
||||
|
||||
-7
@@ -12,13 +12,6 @@ interface IdeaKotlinModule : Serializable {
|
||||
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(
|
||||
override val moduleIdentifier: IdeaKotlinModuleIdentifier,
|
||||
|
||||
-24
@@ -21,30 +21,6 @@ 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?,
|
||||
|
||||
-10
@@ -16,16 +16,6 @@ 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,
|
||||
|
||||
-6
@@ -12,12 +12,6 @@ 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
|
||||
|
||||
-6
@@ -12,12 +12,6 @@ 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,
|
||||
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user