Search code examples
javajava-platform-module-system

What are the real-world use cases for java.lang.Module#addOpens(String, Module)?


I am familiar with the Java Platform Module System.

The javadoc for Module#addOpens sketches out a use case that I cannot translate into the real world:

This method can be used for cases where a consumer module uses a qualified opens to open a package to an API module but where the reflective access to the members of classes in the consumer module is delegated to code in another module. Code in the API module can use this method to open the package in the consumer module to the other module.

I confess I am too stupid to make the leap from this description to the real world. I can see that this has to do with expanding deep reflective access in some fashion, but I'm not sure what that fashion is, particularly given the method description itself:

If this module [the "consumer module"? maybe?] has opened a package to at least the caller module [the "API module"? maybe?] then update this module to open the package to the given module [the "other module"? maybe?].

I have a vague and almost certainly wrong sense this is related to, say, my module (the "consumer module"?) opening a package to something like Servlet (the "API module"?), without knowing at runtime that the implementation of Servlet in play is actually (say) Tomcat, so, if my code wishes, it (I think?) may call this method and thus somehow allow Tomcat (the "other" module?) to do reflective things without knowing that it is, in fact, Tomcat (and not, say, Jetty) doing those reflective things, and without having to have an extra opens com.foo.bar to Tomcat in module-info as well as opens com.foo.bar to Servlet.

(I'm also not sure where the "delegation" is supposed to be happening; Servlet can't "delegate" anything to Tomcat in this scenario.)

But given Lookup call site sensitivity restrictions I am not sure how this is supposed to work, or where and when in my code in this scenario I am supposed to make this sort of call, or why, if I've already added an opens statement, I must now also do something programmatic. I understand the motivating goal of integrity and that permission must be granted for reflective access and that this is supposed to somehow make things easier, but I can't get there concretely.

In short: is there actually a concrete use case for this feature? If so, what is it? How does it work?


Solution

  • The Jakarta xml bind code uses this method in jakarta.xml.bind.ModuleUtil.delegateAddOpensToImplModule

    Source code is here

    In the JDK Javadoc code TagletManager in jdk.javadoc.internal.doclets.formats.html.taglet also uses this.