Patterns for libraries to access private state of user modules

David Lloyd david.lloyd at redhat.com
Mon Nov 13 16:54:06 UTC 2017


On Mon, Nov 13, 2017 at 10:03 AM, Gunnar Morling <gunnar at hibernate.org> wrote:
> Hi,
>
> Libraries such as Hibernate often need to access private state of classes
> from other modules (e.g. when using field access for JPA entities).
>
> [...] I can't see how a utility
> method in my library could obtain the Lookup, as MethodHandles#lookup() is
> caller-sensitive and must be invoked from within the module that wishes to
> grant private access to the library.

My understanding is that you should be using
MethodHandlers.privateLookupIn(userClazz, lookup()); and that the user
module should be "open" to you (i.e. they have to opt in to granting
reflection access).  Part of this was tied up in the discussion around
the need for standardized module names for spec modules, so a user can
choose the right module name:

  opens com.mycompany.dao to java.spec.jpa;

Then you only need a Lookup from the "java.spec.jpa" module to create
the private lookup in the user class.

-- 
- DML


More information about the jigsaw-dev mailing list