MethodHandles.publicLookup().in() returns Lookup with no permissions?

Alan Bateman Alan.Bateman at oracle.com
Tue Nov 24 18:35:22 UTC 2020


On 24/11/2020 18:21, Simone Bordet wrote:
> Hi,
>
> testing the Jetty MethodHandle usages we encountered this situation:
>
> .class org.openjdk.mh.Main
> ----
> Class<?> klass = Main.class;
> MethodHandles.Lookup lookup = MethodHandles.publicLookup().in(klass);
> MethodHandle handle = lookup.findVirtual(klass, "test",
> MethodType.methodType(String.class));
> ----
>
> .module-info.java
> ----
> module org.openjdk.mh {
>      exports org.openjdk.mh to com.acme;
> }
> ----
>
> findVirtual() throws:
> java.lang.IllegalAccessException: symbolic reference class is not
> accessible: class org.openjdk.mh.Main, from
> org.openjdk.mh.Main/noaccess (module org.openjdk.mh)
>
> Removing the "to" clause from the exports in module-info.java fixes the issue.
>
> Seems that a class cannot get a MethodHandle on itself, the reason
> being that org.openjdk.mh is selectively exported in module-info.java.
>
> Is this intended behavior?
> I would have expected that a class could always get a MethodHandle on itself.
> I tested this with 11, 15, and 16+25, all fail.
>
> I have a simple reproducer, and I can open an OpenJDK bug.
>
It looks like you are starting with publicLookup and then teleporting to 
mh.Main that is not accessible to that lookup. When you change it from a 
qualified export  to exporting it to all modules then it becomes 
accessible to the lookup so that is why it works.

Have you tried starting with MethodHandles.lookup() ?

-Alan


More information about the jigsaw-dev mailing list