Java 9重叠非导出包 [英] Java 9 overlapping non-exported packages
问题描述
各种资源( infoq , jigsaw-dev , osdir )表明在不同的java模块中使用相同的包将导致 LayerInstantiationException
,即使包是模块的内部(非导出)。
这似乎与要求说:
Various resources (infoq, jigsaw-dev, osdir) indicate that having the same package in different java modules will lead to a LayerInstantiationException
, even when the packages are internal to the module (non-exported).
This seems to be the exact opposite of what the requirements say :
Java编译器,虚拟机和运行时系统必须确保包含相同名称的包的模块不会相互干扰。如果两个不同的模块包含相同名称的包,那么从每个模块的角度来看,该包中的所有类型和成员都只由该模块定义。
The Java compiler, virtual machine, and run-time system must ensure that modules that contain packages of the same name do not interfere with each other. If two distinct modules contain packages of the same name then, from the perspective of each module, all of the types and members in that package are defined only by that module.
应用程序使用的两个模块是否能够包含同名的私有包?
So are (will) two modules used by an app be able to contain private packages of the same name ?
EDIT
这是一个 issue Stanislav Lukyanov指出的JMPS> b
EDIT
This is an issue of JMPS as pointed by Stanislav Lukyanov
推荐答案
正如您所讨论的讨论所述,问题是关于类之间的映射装载机和模块。
As said in the discussions you've linked, the issue is about mapping between class loaders and modules.
当你装载两个模块 M1
和 M2
都包含一个非导出(也称为隐藏)的包 P
通过类加载器 CL
JPMS具有拒绝这样的配置,因为否则都是关键的JPMS原则 - 强封装可靠的配置 - 可能会被打破。通过在此处抛出异常,JPMS实际上实现了您引用的要求,并确保在执行期间没有任何冲突可能会破坏任何内容。
When you're loading two modules M1
and M2
both containing a non-exported (a.k.a. concealed) package P
via a class loader CL
JPMS has to reject such configuration, since otherwise both key JPMS principles - strong encapsulation and reliable configuration - could be broken. By throwing an exception here JPMS actually implements the requirement you've quoted and ensures that no conflicts may break anything later during the execution.
另一方面,当你'通过两个装载程序重新加载 M1
和 M2
CL1
和 CL2
你实际上是在创建两个运行时包 {CL1,P}
和 {CL2 ,P}
,所以没有碰撞,可以实例化 Layer
。
On the other hand, when you're loading M1
and M2
via two loaders CL1
and CL2
you're actually creating two run-time packages {CL1, P}
and {CL2, P}
, so there is no collision and the Layer
can be instantiated.
这里的可用性问题是 java
对应用程序层的所有模块使用单个加载器(起始模块,从命令行参数创建)导致 LayerInstantiationException
。这是目前JPMS列表中的一个未解决的问题,请参见[此处]( http:/ /openjdk.java.net/projects/jigsaw/spec/issues/#AvoidConcealedPackageConflicts )。但无论问题的解决方案如何,如果需要,你应该能够通过编写一个小的主类来处理拆分包,这个主类将创建你需要的配置
(BTW a JPMS) - 意识到应用程序容器可能会为你做这个。)
The usability problem here is that java
uses single loader for all modules of the application layer (the "starting" one, created from the command-line arguments) which leads to the LayerInstantiationException
. That's currently an open issue on the JPMS list, see [here] (http://openjdk.java.net/projects/jigsaw/spec/issues/#AvoidConcealedPackageConflicts). But regardless of the issue's resolution, if needed, you should be able to deal with the split packages by writing a tiny main class that will create the Configuration
you need (BTW a JPMS-aware application container will probably do it for you).
这篇关于Java 9重叠非导出包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!