Java 9 overlapping non-exportable packages

Different resources ( infoq , jigsaw-dev , osdir ) show that having the same package in different java modules will result in a LayerInstantiationException , even if the packages are internal to the module (unexported).
This seems to be the exact opposite of requirements:

The Java compiler, virtual machine, and runtime system must ensure that modules containing packages with the same name do not interfere with each other. If two different modules contain packages with the same name, then from the point of view of each module, all types and members of this package are defined only by this module.

So what (will) two modules used by the application can contain private packages with the same name?

EDIT
This is a JMPS issue , as Stanislav Lukyanov pointed out.

+6
source share
2 answers

As stated in the discussions you are discussing, the problem is the mapping between class loaders and modules.

When you load two modules M1 and M2 , both of which contain a non-exportable (aka hidden) package P through the CL class loader, JPMS should reject this configuration, because otherwise both key JPMS principles - strong encapsulation and reliable configuration - can be broken. Throwing an exception, here JPMS actually implements the requirement you specified and ensures that no conflicts can break anything later at runtime.

On the other hand, when you boot M1 and M2 through two boot loaders CL1 and CL2 , you actually create two runtime packages {CL1, P} and {CL2, P} , so there is no collision there, and Layer can be created.

The usability problem is that java uses a single bootloader for all application-level modules ("initial", created from command line arguments), which leads to a LayerInstantiationException . This is currently an open issue on the JPMS list, see here [here] ( http://openjdk.java.net/projects/jigsaw/spec/issues/#AvoidConcealedPackageConflicts ). But regardless of the resolution of the problem, if necessary, you should be able to deal with shared packages by writing a tiny main class that will create you Configuration (by the way, you will need an application container that supports JPMS).

+5
source

This definition is open to interpretation. It remains true, because Jigsaw ensures that the two modules will never determine common packages, class loading fails in the event of such a conflict.

If you look at the implementation of the Java 9 class loader, you will see that the package name is displayed by one module. Therefore, it is impossible for two to have two modules for declaring ownership. However, it is possible that two class loaders in the child and parent relationships define the same package.

+1
source

Source: https://habr.com/ru/post/1012353/


All Articles