RFR: 8372696: Allow boot classes to explicitly opt-in for final field trusting

Chen Liang liach at openjdk.org
Fri Nov 28 15:08:56 UTC 2025


On Fri, 28 Nov 2025 13:45:19 GMT, Jaikiran Pai <jpai at openjdk.org> wrote:

>> Currently, the hotspot compiler (as in ciField) trusts final fields in hidden classes, record classes, and selected jdk packages. Some classes in the JDK wish to be trusted, but they cannot apply package-wide opt-in due to other legacy classes in the package, such as java.util.
>> 
>> They currently can use `@Stable` as a workaround, but this is fragile because a stable final field may hold a trusted null, zero, or false value, which is currently treated as non-constant by ciField.
>> 
>> We should add an annotation to opt-in for a whole class, mainly for legacy packages. This would benefit greatly some of our classes already using a lot of Stable, such as java.util.Optional, whose empty instance is now constant-foldable, as demonstrated in a new IR test.
>> 
>> Paging @minborg who requested Optional folding for review.
>> 
>> I think we can remove redundant Stable in a few other java.util classes after this patch is integrated. I plan to do that in subsequent patches.
>
> src/java.base/share/classes/jdk/internal/vm/annotation/TrustFinalFields.java line 49:
> 
>> 47: /// As a result, this should be used on classes where package-wide trusting is
>> 48: /// not possible due to backward compatibility concerns, such as for `java.util`
>> 49: /// classes.
> 
> Should this sentence be reworded? It's not clear what the backward compatible concerns (for `java.util` package) are. I think it might be better to leave out any backward compatibility part when explaining which classes to use this annotation on.

Existing users have been hacking java.util final fields. I think leaving out the backward compatibility part causes more trouble, because otherwise people can just blanket-approve java.util classes for trusting and break those applications.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/28540#discussion_r2571941033


More information about the hotspot-dev mailing list