RFR: 8353835: Implement JEP 500: Prepare to Make Final Mean Final [v7]

Shaojin Wen swen at openjdk.org
Tue Oct 7 14:04:38 UTC 2025


On Sat, 4 Oct 2025 05:09:27 GMT, Alan Bateman <alanb at openjdk.org> wrote:

>> Implementation changes for [JEP 500: Prepare to Make Final Mean Final](https://openjdk.org/jeps/500).
>> 
>> Field.set (and Lookup.unreflectSetter) are changed to allow/warn/debug/deny when mutating a final instance field. JFR event recorded if final field mutated. Spec updates to Field.set, Field.setAccessible and Module.addOpens to align with the proposal in the JEP.
>> 
>> HotSpot is updated to add support for the new command line options. To aid diagnosability, -Xcheck:jni reports a fatal error when a mutating a final field with JNI, and -Xlog:jni=debug can help identity when JNI code mutates finals. For now, JNI code is allowed to set the "write-protected" fields System.in/out/err, we can re-visit once we change the System.setIn/setOut/setErr methods to not use JNI (I prefer to keep this separate to this PR because there is a small startup regression to address when changing System.setXXX).
>> 
>> There are many new tests. A small number of existing tests are changed to run /othervm as reflectively opening a package isn't sufficient. Changing the tests to /othervm means that jtreg will launch the agent with the command line options to open the package.
>> 
>> Testing: tier1-6
>
> Alan Bateman has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains 34 commits:
> 
>  - Merge branch 'master' into JDK-8353835
>  - Add test for -Xlog:jni=debug
>  - Merge branch 'master' into JDK-8353835
>  - Merge branch 'master' into JDK-8353835
>  - Improve CommandLineTest.testWarn
>  - More test cleanup
>  - Merge branch 'master' into JDK-8353835
>  - Expand jni/JNIAttachMutatorTest to final fields in named modules
>  - Merge branch 'master' into JDK-8353835
>  - Test updates based on reviewer feedback
>  - ... and 24 more: https://git.openjdk.org/jdk/compare/72319167...eed7ec4a

src/java.base/share/classes/java/lang/reflect/Field.java line 982:

> 980:         } else {
> 981:             setFinal(Reflection.getCallerClass(), obj, () -> fa.setByte(obj, b));
> 982:         }

Suggestion:

        if (!Modifier.isFinal(modifiers)) {
            fa.setByte(obj, b);
        } else {
            setFinal(Reflection.getCallerClass(), obj, () -> getOverrideFieldAccessor().setByte(obj, b));
        }

src/java.base/share/classes/java/lang/reflect/Field.java line 1027:

> 1025:         } else {
> 1026:             setFinal(Reflection.getCallerClass(), obj, () -> fa.setChar(obj, c));
> 1027:         }

Suggestion:

        if (!Modifier.isFinal(modifiers)) {
            fa.setChar(obj, c);
        } else {
            setFinal(Reflection.getCallerClass(), obj, () -> getOverrideFieldAccessor().setChar(obj, c));
        }

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25115#discussion_r2410745955
PR Review Comment: https://git.openjdk.org/jdk/pull/25115#discussion_r2410751688


More information about the hotspot-dev mailing list