RFR: 8353835: Implement JEP 500: Prepare to Make Final Mean Final

Shaojin Wen swen at openjdk.org
Tue Sep 23 06:47:37 UTC 2025


On Thu, 8 May 2025 11:22:30 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

Duplicate Code in Field.java
In the java.lang.reflect.Field class, the set* methods (such as set, setBoolean, setByte, etc.) contain a lot of duplicate code for handling access control of final fields.

Recommendation: Consider creating a generic helper method to handle the access control check for final fields, thus reducing code duplication.


    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void set(Object obj, Object value)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .set(obj, value));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setBoolean(Object obj, boolean z)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setBoolean(obj, z));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setByte(Object obj, byte b)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setByte(obj, b));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setChar(Object obj, char c)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setChar(obj, c));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setShort(Object obj, short s)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setShort(obj, s));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setInt(Object obj, int i)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setInt(obj, i));
    }

 
    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setLong(Object obj, long l)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setLong(obj, l));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setFloat(Object obj, float f)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setFloat(obj, f));
    }

    @CallerSensitive
    @ForceInline // to ensure Reflection.getCallerClass optimization
    public void setDouble(Object obj, double d)
        throws IllegalArgumentException, IllegalAccessException
    {
        handleFinalFieldModification(
                Reflection.getCallerClass(),
                obj,
                () -> getOverrideFieldAccessor()
                        .setDouble(obj, d));
    }

    private interface Setter {
        void run() throws IllegalAccessException;
    }

    @ForceInline
    private void handleFinalFieldModification(Class<?> callerClass, Object obj, Setter setter)
            throws IllegalAccessException {
        if (!override) {
            checkAccess(callerClass, obj);
            setter.run();
            return;
        }

        if (!isFinalInstanceInNormalClass()) {
            setter.run();
            return;
        }

        // final field in normal class handling
        preSetFinal(callerClass, obj);
        setter.run();
        postSetFinal(callerClass);
    }

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

PR Comment: https://git.openjdk.org/jdk/pull/25115#issuecomment-3322635579


More information about the core-libs-dev mailing list