6

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • 2
    ModulePackages is an optimization. Consider a modular JAR on the module path. If module-info doesn't have a ModulePackages attribute then the JAR file needs to be scanned to determine the packages in the module. If the ModulePackages attribute is present then it avoids this scan. The `jar` tool will add and update it when creating or updating a modular JAR. – Alan Bateman Jul 26 '18 at 09:45
  • But would the runtime not need to apply a consistency check of this attribute against the actual contents? Doing so, a full scan was still needed. Or is it a runtime error if the attribute was present and a class of an unlisted package waa loaded. The attribute would for example be inaccurate if someone bundled dependencies using maven-shade. – Rafael Winterhalter Jul 26 '18 at 10:19
  • 2
    The value of ModulePackages needs to be accurate, there is no scanning when this attribute is present. If classes are added to a module JAR in a package that is not listed in the attribute then they will not be found. Tools or plugins doing this must update the value of the ModulePackages attribute. – Alan Bateman Jul 26 '18 at 10:33
  • Thanks for the clarificatiion. Is that something the JVMS should mention? – Rafael Winterhalter Jul 26 '18 at 10:41

1 Answers1

2

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192