Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case for adding non-exported/non-opened packages to a module-info's ModulePackages?

The JVMS states in section 4.7.26 that:

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.

When would it be meaningful to add such a package when it is not exported or opened? I cannot see that the Java compiler is ever adding packages this way.

I stumbled over this since ASM also offers a ModuleVisitor::visitPackage method.

like image 532
Rafael Winterhalter Avatar asked Nov 08 '22 04:11

Rafael Winterhalter


1 Answers

To answer my own questions based on the comments of Alan Bateman:

The ModulePackages property is an optimization and optional. If the attribute is present, the JVM can get hold of a list of the module's packages by simply reading this property. If this property is not present, the jar file must be scanned what involves I/O and is therefore undesired.

If the list of packages in incomplete, the runtime will fail to load classes from these packages as if those packages were not included in the jar.

The attribute is added by the jar tool but not by javac.

like image 75
Rafael Winterhalter Avatar answered Nov 14 '22 22:11

Rafael Winterhalter