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.
This commit is contained in:
committed by
Space Team
parent
99b38ccb74
commit
ba3f21e125
@@ -9,6 +9,7 @@ import junit.framework.TestCase
|
|||||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
||||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||||
import java.lang.reflect.Field
|
import java.lang.reflect.Field
|
||||||
|
import java.lang.reflect.Member
|
||||||
import java.lang.reflect.Modifier
|
import java.lang.reflect.Modifier
|
||||||
import kotlin.reflect.jvm.javaField
|
import kotlin.reflect.jvm.javaField
|
||||||
|
|
||||||
@@ -44,23 +45,24 @@ class ReflectionCodeSanityTest : TestCase() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun testNoDelegatedPropertiesInKClassAndKProperties() {
|
fun testNoDelegatedPropertiesInKClassAndKProperties() {
|
||||||
val badFields = linkedSetOf<Field>()
|
val badMembers = linkedSetOf<Member>()
|
||||||
for (klass in collectClassesWithSupers(
|
for (klass in collectClassesWithSupers(
|
||||||
"KClassImpl",
|
"KClassImpl",
|
||||||
"KMutableProperty0Impl",
|
"KMutableProperty0Impl",
|
||||||
"KMutableProperty1Impl",
|
"KMutableProperty1Impl",
|
||||||
"KMutableProperty2Impl"
|
"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()) {
|
if (badMembers.isNotEmpty()) {
|
||||||
fail("The fields listed below appear to be delegates for properties.\n" +
|
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" +
|
"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" +
|
"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" +
|
"implementation classes unpredictable and leads to a deadlock or ExceptionInInitializerError.\n\n" +
|
||||||
"Please un-delegate the corresponding properties:\n\n" +
|
"Please un-delegate the corresponding properties:\n\n" +
|
||||||
badFields.joinToString("\n"))
|
badMembers.joinToString("\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user