diff --git a/src/main/java/com/volmit/ModuleInfo.java b/src/main/java/com/volmit/ModuleInfo.java
index a0814707c..b4b6743e1 100644
--- a/src/main/java/com/volmit/ModuleInfo.java
+++ b/src/main/java/com/volmit/ModuleInfo.java
@@ -3,17 +3,23 @@ package com.volmit;
import com.volmit.resource.ResourceId;
import java.util.Objects;
-/** Module identity. Stages a module defines must use the namespace of its id. */
-public record ModuleInfo(ResourceId id, String version) {
+/** Module identity. Stages a module defines must use the namespace of its id. The name is for
+ * people, for example in load logs; the id is what the host and other modules match on.
+ */
+public record ModuleInfo(ResourceId id, String name, String version) {
public ModuleInfo {
Objects.requireNonNull(id, "id");
+ Objects.requireNonNull(name, "name");
Objects.requireNonNull(version, "version");
+ if (name.isBlank()) {
+ throw new IllegalArgumentException("Blank module name: " + id);
+ }
if (version.isBlank()) {
throw new IllegalArgumentException("Blank module version: " + id);
}
}
- public static ModuleInfo of(String id, String version) {
- return new ModuleInfo(ResourceId.parse(id), version);
+ public static ModuleInfo of(String id, String name, String version) {
+ return new ModuleInfo(ResourceId.parse(id), name, version);
}
}
diff --git a/src/main/java/com/volmit/host/ModuleJar.java b/src/main/java/com/volmit/host/ModuleJar.java
index 239eb7883..340dd10c9 100644
--- a/src/main/java/com/volmit/host/ModuleJar.java
+++ b/src/main/java/com/volmit/host/ModuleJar.java
@@ -13,38 +13,42 @@ import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
+import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import java.util.zip.ZipFile;
-/** One jar in a pack's modules directory and the entry class it names.
+/** One jar in a pack's modules directory, the entry class it names and the classes it holds.
*
*
The entry point is the jar's own {@code META-INF/services/com.volmit.IrisModule} file, in
* the format {@link java.util.ServiceLoader} reads. The host reads that file from this jar only:
* {@code ServiceLoader} would also return providers from parent class loaders and could not tell
* which jar a broken entry came from.
*/
-record ModuleJar(Path path, String origin, String entryClass) {
+record ModuleJar(Path path, String origin, String entryClass, List classNames) {
static final String ENTRY_POINT_FILE = "META-INF/services/" + IrisModule.class.getName();
private static final String COMMENT_START = "#";
+ private static final String CLASS_FILE_SUFFIX = ".class";
+ private static final String MODULE_DESCRIPTOR = "module-info" + CLASS_FILE_SUFFIX;
+ private static final String META_INF = "META-INF/";
ModuleJar {
Objects.requireNonNull(path, "path");
Objects.requireNonNull(origin, "origin");
Objects.requireNonNull(entryClass, "entryClass");
+ classNames = List.copyOf(classNames);
}
- /** Reads the single entry class a jar names. The origin is the jar path relative to its pack. */
+ /** Reads the single entry class a jar names and the classes it holds. The origin is the jar
+ * path relative to its pack.
+ */
static ModuleJar read(Path packRoot, Path jar) {
var origin = packRoot.relativize(jar).toString();
- var entryClasses = readEntryClasses(jar, origin);
- if (entryClasses.isEmpty()) {
- throw new IllegalArgumentException("Module jar " + origin + " has no entry point; name its "
- + IrisModule.class.getSimpleName() + " class in " + ENTRY_POINT_FILE);
+ try (var file = openAsClassLoaderSeesIt(jar)) {
+ var entryClass = requireSingleEntryClass(origin, readEntryClasses(file));
+ return new ModuleJar(jar, origin, entryClass, readClassNames(file));
+ } catch (IOException exception) {
+ throw new UncheckedIOException("Module jar " + origin + " is not a readable jar", exception);
}
- if (entryClasses.size() > 1) {
- throw new IllegalArgumentException("Module jar " + origin + " names " + entryClasses.size()
- + " entry points " + entryClasses + "; one jar holds exactly one module");
- }
- return new ModuleJar(jar, origin, entryClasses.getFirst());
}
URL url() {
@@ -66,21 +70,61 @@ record ModuleJar(Path path, String origin, String entryClass) {
return new DiscoveredModule(module, origin);
}
- private static List readEntryClasses(Path jar, String origin) {
- try (var file = new JarFile(jar.toFile())) {
- var entry = file.getJarEntry(ENTRY_POINT_FILE);
- if (entry == null) return List.of();
- try (var reader = new BufferedReader(new InputStreamReader(file.getInputStream(entry), StandardCharsets.UTF_8))) {
- return reader.lines()
- .map(ModuleJar::withoutComment)
- .map(String::strip)
- .filter(Predicate.not(String::isEmpty))
- .distinct()
- .toList();
- }
- } catch (IOException exception) {
- throw new UncheckedIOException("Module jar " + origin + " is not a readable jar", exception);
+ /** Opens the jar with the runtime's multi-release version, like the pack class loader does, so
+ * class names below match the classes that loader can define.
+ */
+ private static JarFile openAsClassLoaderSeesIt(Path jar) throws IOException {
+ return new JarFile(jar.toFile(), true, ZipFile.OPEN_READ, JarFile.runtimeVersion());
+ }
+
+ private static String requireSingleEntryClass(String origin, List entryClasses) {
+ if (entryClasses.isEmpty()) {
+ throw new IllegalArgumentException("Module jar " + origin + " has no entry point; name its "
+ + IrisModule.class.getSimpleName() + " class in " + ENTRY_POINT_FILE);
}
+ if (entryClasses.size() > 1) {
+ throw new IllegalArgumentException("Module jar " + origin + " names " + entryClasses.size()
+ + " entry points " + entryClasses + "; one jar holds exactly one module");
+ }
+ return entryClasses.getFirst();
+ }
+
+ private static List readEntryClasses(JarFile file) throws IOException {
+ var entry = file.getJarEntry(ENTRY_POINT_FILE);
+ if (entry == null) return List.of();
+ try (var reader = new BufferedReader(new InputStreamReader(file.getInputStream(entry), StandardCharsets.UTF_8))) {
+ return reader.lines()
+ .map(ModuleJar::withoutComment)
+ .map(String::strip)
+ .filter(Predicate.not(String::isEmpty))
+ .distinct()
+ .toList();
+ }
+ }
+
+ /** Binary names of the classes in the jar, sorted. A multi-release jar contributes each class
+ * once, under its base name, in the version this runtime would load. Anything left below
+ * {@code META-INF/}, such as versioned entries of a jar that is not multi-release, cannot be
+ * loaded as a class: {@code META-INF} is not a valid package name.
+ */
+ private static List readClassNames(JarFile file) {
+ return file.versionedStream()
+ .filter(Predicate.not(JarEntry::isDirectory))
+ .map(JarEntry::getName)
+ .filter(ModuleJar::isClassFile)
+ .map(ModuleJar::toBinaryName)
+ .sorted()
+ .toList();
+ }
+
+ private static boolean isClassFile(String entryName) {
+ return entryName.endsWith(CLASS_FILE_SUFFIX)
+ && !entryName.equals(MODULE_DESCRIPTOR)
+ && !entryName.startsWith(META_INF);
+ }
+
+ private static String toBinaryName(String entryName) {
+ return entryName.substring(0, entryName.length() - CLASS_FILE_SUFFIX.length()).replace('/', '.');
}
private static String withoutComment(String line) {
diff --git a/src/main/java/com/volmit/host/PackModules.java b/src/main/java/com/volmit/host/PackModules.java
index da4fa096a..42c0df484 100644
--- a/src/main/java/com/volmit/host/PackModules.java
+++ b/src/main/java/com/volmit/host/PackModules.java
@@ -3,11 +3,13 @@ package com.volmit.host;
import com.volmit.IrisModule;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.lang.System.Logger.Level;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.List;
import java.util.Objects;
@@ -16,7 +18,11 @@ import java.util.Objects;
*
* All jars of a pack share one class loader whose parent is the loader of the API. A pack jar
* can therefore name another pack jar's stage constants in {@code requires(...)}, as well as
- * those of built-in modules visible to the API. Packs do not see each other's classes.
+ * those of built-in modules visible to the API. Packs do not see each other's classes, and no class
+ * may appear in two jars of one pack.
+ *
+ *
A successful load logs one INFO summary of the pack's modules through {@link System.Logger},
+ * which the host can route to its own logging.
*
*
Close this when the pack unloads, after dropping everything built from its modules, such as
* {@link LoadedModules}. Classes not loaded before then can no longer be loaded.
@@ -24,6 +30,7 @@ import java.util.Objects;
public final class PackModules implements AutoCloseable {
public static final String MODULES_DIRECTORY = "modules";
static final String JAR_EXTENSION = ".jar";
+ private static final System.Logger LOGGER = System.getLogger(PackModules.class.getName());
private final List modules;
private final URLClassLoader loader;
@@ -41,13 +48,10 @@ public final class PackModules implements AutoCloseable {
var jars = findModuleJars(packRoot).stream()
.map(jar -> ModuleJar.read(packRoot, jar))
.toList();
- var loader = newPackClassLoader(packRoot, jars);
- try {
- return new PackModules(createModules(jars, loader), loader);
- } catch (RuntimeException | Error failure) {
- closeAfterFailure(loader, failure);
- throw failure;
- }
+ requireDistinctClasses(jars);
+ var packModules = defineModules(packRoot, jars);
+ LOGGER.log(Level.INFO, () -> PackModulesSummary.describe(packName(packRoot), packModules.modules()));
+ return packModules;
}
/** One module per jar, in jar file name order. */
@@ -81,9 +85,43 @@ public final class PackModules implements AutoCloseable {
return Files.isRegularFile(path) && path.getFileName().toString().endsWith(JAR_EXTENSION);
}
+ /** Fails before any class is defined when two jars hold the same class. The shared loader would
+ * otherwise silently use whichever copy comes first in file name order.
+ */
+ private static void requireDistinctClasses(List jars) {
+ var owners = new HashMap();
+ for (var jar : jars) {
+ for (var className : jar.classNames()) {
+ var owner = owners.putIfAbsent(className, jar);
+ if (owner != null) throw duplicateClass(className, owner, jar);
+ }
+ }
+ }
+
+ private static IllegalArgumentException duplicateClass(String className, ModuleJar first, ModuleJar second) {
+ return new IllegalArgumentException("Class " + className + " is in both module jars " + first.origin()
+ + " and " + second.origin() + "; every class of a pack must come from one jar, so relocate"
+ + " shaded libraries or keep shared classes in one module");
+ }
+
+ private static PackModules defineModules(Path packRoot, List jars) {
+ var loader = newPackClassLoader(packRoot, jars);
+ try {
+ return new PackModules(createModules(jars, loader), loader);
+ } catch (RuntimeException | Error failure) {
+ closeAfterFailure(loader, failure);
+ throw failure;
+ }
+ }
+
private static URLClassLoader newPackClassLoader(Path packRoot, List jars) {
var urls = jars.stream().map(ModuleJar::url).toArray(URL[]::new);
- return new URLClassLoader("pack:" + packRoot.getFileName(), urls, IrisModule.class.getClassLoader());
+ return new URLClassLoader("pack:" + packName(packRoot), urls, IrisModule.class.getClassLoader());
+ }
+
+ private static String packName(Path packRoot) {
+ var name = packRoot.getFileName();
+ return name == null ? packRoot.toString() : name.toString();
}
private static List createModules(List jars, ClassLoader loader) {
diff --git a/src/main/java/com/volmit/host/PackModulesSummary.java b/src/main/java/com/volmit/host/PackModulesSummary.java
new file mode 100644
index 000000000..86cbb578b
--- /dev/null
+++ b/src/main/java/com/volmit/host/PackModulesSummary.java
@@ -0,0 +1,29 @@
+package com.volmit.host;
+
+import java.util.List;
+
+/** The one log message a pack load writes: which modules the pack uses and which jar each comes
+ * from.
+ */
+final class PackModulesSummary {
+ private static final String INDENT = " ";
+
+ private PackModulesSummary() {}
+
+ static String describe(String packName, List modules) {
+ if (modules.isEmpty()) {
+ return "Pack " + packName + " uses no modules: no jars in its " + PackModules.MODULES_DIRECTORY + " directory";
+ }
+ var text = new StringBuilder("Pack ").append(packName).append(" uses ").append(modules.size())
+ .append(modules.size() == 1 ? " module:" : " modules:");
+ for (var module : modules) {
+ text.append('\n').append(INDENT).append(describe(module));
+ }
+ return text.toString();
+ }
+
+ private static String describe(DiscoveredModule discovered) {
+ var info = discovered.module().info();
+ return info.name() + " (" + info.id() + " " + info.version() + ") from " + discovered.origin();
+ }
+}
diff --git a/src/test/java/com/volmit/ApiValuesTest.java b/src/test/java/com/volmit/ApiValuesTest.java
index d59b58d05..40234f7c0 100644
--- a/src/test/java/com/volmit/ApiValuesTest.java
+++ b/src/test/java/com/volmit/ApiValuesTest.java
@@ -31,6 +31,16 @@ class ApiValuesTest {
assertThrows(IllegalArgumentException.class, () -> new ResourceKey<>("test:value", int.class));
}
+ @Test
+ void moduleInfoRequiresANameAndVersion() {
+ var info = ModuleInfo.of("example:moss", "Moss", "1.0.0");
+ assertEquals(ResourceId.parse("example:moss"), info.id());
+ assertEquals("Moss", info.name());
+ assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("example:moss", " ", "1.0.0"));
+ assertThrows(IllegalArgumentException.class, () -> ModuleInfo.of("example:moss", "Moss", ""));
+ assertThrows(NullPointerException.class, () -> ModuleInfo.of("example:moss", null, "1.0.0"));
+ }
+
@Test
void sparseObjectsCopySortAndRejectDuplicateOrOutOfBoundsBlocks() {
var a = new ObjectBlock(new BlockPosition(-1, 0, 0), new BlockState("minecraft:stone"));
diff --git a/src/test/java/com/volmit/example/PlacementLifecyclePortTest.java b/src/test/java/com/volmit/example/PlacementLifecyclePortTest.java
index 9a0174f9b..ac5f6368a 100644
--- a/src/test/java/com/volmit/example/PlacementLifecyclePortTest.java
+++ b/src/test/java/com/volmit/example/PlacementLifecyclePortTest.java
@@ -177,7 +177,7 @@ class PlacementLifecyclePortTest {
void inactiveRequirementDeactivatesThirdPartyDependents() {
var capModule = new IrisModule() {
static final PlacementStage CAP = Stage.placement("example:cap/top");
- public ModuleInfo info() { return ModuleInfo.of("example:cap", "1"); }
+ public ModuleInfo info() { return ModuleInfo.of("example:cap", "Cap", "1"); }
public void install(StageGraph graph) {
graph.define(CAP).requires(StiltModule.FILL).run(context -> context.access().set(
new PlacedBlock(new BlockPosition(5, 20, 5), PLANKS, Optional.empty())));
diff --git a/src/test/java/com/volmit/example/chunk/GrassModule.java b/src/test/java/com/volmit/example/chunk/GrassModule.java
index 7b084d402..9a5e4ac86 100644
--- a/src/test/java/com/volmit/example/chunk/GrassModule.java
+++ b/src/test/java/com/volmit/example/chunk/GrassModule.java
@@ -14,7 +14,7 @@ public final class GrassModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("example:grass", "1.0.0");
+ return ModuleInfo.of("example:grass", "Grass", "1.0.0");
}
@Override
diff --git a/src/test/java/com/volmit/example/chunk/TerrainModule.java b/src/test/java/com/volmit/example/chunk/TerrainModule.java
index e03ab2dc7..aacc16245 100644
--- a/src/test/java/com/volmit/example/chunk/TerrainModule.java
+++ b/src/test/java/com/volmit/example/chunk/TerrainModule.java
@@ -17,7 +17,7 @@ public final class TerrainModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("iris:terrain", "test");
+ return ModuleInfo.of("iris:terrain", "Terrain", "test");
}
@Override
diff --git a/src/test/java/com/volmit/example/placement/AnchorModule.java b/src/test/java/com/volmit/example/placement/AnchorModule.java
index e7715f607..dcabad645 100644
--- a/src/test/java/com/volmit/example/placement/AnchorModule.java
+++ b/src/test/java/com/volmit/example/placement/AnchorModule.java
@@ -15,7 +15,7 @@ public final class AnchorModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("iris:anchor", "test");
+ return ModuleInfo.of("iris:anchor", "Anchor", "test");
}
@Override
diff --git a/src/test/java/com/volmit/example/placement/BlocksModule.java b/src/test/java/com/volmit/example/placement/BlocksModule.java
index 1d977c5aa..d03088d22 100644
--- a/src/test/java/com/volmit/example/placement/BlocksModule.java
+++ b/src/test/java/com/volmit/example/placement/BlocksModule.java
@@ -17,7 +17,7 @@ public final class BlocksModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("iris:blocks", "test");
+ return ModuleInfo.of("iris:blocks", "Blocks", "test");
}
@Override
diff --git a/src/test/java/com/volmit/example/placement/MossModule.java b/src/test/java/com/volmit/example/placement/MossModule.java
index 1c2d46445..94c7f8b92 100644
--- a/src/test/java/com/volmit/example/placement/MossModule.java
+++ b/src/test/java/com/volmit/example/placement/MossModule.java
@@ -21,7 +21,7 @@ public final class MossModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("example:moss", "1.0.0");
+ return ModuleInfo.of("example:moss", "Moss", "1.0.0");
}
@Override
diff --git a/src/test/java/com/volmit/example/placement/PrepareModule.java b/src/test/java/com/volmit/example/placement/PrepareModule.java
index ceed81465..aa35585b0 100644
--- a/src/test/java/com/volmit/example/placement/PrepareModule.java
+++ b/src/test/java/com/volmit/example/placement/PrepareModule.java
@@ -17,7 +17,7 @@ public final class PrepareModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("iris:prepare", "test");
+ return ModuleInfo.of("iris:prepare", "Prepare", "test");
}
@Override
diff --git a/src/test/java/com/volmit/example/placement/SnowModule.java b/src/test/java/com/volmit/example/placement/SnowModule.java
index b5207019e..bf41113f9 100644
--- a/src/test/java/com/volmit/example/placement/SnowModule.java
+++ b/src/test/java/com/volmit/example/placement/SnowModule.java
@@ -23,7 +23,7 @@ public final class SnowModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("iris:snow", "test");
+ return ModuleInfo.of("iris:snow", "Snow", "test");
}
@Override
diff --git a/src/test/java/com/volmit/example/placement/StiltModule.java b/src/test/java/com/volmit/example/placement/StiltModule.java
index 2459df836..491e8abdb 100644
--- a/src/test/java/com/volmit/example/placement/StiltModule.java
+++ b/src/test/java/com/volmit/example/placement/StiltModule.java
@@ -19,7 +19,7 @@ public final class StiltModule implements IrisModule {
@Override
public ModuleInfo info() {
- return ModuleInfo.of("iris:stilt", "test");
+ return ModuleInfo.of("iris:stilt", "Stilts", "test");
}
@Override
diff --git a/src/test/java/com/volmit/host/ModuleRegistryTest.java b/src/test/java/com/volmit/host/ModuleRegistryTest.java
index ac8b5b8ae..87ff5da4e 100644
--- a/src/test/java/com/volmit/host/ModuleRegistryTest.java
+++ b/src/test/java/com/volmit/host/ModuleRegistryTest.java
@@ -69,7 +69,7 @@ class ModuleRegistryTest {
void loadingModulesCatalogsStageSettingsAndRegistrationsTogether() {
var conditioned = new IrisModule() {
static final PlacementStage WORK = Stage.placement("test:conditioned/work");
- public ModuleInfo info() { return ModuleInfo.of("test:conditioned", "1"); }
+ public ModuleInfo info() { return ModuleInfo.of("test:conditioned", "Conditioned", "1"); }
public void install(StageGraph graph) { graph.define(WORK).when(One.class).run((context, one) -> {}); }
public void register(FeatureRegistry registry) { registry.registerObject(Two.class, ignored -> context -> null); }
};
diff --git a/src/test/java/com/volmit/host/PackModulesSummaryTest.java b/src/test/java/com/volmit/host/PackModulesSummaryTest.java
new file mode 100644
index 000000000..b0dfb1989
--- /dev/null
+++ b/src/test/java/com/volmit/host/PackModulesSummaryTest.java
@@ -0,0 +1,38 @@
+package com.volmit.host;
+
+import com.volmit.example.placement.MossModule;
+import com.volmit.example.placement.SnowModule;
+import org.junit.jupiter.api.Test;
+import java.util.List;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class PackModulesSummaryTest {
+ @Test
+ void listsNameIdVersionAndJarOfEveryModuleInLoadOrder() {
+ var modules = List.of(
+ new DiscoveredModule(new MossModule(), "modules/moss.jar"),
+ new DiscoveredModule(new SnowModule(), "modules/snow.jar"));
+
+ assertEquals("""
+ Pack overworld uses 2 modules:
+ Moss (example:moss 1.0.0) from modules/moss.jar
+ Snow (iris:snow test) from modules/snow.jar""",
+ PackModulesSummary.describe("overworld", modules));
+ }
+
+ @Test
+ void singleModuleReadsAsOneModule() {
+ var modules = List.of(new DiscoveredModule(new MossModule(), "modules/moss.jar"));
+
+ assertEquals("""
+ Pack overworld uses 1 module:
+ Moss (example:moss 1.0.0) from modules/moss.jar""",
+ PackModulesSummary.describe("overworld", modules));
+ }
+
+ @Test
+ void packWithoutModulesSaysSo() {
+ assertEquals("Pack overworld uses no modules: no jars in its modules directory",
+ PackModulesSummary.describe("overworld", List.of()));
+ }
+}
diff --git a/src/test/java/com/volmit/host/PackModulesTest.java b/src/test/java/com/volmit/host/PackModulesTest.java
index a02c647c9..6d15b2f9b 100644
--- a/src/test/java/com/volmit/host/PackModulesTest.java
+++ b/src/test/java/com/volmit/host/PackModulesTest.java
@@ -18,6 +18,10 @@ class PackModulesTest {
"public static final PlacementStage MARK = Stage.placement(\"alpha:core/mark\");";
private static final String NO_MEMBERS = "";
private static final String NO_STAGES = "";
+ private static final String SHARED_CLASS = "pack.shared.Util";
+ private static final String SHARED_SOURCE = "package pack.shared; public final class Util {}";
+ private static final byte[] UNUSED_CLASS_BYTES = {(byte) 0xCA, (byte) 0xFE};
+ private static final String DEFINED_PROPERTY = "irisapi.test.packModuleConstructed";
@TempDir
Path pack;
@@ -133,6 +137,76 @@ class PackModulesTest {
"not from this jar");
}
+ @Test
+ void sameClassInTwoJarsFailsBeforeAnyModuleIsConstructed() {
+ var constructorMarker = "public %s() { System.setProperty(\"" + DEFINED_PROPERTY + "\", \"true\"); }";
+ module("pack.alpha.Alpha", "alpha:core", constructorMarker.formatted("Alpha"), NO_STAGES)
+ .source(SHARED_CLASS, SHARED_SOURCE).writeInto(pack, "alpha.jar");
+ module("pack.beta.Beta", "beta:core", constructorMarker.formatted("Beta"), NO_STAGES)
+ .source(SHARED_CLASS, SHARED_SOURCE).writeInto(pack, "beta.jar");
+
+ try {
+ assertMentions(failure(() -> PackModules.load(pack)),
+ "Class " + SHARED_CLASS + " is in both module jars " + origin("alpha.jar") + " and " + origin("beta.jar"));
+ assertNull(System.getProperty(DEFINED_PROPERTY), "a module was constructed before the duplicate was found");
+ } finally {
+ System.clearProperty(DEFINED_PROPERTY);
+ }
+ }
+
+ @Test
+ void moduleDescriptorsMayRepeatAcrossJars() {
+ module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
+ .resource("module-info.class", UNUSED_CLASS_BYTES).writeInto(pack, "alpha.jar");
+ module("pack.beta.Beta", "beta:core", NO_MEMBERS, NO_STAGES)
+ .resource("module-info.class", UNUSED_CLASS_BYTES).writeInto(pack, "beta.jar");
+
+ try (var packModules = PackModules.load(pack)) {
+ assertEquals(2, packModules.modules().size());
+ }
+ }
+
+ @Test
+ void versionedClassOfAMultiReleaseJarCountsUnderItsBaseName() {
+ module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
+ .source(SHARED_CLASS, SHARED_SOURCE).writeInto(pack, "alpha.jar");
+ module("pack.beta.Beta", "beta:core", NO_MEMBERS, NO_STAGES)
+ .multiRelease()
+ .resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
+ .writeInto(pack, "beta.jar");
+
+ assertMentions(failure(() -> PackModules.load(pack)),
+ "Class " + SHARED_CLASS + " is in both module jars " + origin("alpha.jar") + " and " + origin("beta.jar"));
+ }
+
+ @Test
+ void versionedEntriesOutsideAMultiReleaseJarAreNotClasses() {
+ module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
+ .source(SHARED_CLASS, SHARED_SOURCE)
+ .resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
+ .writeInto(pack, "alpha.jar");
+ module("pack.beta.Beta", "beta:core", NO_MEMBERS, NO_STAGES)
+ .resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
+ .writeInto(pack, "beta.jar");
+
+ try (var packModules = PackModules.load(pack)) {
+ assertEquals(2, packModules.modules().size());
+ }
+ }
+
+ @Test
+ void multiReleaseJarMayHoldAVersionedCopyOfItsOwnClass() {
+ module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
+ .source(SHARED_CLASS, SHARED_SOURCE)
+ .multiRelease()
+ .resource("META-INF/versions/9/pack/shared/Util.class", UNUSED_CLASS_BYTES)
+ .writeInto(pack, "alpha.jar");
+
+ try (var packModules = PackModules.load(pack)) {
+ assertEquals(1, packModules.modules().size());
+ }
+ }
+
@Test
void duplicateModuleIdsAcrossJarsFailNamingBothJars() {
module("pack.one.Duplicate", "dup:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "one.jar");
diff --git a/src/test/java/com/volmit/host/StageGraphTest.java b/src/test/java/com/volmit/host/StageGraphTest.java
index 597ffc664..e2aa85922 100644
--- a/src/test/java/com/volmit/host/StageGraphTest.java
+++ b/src/test/java/com/volmit/host/StageGraphTest.java
@@ -41,7 +41,7 @@ class StageGraphTest {
private static IrisModule module(String id, Consumer install) {
return new IrisModule() {
- public ModuleInfo info() { return ModuleInfo.of(id, "1"); }
+ public ModuleInfo info() { return ModuleInfo.of(id, "Test module " + id, "1"); }
public void install(StageGraph graph) { install.accept(graph); }
};
}
diff --git a/src/test/java/com/volmit/support/TestModuleJar.java b/src/test/java/com/volmit/support/TestModuleJar.java
index e0531e0cf..0a7f6c72b 100644
--- a/src/test/java/com/volmit/support/TestModuleJar.java
+++ b/src/test/java/com/volmit/support/TestModuleJar.java
@@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
@@ -30,6 +31,8 @@ public final class TestModuleJar {
private static final String SERVICES_FILE = "META-INF/services/" + IrisModule.class.getName();
private final Map sources = new LinkedHashMap<>();
+ private final Map resources = new LinkedHashMap<>();
+ private boolean multiRelease;
private final List entryPoints = new ArrayList<>();
private final List classpath = new ArrayList<>(List.of(classesOf(IrisModule.class), classesOf(AnchorModule.class)));
@@ -49,6 +52,18 @@ public final class TestModuleJar {
return this;
}
+ /** Adds a raw jar entry, such as a versioned class file, without compiling anything. */
+ public TestModuleJar resource(String entryName, byte[] content) {
+ resources.put(entryName, content.clone());
+ return this;
+ }
+
+ /** Marks the jar {@code Multi-Release: true}, so its {@code META-INF/versions} entries count. */
+ public TestModuleJar multiRelease() {
+ multiRelease = true;
+ return this;
+ }
+
public TestModuleJar compileAgainst(Path jar) {
classpath.add(jar);
return this;
@@ -59,8 +74,9 @@ public final class TestModuleJar {
try {
var jar = Files.createDirectories(packRoot.resolve("modules")).resolve(jarName);
var classes = compile(Files.createTempDirectory(packRoot, "classes"));
- try (var output = new JarOutputStream(Files.newOutputStream(jar), new Manifest())) {
+ try (var output = new JarOutputStream(Files.newOutputStream(jar), manifest())) {
writeClasses(output, classes);
+ writeResources(output);
writeEntryPoints(output);
}
return jar;
@@ -92,6 +108,21 @@ public final class TestModuleJar {
}
}
+ private Manifest manifest() {
+ var manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+ if (multiRelease) manifest.getMainAttributes().put(Attributes.Name.MULTI_RELEASE, "true");
+ return manifest;
+ }
+
+ private void writeResources(JarOutputStream output) throws IOException {
+ for (var resource : resources.entrySet()) {
+ output.putNextEntry(new JarEntry(resource.getKey()));
+ output.write(resource.getValue());
+ output.closeEntry();
+ }
+ }
+
private void writeEntryPoints(JarOutputStream output) throws IOException {
if (entryPoints.isEmpty()) return;
output.putNextEntry(new JarEntry(SERVICES_FILE));
@@ -111,11 +142,16 @@ public final class TestModuleJar {
public final class %s implements IrisModule {
%s
- public ModuleInfo info() { return ModuleInfo.of("%s", "1.0.0"); }
+ public ModuleInfo info() { return ModuleInfo.of("%s", "%s", "1.0.0"); }
public void install(StageGraph graph) { %s }
}
- """.formatted(className.substring(0, dot), className.substring(dot + 1), members, moduleId, install);
+ """.formatted(className.substring(0, dot), className.substring(dot + 1), members, moduleId, displayName(moduleId), install);
+ }
+
+ /** The display name test modules get, so assertions can predict it from the id. */
+ public static String displayName(String moduleId) {
+ return "Test " + moduleId;
}
private static Path classesOf(Class> type) {