mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Let modules declare optional packages that need no missing-class warning
A module whose bundled library references optional integrations got a
WARNING on every pack load, with no way to say the classes are expected
to be absent. ModuleInfo now carries optionalPackages, set with
withOptionalPackages("org.slf4j", ...); ModuleInfo.of(id, name, version)
still declares none. Each package covers its subpackages.
Missing classes of a jar inside its own module's optional packages are
dropped from the warning and from PackModules.missingClasses(); other
jars are unaffected. The declaration comes from info(), so it is applied
after the entry classes exist. Load errors (hidden Iris classes, classes
missing from exposed packages) are decided before that and never depend
on it.
Package-name validation and whole-package matching move from
ExposedPackages to a new API value, PackageName, used by both.
This commit is contained in:
1 parent
3873894a22
commit
57774fade0
8 files changed
+241
-41
No files matched your search
@@ -41,6 +41,23 @@ class ApiValuesTest {
|
||||
assertThrows(NullPointerException.class, () -> ModuleInfo.of("example:moss", null, "1.0.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void moduleInfoDeclaresOptionalPackagesWithTheirSubpackages() {
|
||||
var plain = ModuleInfo.of("example:moss", "Moss", "1.0.0");
|
||||
var info = plain.withOptionalPackages("org.slf4j", "org.apache.logging.");
|
||||
|
||||
assertEquals(List.of(), plain.optionalPackages());
|
||||
assertEquals(List.of(new PackageName("org.slf4j"), new PackageName("org.apache.logging")), info.optionalPackages());
|
||||
assertEquals(List.of(), info.withOptionalPackages().optionalPackages());
|
||||
assertTrue(info.optionalPackages().getFirst().contains("org.slf4j.spi.LocationAwareLogger"));
|
||||
assertFalse(info.optionalPackages().getFirst().contains("org.slf4jx.Logger"));
|
||||
for (var invalid : List.of("", "org.*", "org..slf4j", "1org")) {
|
||||
var failure = assertThrows(IllegalArgumentException.class, () -> plain.withOptionalPackages(invalid));
|
||||
assertTrue(failure.getMessage().startsWith("Optional package '" + invalid + "' of module example:moss is not a package name"),
|
||||
failure.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void moduleInfoTextFitsOnOneLogLine() {
|
||||
for (var broken : List.of("Moss\nINFO forged line", "Moss\r", "Moss\tTabbed", "Moss\u2028Next")) {
|
||||
|
||||
@@ -49,6 +49,21 @@ class PackModulesTest {
|
||||
return TestModuleJar.compileClass(pack, SHARED_CLASS, SHARED_SOURCE);
|
||||
}
|
||||
|
||||
/** A jar of empty public classes, for modules to compile against without shipping it. */
|
||||
private static Path library(Path build, String jarName, String... classNames) {
|
||||
var library = new TestModuleJar();
|
||||
for (var className : classNames) {
|
||||
int dot = className.lastIndexOf('.');
|
||||
library.source(className, "package %s; public final class %s {}".formatted(className.substring(0, dot), className.substring(dot + 1)));
|
||||
}
|
||||
return library.writeInto(build, jarName);
|
||||
}
|
||||
|
||||
/** Module members that use each class, so its class file references them. */
|
||||
private static String using(String... classNames) {
|
||||
return "static Object[] used() { return new Object[] {" + String.join(".class, ", classNames) + ".class}; }";
|
||||
}
|
||||
|
||||
private static String failure(Executable load) {
|
||||
return assertThrows(IllegalArgumentException.class, load).getMessage();
|
||||
}
|
||||
@@ -294,6 +309,70 @@ class PackModulesTest {
|
||||
assertFalse(message.contains(OPTIONAL_CLASS), message);
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingClassesInDeclaredOptionalPackagesDoNotWarn(@TempDir Path build) {
|
||||
module("pack.user.User", "user:core", List.of("org.optional", "org.other."), using(OPTIONAL_CLASS, "org.other.deep.Thing"), NO_STAGES)
|
||||
.compileAgainst(library(build, "optional.jar", OPTIONAL_CLASS, "org.other.deep.Thing"))
|
||||
.writeInto(pack, "user.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(List.of(), packModules.missingClasses());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingClassesOutsideDeclaredOptionalPackagesStillWarn(@TempDir Path build) {
|
||||
module("pack.user.User", "user:core", List.of("org.optional"), using(OPTIONAL_CLASS, "org.optionalx.Other", "org.slf4j.Logger"), NO_STAGES)
|
||||
.compileAgainst(library(build, "optional.jar", OPTIONAL_CLASS, "org.optionalx.Other", "org.slf4j.Logger"))
|
||||
.writeInto(pack, "user.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(List.of(new MissingClasses(origin("user.jar"), List.of("org.optionalx.Other", "org.slf4j.Logger"))),
|
||||
packModules.missingClasses());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionalPackagesExcuseOnlyTheirOwnJar(@TempDir Path build) {
|
||||
var optionalLibrary = library(build, "optional.jar", OPTIONAL_CLASS);
|
||||
module("pack.alpha.Alpha", "alpha:core", List.of("org.optional"), using(OPTIONAL_CLASS), NO_STAGES)
|
||||
.compileAgainst(optionalLibrary)
|
||||
.writeInto(pack, "alpha.jar");
|
||||
module("pack.beta.Beta", "beta:core", using(OPTIONAL_CLASS), NO_STAGES)
|
||||
.compileAgainst(optionalLibrary)
|
||||
.writeInto(pack, "beta.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
assertEquals(List.of(new MissingClasses(origin("beta.jar"), List.of(OPTIONAL_CLASS))), packModules.missingClasses());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionalPackagesNeverExcuseALoadError(@TempDir Path build) {
|
||||
var hidden = pack.resolve("hidden");
|
||||
module("pack.user.User", "user:core", List.of("org.junit"), using("org.junit.jupiter.api.Test"), NO_STAGES)
|
||||
.compileAgainstHostOf(Test.class)
|
||||
.writeInto(hidden, "user.jar");
|
||||
assertMentions(failure(() -> PackModules.load(hidden)),
|
||||
"uses classes that exist in Iris but are hidden from modules:\n pack.user.User -> org.junit.jupiter.api.Test");
|
||||
|
||||
var newerApi = pack.resolve("newer-api");
|
||||
module("pack.user.User", "user:core", List.of("com.volmit"), using("com.volmit.Newer"), NO_STAGES)
|
||||
.compileAgainst(library(build, "newer-api.jar", "com.volmit.Newer"))
|
||||
.writeInto(newerApi, "user.jar");
|
||||
assertMentions(failure(() -> PackModules.load(newerApi)),
|
||||
"uses classes that this Iris does not have, in packages Iris provides to modules:\n pack.user.User -> com.volmit.Newer");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidOptionalPackageFailsNamingTheJar() {
|
||||
module("pack.user.User", "user:core", List.of("org.*"), NO_MEMBERS, NO_STAGES).writeInto(pack, "user.jar");
|
||||
|
||||
assertMentions(failure(() -> PackModules.load(pack)),
|
||||
"Module jar " + origin("user.jar") + " has a broken entry point pack.user.User: info() failed",
|
||||
"Optional package 'org.*' of module user:core is not a package name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void referenceToAMissingApiClassPointsAtADifferentApiBuild(@TempDir Path build) {
|
||||
var newerApi = new TestModuleJar()
|
||||
|
||||
@@ -39,7 +39,13 @@ public final class TestModuleJar {
|
||||
|
||||
/** An entry class with the given module id whose install body may use {@code graph}. */
|
||||
public static TestModuleJar module(String className, String moduleId, String members, String install) {
|
||||
return new TestModuleJar().source(className, moduleSource(className, moduleId, members, install)).entryPoint(className);
|
||||
return module(className, moduleId, List.of(), members, install);
|
||||
}
|
||||
|
||||
/** Like {@link #module(String, String, String, String)}, with {@code info()} declaring {@code optionalPackages}. */
|
||||
public static TestModuleJar module(String className, String moduleId, List<String> optionalPackages, String members, String install) {
|
||||
var info = moduleInfo(moduleId, optionalPackages);
|
||||
return new TestModuleJar().source(className, moduleSource(className, info, members, install)).entryPoint(className);
|
||||
}
|
||||
|
||||
/** Compiles one class on its own and returns its class file, for raw entries such as versioned classes. */
|
||||
@@ -147,7 +153,14 @@ public final class TestModuleJar {
|
||||
output.closeEntry();
|
||||
}
|
||||
|
||||
private static String moduleSource(String className, String moduleId, String members, String install) {
|
||||
private static String moduleInfo(String moduleId, List<String> optionalPackages) {
|
||||
var info = "ModuleInfo.of(\"%s\", \"%s\", \"1.0.0\")".formatted(moduleId, displayName(moduleId));
|
||||
if (optionalPackages.isEmpty()) return info;
|
||||
var packages = optionalPackages.stream().map(name -> '"' + name + '"').collect(Collectors.joining(", "));
|
||||
return info + ".withOptionalPackages(" + packages + ")";
|
||||
}
|
||||
|
||||
private static String moduleSource(String className, String info, String members, String install) {
|
||||
int dot = className.lastIndexOf('.');
|
||||
return """
|
||||
package %s;
|
||||
@@ -159,11 +172,11 @@ public final class TestModuleJar {
|
||||
public final class %s implements IrisModule {
|
||||
%s
|
||||
|
||||
public ModuleInfo info() { return ModuleInfo.of("%s", "%s", "1.0.0"); }
|
||||
public ModuleInfo info() { return %s; }
|
||||
|
||||
public void install(StageGraph graph) { %s }
|
||||
}
|
||||
""".formatted(className.substring(0, dot), className.substring(dot + 1), members, moduleId, displayName(moduleId), install);
|
||||
""".formatted(className.substring(0, dot), className.substring(dot + 1), members, info, install);
|
||||
}
|
||||
|
||||
/** The display name test modules get, so assertions can predict it from the id. */
|
||||
|
||||
Reference in new issue
Block a user