Skip to content

Commit

Permalink
[java] move the module-info.class to the correct place
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 4, 2023
1 parent 7247f84 commit 9e2da6e
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions java/src/dev/selenium/tools/modules/ModuleGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static net.bytebuddy.jar.asm.Opcodes.ACC_STATIC_PHASE;
import static net.bytebuddy.jar.asm.Opcodes.ACC_TRANSITIVE;
import static net.bytebuddy.jar.asm.Opcodes.ASM9;
import static net.bytebuddy.jar.asm.Opcodes.V11;

import com.github.bazelbuild.rules_jvm_external.zip.StableZipEntry;
import com.github.javaparser.JavaParser;
Expand Down Expand Up @@ -319,9 +320,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
exportedPackages.stream().map(Name::new).collect(Collectors.toSet())))));

ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(
/* version 9 */
53, ACC_MODULE, "module-info", null, null, null);
classWriter.visit(V11, ACC_MODULE, "module-info", null, null, null);

This comment has been minimized.

Copy link
@joerg1985

joerg1985 Nov 4, 2023

Author Member

As the released jar's minimum java version is 11 we can set the version here to 11.

ModuleVisitor moduleVisitor = classWriter.visitModule(moduleName, isOpen ? ACC_OPEN : 0, null);
moduleVisitor.visitRequire("java.base", ACC_MANDATED, null);

Expand All @@ -334,24 +333,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.MULTI_RELEASE, "true");

This comment has been minimized.

Copy link
@joerg1985

joerg1985 Nov 4, 2023

Author Member

And we do not need to create a multi release for the module-info.class in a java 11+ world.


try (OutputStream os = Files.newOutputStream(outJar);
JarOutputStream jos = new JarOutputStream(os, manifest)) {
jos.setLevel(ZipOutputStream.STORED);

ZipEntry dir = new StableZipEntry("META-INF/");
jos.putNextEntry(dir);

dir = new StableZipEntry("META-INF/versions/");
jos.putNextEntry(dir);

dir = new StableZipEntry("META-INF/versions/9/");
jos.putNextEntry(dir);

byte[] bytes = classWriter.toByteArray();

ZipEntry entry = new StableZipEntry("META-INF/versions/9/module-info.class");
ZipEntry entry = new StableZipEntry("module-info.class");

This comment has been minimized.

Copy link
@joerg1985

joerg1985 Nov 4, 2023

Author Member

The module-info.class must be at the root in any case, see the Modular JAR files section in the JAR spec.

entry.setSize(bytes.length);

jos.putNextEntry(entry);
Expand Down

0 comments on commit 9e2da6e

Please sign in to comment.