RFR: 8353835: Implement JEP 500: Prepare to Make Final Mean Final [v10]
Chen Liang
liach at openjdk.org
Mon Nov 10 20:27:39 UTC 2025
On Mon, 10 Nov 2025 10:06:31 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 warning and -Xlog:jni=debug logs a message to help identity JNI code that mutates finals. For now, JNI code is allowed to set the "write-protected" fields System.in/out/err without a warning, 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 45 commits:
>
> - Merge branch 'master' into JDK-8353835
> - Fix typo in test comment
> - Merge branch 'master' into JDK-8353835
> - Merge branch 'master' into JDK-8353835
> - Suppress warnings from some tests
> - Change -Xcheck:jni to be warning rather than fatal error
> - Merge branch 'master' into JDK-8353835
> - Simplify filter
> - Merge branch 'master' into JDK-8353835
> - Update Xcheck:jni description
> - ... and 35 more: https://git.openjdk.org/jdk/compare/066810c8...6671ae02
I still wonder about the decision for JNI to call final Field.set with an unconditional export check instead of an unconditional open check - the open check is done for all Java code already.
src/java.base/share/classes/java/lang/reflect/doc-files/MutationMethods.html line 56:
> 54: <li><a href="../Field.html#setDouble(java.lang.Object,double)">
> 55: java.lang.reflect.Field.setDouble(Object, double)</a></li>
> 56: <li><a href="{@docRoot}/java.base/java/lang/invoke/MethodHandles.Lookup.html#unreflectSetter(java.lang.reflect.Field)">
Nit: Since javadoc process tags here, you could just use `{@link Field#set java.lang.reflect.Field.set(Object, Object)}` instead of full-fledged a tags.
src/java.base/share/classes/java/lang/reflect/doc-files/MutationMethods.html line 66:
> 64: <p> In the reference implementation, a module can be granted the capability to mutate
> 65: final instance fields of classes in packages that are open to the module using
> 66: the command line option <code>--enable-final-field-mutation=M1,M2, ... M}</code> where
Suggestion:
the command line option <code>--enable-final-field-mutation=M1,M2, ... Mn}</code> where
src/java.base/share/classes/java/lang/reflect/doc-files/MutationMethods.html line 72:
> 70: <em>illegal</em>.
> 71:
> 72: The command line option <code>--illegal-final-field-mutation</code> controls how illegal
Missing `<p>`?
test/jdk/java/lang/reflect/Field/mutateFinals/cli/CommandLineTest.java line 234:
> 232: @Test
> 233: void testSetPropertyToAllow() throws Exception {
> 234: test("setSystemPropertyToAllow+testFieldSetInt")
I thought this was setting the property before the VM boot. Can we have another test that does something like:
test("testFieldSetInt", "-Djdk.module.illegal.final.field.mutation=allow")
Which I think is closer to what @vy asks for.
-------------
PR Review: https://git.openjdk.org/jdk/pull/25115#pullrequestreview-3444803078
PR Review Comment: https://git.openjdk.org/jdk/pull/25115#discussion_r2511838513
PR Review Comment: https://git.openjdk.org/jdk/pull/25115#discussion_r2511840059
PR Review Comment: https://git.openjdk.org/jdk/pull/25115#discussion_r2511841318
PR Review Comment: https://git.openjdk.org/jdk/pull/25115#discussion_r2511708135
More information about the core-libs-dev
mailing list