Skip to content

Commit a92e289

Browse files
Fix searching of modifiers field for JDK 8-17
1 parent 9a3acb6 commit a92e289

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

utbot-core/src/main/kotlin/org/utbot/common/ReflectionUtil.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ object Reflection {
1515
unsafe = f.get(null) as Unsafe
1616
}
1717

18-
private val modifiersField: Field = Field::class.java.getDeclaredField("modifiers")
18+
19+
// TODO: works on JDK 8-17. Doesn't work on JDK 18
20+
private val modifiersField: Field = run {
21+
val getDeclaredFields0 = Class::class.java.getDeclaredMethod("getDeclaredFields0", Boolean::class.java)
22+
getDeclaredFields0.isAccessible = true
23+
@Suppress("UNCHECKED_CAST")
24+
val fields = getDeclaredFields0.invoke(Field::class.java, false) as Array<Field>
25+
fields.first { it.name == "modifiers" }
26+
}
1927

2028
init {
2129
modifiersField.isAccessible = true

0 commit comments

Comments
 (0)