JVMS states in section 4.7.26那:
The ModulePackages attribute indicates all the packages of a module that are exported or opened by the Module attribute, as well as all the packages of the service implementations recorded in the Module attribute. The ModulePackages attribute may also indicate packages in the module that are neither exported nor opened nor contain service implementations.
在没有导出或打开的情况下添加这样的包什么时候才有意义?我看不到 Java 编译器曾经以这种方式添加包。
我偶然发现了这一点,因为 ASM 也是 offers a ModuleVisitor::visitPackage
method .
最佳答案
根据 Alan Bateman 的评论回答我自己的问题:
ModulePackages
属性是一个优化和可选的。如果该属性存在,JVM 可以通过简单地读取该属性来获取模块包的列表。如果此属性不存在,则必须扫描涉及 I/O 的 jar 文件,因此不需要。
如果包列表不完整,运行时将无法从这些包中加载类,就好像这些包不包含在 jar 中一样。
该属性由 jar 工具添加,而不是由 javac 添加。
关于java - 将未导出/未打开的包添加到模块信息的 ModulePackages 的用例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532510/