From ba3f21e125214e9a5cb37bc46b3cb809586f3c7c Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 25 Jan 2023 21:31:52 +0100 Subject: [PATCH] Minor, improve testNoDelegatedPropertiesInKClassAndKProperties Also consider delegated properties which are optimized since Kotlin 1.7.20 because of KT-23397, where backend doesn't generate a field `foo$delegate`, but generates a method `getFoo$delegate` instead. --- .../kotlin/code/ReflectionCodeSanityTest.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/repo/codebase-tests/tests/org/jetbrains/kotlin/code/ReflectionCodeSanityTest.kt b/repo/codebase-tests/tests/org/jetbrains/kotlin/code/ReflectionCodeSanityTest.kt index 3d2798ab043..ca0c831a9c3 100644 --- a/repo/codebase-tests/tests/org/jetbrains/kotlin/code/ReflectionCodeSanityTest.kt +++ b/repo/codebase-tests/tests/org/jetbrains/kotlin/code/ReflectionCodeSanityTest.kt @@ -9,6 +9,7 @@ import junit.framework.TestCase import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime import org.jetbrains.kotlin.load.java.JvmAbi import java.lang.reflect.Field +import java.lang.reflect.Member import java.lang.reflect.Modifier import kotlin.reflect.jvm.javaField @@ -44,23 +45,24 @@ class ReflectionCodeSanityTest : TestCase() { } fun testNoDelegatedPropertiesInKClassAndKProperties() { - val badFields = linkedSetOf() + val badMembers = linkedSetOf() for (klass in collectClassesWithSupers( "KClassImpl", "KMutableProperty0Impl", "KMutableProperty1Impl", "KMutableProperty2Impl" )) { - badFields.addAll(klass.declaredFields.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }) + badMembers.addAll(klass.declaredFields.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }) + badMembers.addAll(klass.declaredMethods.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }) } - if (badFields.isNotEmpty()) { - fail("The fields listed below appear to be delegates for properties.\n" + + if (badMembers.isNotEmpty()) { + fail("The members listed below appear to be delegates for properties.\n" + "It's highly not recommended to use property delegates in reflection.jvm because a KProperty instance\n" + "is created for each delegated property and that makes the initialization sequence of reflection\n" + "implementation classes unpredictable and leads to a deadlock or ExceptionInInitializerError.\n\n" + "Please un-delegate the corresponding properties:\n\n" + - badFields.joinToString("\n")) + badMembers.joinToString("\n")) } }