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