Does opens/exports moduleA.X to moduleB work when moduleB is in other layer?
Alan Bateman
alan.bateman at oracle.com
Thu Jan 16 09:52:42 UTC 2025
On 16/01/2025 00:28, Code Ranger wrote:
> Please, consider the following situation:
>
> The boot layer contains moduleA, which has the following line in
> module-info: "opens com.company.a.foo to com.google.gson;"
> The child layer contains gson.
>
> And this is what I get:
>
> java.lang.reflect.InaccessibleObjectException: Unable to make field
> private ... com.company.a.foo.Bar accessible: moduleA does not "opens
> com.company.a.foo" to module com.google.gson
>
> If I do "opens com.company.a.foo;" then everything works fine, but it
> is not what I would like to do.
>
> Can someone confirm if this is expected behavior? Is this a bug, or
> does opens/exports moduleA.X to moduleB only work when moduleA and
> moduleB are in the same layer?
Assuming com.google.gson wasn't observable at compile-time, then I
assume you got a warning at compile-time too.
When creating a module layer, qualified exports/opens are reified to
export or open to the target module in the current or parent layers.
Qualified exports to modules that aren't found are ignored. The details
are specified in the ModuleLayer class description. So yes, this is
expected behavior.
Maybe someone familiar with Gson can help but it seems to use "type
adapters" for cases like this. If you really need com.google.gson in a
child layer doing deep reflection on classes in a parent or boot layer
then it require cooperation from code in the parent layer.
-Alan
More information about the jigsaw-dev
mailing list