On Tue, 23 Sep 2025 14:39:34 GMT, Vladimir Ivanov <[email protected]> wrote:

>> src/hotspot/share/runtime/fieldDescriptor.cpp line 53:
>> 
>>> 51:   // write protected fields (JLS 17.5.4)
>>> 52:   if (is_final() && is_static() && ik == vmClasses::System_klass() &&
>>> 53:       (offset() == java_lang_System::in_offset() || offset() == 
>>> java_lang_System::out_offset() || offset() == 
>>> java_lang_System::err_offset())) {
>> 
>> I thought the ability to mutate these fields was restricted to the native 
>> implementations of `setIn` et al. and was not allowed via regular 
>> reflection? Otherwise how can these fields be considered as "trusted 
>> finals"??
>
>> Otherwise how can these fields be considered as "trusted finals"??
> 
> FTR `System.in` et al are not considered as trusted finals. There's a special 
> logic in `ciField::initialize_from()` to disable corresponding optimizations, 
> so the fields aren't treated as constants.

> I thought the ability to mutate these fields was restricted to the native 
> implementations of setIn et al. and was not allowed via regular reflection?

Right, core reflection can't modify static finals. JLS 17.5.4 labels 
System.in/out/err as write-protected fields that can be modified by the 
setIn/setOut/setErr methods. Historical and unfortunate, and works because 
these methods were always in JNI. The existence of these methods means the VM 
can't trust these fields and so has to special case them.

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

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

Reply via email to