From 34c82011fc2fd169315ffb14a5a57940d46f9ba1 Mon Sep 17 00:00:00 2001 From: Leonid Shalupov Date: Wed, 29 Mar 2023 20:20:02 +0100 Subject: [PATCH] Remove copying of transient fields in argumentUtils Current code in argumentUtils handles transient fields incorrectly and copies them (not intended behaviour) change is required for further work in arguments handling ref https://jetbrains.team/im/review/3CCOfH4ffz5J?message=9fPd30T1rQz&channel=1y9ZTj2JB0aG --- .../cli/common/arguments/argumentUtils.kt | 5 +-- .../jetbrains/kotlin/cli/ArgumentUtilTest.kt | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 compiler/tests-common/tests/org/jetbrains/kotlin/cli/ArgumentUtilTest.kt diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt index 0650334d157..1bde84de251 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt @@ -17,13 +17,14 @@ package org.jetbrains.kotlin.cli.common.arguments import com.intellij.util.text.VersionComparatorUtil -import java.util.* +import java.lang.reflect.Modifier import kotlin.reflect.KClass import kotlin.reflect.KMutableProperty1 import kotlin.reflect.KProperty1 import kotlin.reflect.KVisibility import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.memberProperties +import kotlin.reflect.jvm.javaField @Suppress("UNCHECKED_CAST") fun copyBean(bean: T) = copyBeanTo(bean, bean::class.java.newInstance()!!) @@ -103,7 +104,7 @@ fun collectProperties(kClass: KClass, inheritedOnly: Boolean): List properties.removeAll(kClass.declaredMemberProperties) } return properties.filter { property -> - property.visibility == KVisibility.PUBLIC && (property.annotations.firstOrNull { it is Transient } as Transient?) == null + property.visibility == KVisibility.PUBLIC && (property.javaField?.modifiers?.let { Modifier.isTransient(it) } != true) } } diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/cli/ArgumentUtilTest.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/cli/ArgumentUtilTest.kt new file mode 100644 index 00000000000..a73c80794ac --- /dev/null +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/cli/ArgumentUtilTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2023 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.cli + +import junit.framework.TestCase +import org.jetbrains.kotlin.cli.common.arguments.ArgumentParseErrors +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.copyBeanTo +import java.lang.reflect.Modifier +import kotlin.reflect.jvm.javaField + +class ArgumentUtilTest : TestCase() { + fun testCopyDoesNotCopyTransientFields() { + assertTrue(Modifier.isTransient(K2JVMCompilerArguments::errors.javaField!!.modifiers)) + + val a = K2JVMCompilerArguments() + a.errors = ArgumentParseErrors() + a.moduleName = "my module name" + + val b = K2JVMCompilerArguments() + assertNull(b.errors) + assertNull(b.moduleName) + + copyBeanTo(a, b) + + assertNull(b.errors) + assertEquals("my module name", b.moduleName) + } +} \ No newline at end of file