'Is it possible to set private static final field using Reflection in JDK 17 [duplicate]

I am trying to upgrade my application Java version from v1.8 to v17. There are some unit tests which are using Reflection to set value of private, static, final fields. I tried using VarHandle but no luck.

@user207421 agree could have added more details. Whatever threads are marked as possible duplicate of not solving my problem. And there are mixed responses to the question. Some say can not done and period. More important is version of Java with respect to which they are suggesting the solution is not clear. We have a working code which sets static final field values using Reflection. We use it only for tests. Now we are upgrading Java from v1.8 to v17 any approach which uses Field.class.getDeclaredField("modifiers"); will not work. I tried using VarHandle in conjunction with setting --add-open to VM arg but it makes my runtime to crash. I will post my code sameple shortly .. any ideas would help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source