Generic MethodHandle.lookup utility method and modules

mandy.chung at oracle.com mandy.chung at oracle.com
Thu Aug 25 23:25:00 UTC 2022


When you teleport the lookup from module R class C to another module M 
class D, the cross-module access check ensures that the resulting Lookup 
has access to public types that both R/C and M/D can access (i.e. their 
intersection).

The table in the cross-module access checks section at:
https://download.java.net/java/early_access/jdk20/docs/api/java.base/java/lang/invoke/MethodHandles.Lookup.html#module-access-check

describes the allowed modes of the resulting Lookup after teleporting.

On 8/25/22 11:24 AM, Rudy De Busscher wrote:
> Hi All,
>
> I'm playing around with MethodHandle.lookup and LambdaMetafactory to 
> have some generic utility method to access fields (through the getters 
> and setters) of some arbitrary class. (as alternative for reflection 
> as this should be faster)
>
> Code is working fine in classpath mode, but when using Java modules, I 
> get errors saying 'access to public member failed' although module 
> info exports the class.
>
> I believe this is by design because I read the following sentence in 
> the javadoc of MethodHandles.Lookup "*Teleporting to some third module 
> drops all accesses. *"
>
Yes.   Teleporting across modules can only decrease access but cannot 
increase it.


> I seek confirmation that it is indeed not possible what I'm trying 
> (JDK 11/JDK 17)
>
> _Setup_
>
> Module R, Class with calls to MethodHandle.lookup and LambdaMetafactory
>
> Module M, contains simple pojos, public setters and getters for each 
> property. module info contains exports and open clauses for the pojo 
> package(s).
>
> Module App contains the main method, calling the utility class in 
> Module R with Pojos of Module M.
>
> _Code_
>
> Since I have
>
> /MethodHandles.Lookup lookup = 
> MethodHandles.publicLookup().in(beanClass);/
>

A public Lookup has UNCONDITIONAL mode that can access public type in 
all modules when the type is in a package that is exported unconditionally.

Teleporting to a different module does not change its allowed mode.


> Within a class of Module R, called from Module App, where beanClass is 
> a class within M I assume I get this error due to the 'teleporting to 
> third module'
>
> access to public member failed: 
> be.atbash.poc.reflection.Pojo.getData()String/invokeVirtual, from 
> be.atbash.json.accessor.FastPropertyMemberAccessor (module reflection)
>
>
Is be.atbash.poc.reflection.Pojo exported unconditionally?


> When I use
>
>
> MethodHandles.Lookup lookup = MethodHandles.lookup().in(beanClass);
>
> I have as error
>
>
> symbolic reference class is not accessible: class 
> be.atbash.poc.reflection.Pojo, from 
> be.atbash.poc.reflection.Pojo/noaccess (module model)
>
>
> which also indicate the access check is an issue ( xxx/noaccess)
>

Is this error message from JDK 11?  Is it a different message when 
running with JDK 17?

Mandy


>
> _Question_
>
>
> Thanks for any additional insight I might miss or confirmation it is 
> not possible what I try when using Modules.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jigsaw-dev/attachments/20220825/65d1edd5/attachment.htm>


More information about the jigsaw-dev mailing list