RFR: JDK-8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields

David Holmes david.holmes at oracle.com
Fri Jan 11 13:02:06 UTC 2019

On 11/01/2019 10:46 pm, Adam Farley8 wrote:
> Hi All,
> I posit that you shouldn't be able to change the contents of a a final
> field.

Nice in theory, but not true in practice.

> However, if you use Field.setAccessible(true) before calling
> Lookup.unreflectSetter(Field), then you can get a MethodHandle that allows
> you to set the (reflected) value of a static final field.
> This seems wrong to me, so here's a change to fix it.



"If the underlying field is final, the method throws an 
IllegalAccessException unless setAccessible(true) has succeeded for this 
Field object and the field is non-static. Setting a final field in this 
way is meaningful only during deserialization or reconstruction of 
instances of classes with blank final fields, before they are made 
available for access by other parts of a program. Use in any other 
context may have unpredictable effects, including cases in which other 
parts of a program continue to use the original value of this field. "

the same semantics are supported by MH/VH.

But note this requires the field to be non-static - if you can set a 
static final field this way then that may be a bug (they can still be 
set via native methods of course).


> webrev: http://cr.openjdk.java.net/~afarley/8216558/webrev/
> bug: https://bugs.openjdk.java.net/browse/JDK-8216558
> Opinions invited.
> Best Regards
> Adam Farley
> IBM Runtimes
> Unless stated otherwise above:
> IBM United Kingdom Limited - Registered in England and Wales with number
> 741598.
> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

More information about the core-libs-dev mailing list