Hi Martin,

Have you tried using:

 MethodHandles.privateLookupIn(ConcurrentLinkedQueue.class, 
MethodHandles.lookup()).
    findVarHandle(…)

?

I agree there is some inconsistency here, and we wanted to discourage the use 
of setAccessible. One problem is setAccessible conflates accessibility with 
stomping on final fields. If we do allow setAccessible access to work for 
unreflecting i would still not want to allow VarHandles to stomp on final 
fields, so some inconsistency would remain.

My preference would be to encourage people to use MethodHandles.privateLookupIn.

Paul.


> On 9 Jan 2017, at 10:43, Martin Buchholz <marti...@google.com> wrote:
> 
> Relatedly, I'm writing whitebox jtreg tests and would like to use VarHandles 
> to access internal data structures.
> Because I have:
> 
>  * @modules java.base/java.util.concurrent:open
> 
> I can use reflection with setAccessible to obtain a usable Field, but if I 
> try to turn that into a VarHandle I get:
> 
>         Field f = ConcurrentLinkedQueue.class.getDeclaredField("head");
>         f.setAccessible(true);
>         VarHandle v = java.lang.invoke.MethodHandles.lookup()
>             .unreflectVarHandle(f);
> 
>  java.lang.IllegalAccessException: member is private to package: 
> java.util.concurrent.ConcurrentLinkedQueue.head/java.util.concurrent.ConcurrentLinkedQueue$Node/getField,
>  from WhiteBox (unnamed module @7a187f14)
> 
> It seems wrong to have this sort of mismatch - setAccessible works, but the 
> strictly more powerful VarHandles remain unobtainable.
> 
> I happen to have a workaround, but I don't see a general solution for 
> whitebox testing.

Reply via email to