From d0383365cf9b921aad94efd0a128229c714890e1 Mon Sep 17 00:00:00 2001 From: Sergey Pospelov Date: Tue, 5 Jul 2022 10:29:45 +0300 Subject: [PATCH] Fix searching of modifiers field for JDK 8-17 --- .../src/main/kotlin/org/utbot/common/ReflectionUtil.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt b/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt index 160b0adf3f..c692c54fdb 100644 --- a/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt +++ b/utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt @@ -15,7 +15,15 @@ object Reflection { unsafe = f.get(null) as Unsafe } - private val modifiersField: Field = Field::class.java.getDeclaredField("modifiers") + + // TODO: works on JDK 8-17. Doesn't work on JDK 18 + private val modifiersField: Field = run { + val getDeclaredFields0 = Class::class.java.getDeclaredMethod("getDeclaredFields0", Boolean::class.java) + getDeclaredFields0.isAccessible = true + @Suppress("UNCHECKED_CAST") + val fields = getDeclaredFields0.invoke(Field::class.java, false) as Array + fields.first { it.name == "modifiers" } + } init { modifiersField.isAccessible = true