Address review comments

This commit is contained in:
Christian Stein 2026-01-26 09:31:13 +01:00
parent 9ca4567d27
commit 54f12744b6
3 changed files with 41 additions and 37 deletions

View File

@ -102,6 +102,7 @@ final class Validator {
this.zf = zf;
this.zis = zis;
checkModuleDescriptor(MODULE_INFO);
checkAutomaticModuleName();
}
static boolean validate(Main main, File zipFile) throws IOException {
@ -274,26 +275,6 @@ final class Validator {
errorAndInvalid(formatMsg("error.validator.metainf.wrong.position", firstName));
}
}
// Check for a valid and consistent automatic module name
try (InputStream jis = zf.getInputStream(zf.getEntry(entryName))) {
Manifest manifest = new Manifest(jis);
Attributes global = manifest.getMainAttributes();
String name = global.getValue("Automatic-Module-Name");
if (name != null) {
try {
ModuleDescriptor.newAutomaticModule(name);
} catch (IllegalArgumentException e) {
errorAndInvalid(formatMsg("error.validator.manifest.invalid.automatic.module.name", name));
}
if (md != null) {
if (!name.equals(md.name())) {
errorAndInvalid(formatMsg("error.validator.manifest.inconsistent.automatic.module.name", name, md.name()));
}
}
}
} catch (IOException e) {
errorAndInvalid(e.getMessage());
}
}
}
@ -475,6 +456,36 @@ final class Validator {
});
}
/**
* Checks whether an Automatic-Module-Name entry is valid
* and also verifies it to the name given by a compiled
* module descriptor.
*/
private void checkAutomaticModuleName() {
var entry = zf.getEntry("META-INF/MANIFEST.MF");
if (entry == null) {
return;
}
try (InputStream jis = zf.getInputStream(entry)) {
Attributes attributes = new Manifest(jis).getMainAttributes();
String automaticModuleName = attributes.getValue("Automatic-Module-Name");
if (automaticModuleName == null) {
return;
}
try {
ModuleDescriptor.newAutomaticModule(automaticModuleName);
} catch (IllegalArgumentException e) {
errorAndInvalid(formatMsg("error.validator.manifest.invalid.automatic.module.name", automaticModuleName));
}
if (md == null || automaticModuleName.equals(md.name())) {
return;
}
errorAndInvalid(formatMsg("error.validator.manifest.inconsistent.automatic.module.name", automaticModuleName, md.name()));
} catch (IOException e) {
errorAndInvalid(e.getMessage());
}
}
/*
* Checks whether or not the given versioned module descriptor's attributes
* are valid when compared against the root/base module descriptor.

View File

@ -141,7 +141,7 @@ error.validator.manifest.wrong.position=\
error.validator.manifest.invalid.automatic.module.name=\
invalid module name of Automatic-Module-Name entry in manifest: {0}
error.validator.manifest.inconsistent.automatic.module.name=\
expected module name is: {1} - but found Automatic-Module-Name entry in manifest: {0}
expected Automatic-Module-Name entry in manifest: {0} to match name of compiled module: {1}
warn.validator.identical.entry=\
Warning: entry {0} contains a class that\n\
is identical to an entry already in the jar

View File

@ -36,6 +36,7 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@ -318,14 +319,10 @@ class ValidatorTest {
Automatic-Module-Name: default
""");
jar("--create --file " + file + " --manifest " + manifest);
try {
jar("--validate --file " + file.toString());
fail("Expecting non-zero exit code");
} catch (IOException e) {
var err = e.getMessage();
System.out.println(err);
assertTrue(err.contains("invalid module name of Automatic-Module-Name entry in manifest: default"), "missing warning for: default");
}
var e = assertThrows(IOException.class, () -> jar("--validate --file " + file.toString()));
var err = e.getMessage();
System.out.println(err);
assertTrue(err.contains("invalid module name of Automatic-Module-Name entry in manifest: default"), "missing warning for: default");
}
@Test
@ -344,14 +341,10 @@ class ValidatorTest {
""");
JAVAC_TOOL.run(System.out, System.err, foo.toString());
jar("--create --file " + file + " --manifest " + manifest + " module-info.class");
try {
jar("--validate --file " + file.toString());
fail("Expecting non-zero exit code");
} catch (IOException e) {
var err = e.getMessage();
System.out.println(err);
assertTrue(err.contains("expected module name is: foo - but found Automatic-Module-Name entry in manifest: bar"), "missing warning for: foo/bar");
}
var e = assertThrows(IOException.class, () -> jar("--validate --file " + file.toString()));
var err = e.getMessage();
System.out.println(err);
assertTrue(err.contains("expected Automatic-Module-Name entry in manifest: bar to match name of compiled module: foo"), "missing warning for: foo/bar");
}
/**