RFR: JDK-8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
Adam Farley8
adam.farley at uk.ibm.com
Fri Jan 11 14:38:46 UTC 2019
Hi All,
First; thanks for responding so quickly. :)
Second, it sounds like changing the contents of a final field is
acceptable under the spec, but *not* changing the contents of a static
final field.
The test case uploaded to the bug uses a static final field for the test,
so I think we can agree that the ability to create a setter for a static
final field is still a bug.
It sounds like the same fix could work if we check for "static" when we
check for "final", with a modified message.
As Field.set (post-setAccessible) fails to set the value of a static final
field, I think we can agree that unreflectSetter should fail to create a
setter MethodHandle for this same field.
Best Regards
Adam Farley
IBM Runtimes
Remi Forax <forax at univ-mlv.fr> wrote on 11/01/2019 13:11:48:
> From: Remi Forax <forax at univ-mlv.fr>
> To: David Holmes <david.holmes at oracle.com>
> Cc: Adam Farley8 <adam.farley at uk.ibm.com>, core-libs-dev <core-libs-
> dev at openjdk.java.net>
> Date: 11/01/2019 13:11
> Subject: Re: RFR: JDK-8216558: Lookup.unreflectSetter(Field) fails
> to throw IllegalAccessException for final fields
>
>
>
> ----- Mail original -----
> > De: "David Holmes" <david.holmes at oracle.com>
> > À: "Adam Farley8" <adam.farley at uk.ibm.com>, "core-libs-dev" <core-
> libs-dev at openjdk.java.net>
> > Envoyé: Vendredi 11 Janvier 2019 14:02:06
> > Objet: Re: RFR: JDK-8216558: Lookup.unreflectSetter(Field) fails
> to throw IllegalAccessException for final fields
>
> > 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.
> >
> > See:
> >
> > INVALID URI REMOVED
>
u=https-3A__docs.oracle.com_javase_10_docs_api_java_lang_reflect_Field.html-23set-28java.lang.Object-2Cjava.lang.Object-29&d=DwIFaQ&c=jf_iaSHvJObTbx-
> siA1ZOg&r=P5m8KWUXJf-
>
CeVJc0hDGD9AQ2LkcXDC0PMV9ntVw5Ho&m=aBMrDjHarHyUonopC18wEF8NpJy49nWWHAa5zLG7DPA&s=IjUETWTkkQMUCoCFwBXi-
> o8mQuUKzUKO550ntJT6f6c&e=
> >
> > "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.
>
> No VH doesn't support setAccessible (don't ask me why) ...
>
> >
> > 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).
> >
> > David
>
> Rémi
>
> > -----
> >
> >> webrev: INVALID URI REMOVED
>
u=http-3A__cr.openjdk.java.net_-7Eafarley_8216558_webrev_&d=DwIFaQ&c=jf_iaSHvJObTbx-
> siA1ZOg&r=P5m8KWUXJf-
>
CeVJc0hDGD9AQ2LkcXDC0PMV9ntVw5Ho&m=aBMrDjHarHyUonopC18wEF8NpJy49nWWHAa5zLG7DPA&s=her2-
> KtbTJb51VZ58GcKJXMpC6zPiEWqkWeJU9zu-IQ&e=
> >> bug: INVALID URI REMOVED
>
u=https-3A__bugs.openjdk.java.net_browse_JDK-2D8216558&d=DwIFaQ&c=jf_iaSHvJObTbx-
> siA1ZOg&r=P5m8KWUXJf-
>
CeVJc0hDGD9AQ2LkcXDC0PMV9ntVw5Ho&m=aBMrDjHarHyUonopC18wEF8NpJy49nWWHAa5zLG7DPA&s=xxfE7kYjXorAaobSj5gl8uwHLfJMcWYG5IXcrNH-
> wR4&e=
> >>
> >> 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
>
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