MethodHandles.publicLookup().in() returns Lookup with no permissions?
Mandy Chung
mandy.chung at oracle.com
Tue Nov 24 18:39:58 UTC 2020
On 11/24/20 10:21 AM, 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.
You use MethodHandles::publicLookup which has access to unconditionally
exported classes only. It can't be used to teleport to a
qualified-exported class. You can use MethodHandles.lookup().in(klass)
instead.
> Is this intended behavior?
Yes, it's intended behavior.
Mandy
> 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.
>
> Thanks!
>
More information about the jigsaw-dev
mailing list