mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Read module info once at discovery and keep it in DiscoveredModule
DiscoveredModule is now a final class holding the module, the ModuleInfo read when the module was found (pack jar or builtIn) and its origin. Graph install, load errors, the pack summary and optional-package filtering use only that stored value.
This commit is contained in:
1 parent
f5cf4b3ab6
commit
6263075ecd
9 files changed
+163
-22
No files matched your search
@@ -1,24 +1,74 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import java.util.Objects;
|
||||
|
||||
/** A module and where the host found it. Load errors name the origin so the author knows which
|
||||
* jar to fix.
|
||||
/** A module, its identity and where the host found it. Load errors name the origin so the author
|
||||
* knows which jar to fix.
|
||||
*
|
||||
* <p>{@link IrisModule#info()} is read once, when the module is discovered, and only that value is
|
||||
* used afterwards. A module whose {@code info()} changes between calls cannot give the host
|
||||
* inconsistent answers.
|
||||
*/
|
||||
public record DiscoveredModule(IrisModule module, String origin) {
|
||||
public final class DiscoveredModule {
|
||||
static final String HOST_CLASSPATH = "host classpath";
|
||||
|
||||
public DiscoveredModule {
|
||||
Objects.requireNonNull(module, "module");
|
||||
Objects.requireNonNull(origin, "origin");
|
||||
/** Where a module comes from: the host's classpath or a pack jar. */
|
||||
enum Source { HOST, PACK }
|
||||
|
||||
private final IrisModule module;
|
||||
private final ModuleInfo info;
|
||||
private final String origin;
|
||||
private final Source source;
|
||||
|
||||
private DiscoveredModule(IrisModule module, ModuleInfo info, String origin, Source source) {
|
||||
this.module = Objects.requireNonNull(module, "module");
|
||||
this.info = Objects.requireNonNull(info, "info");
|
||||
this.origin = Objects.requireNonNull(origin, "origin");
|
||||
this.source = Objects.requireNonNull(source, "source");
|
||||
if (origin.isBlank()) {
|
||||
throw new IllegalArgumentException("Blank module origin: " + module.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/** A module that ships with the host instead of a pack. */
|
||||
/** A module that ships with the host instead of a pack. Reads its {@code info()}; a failure
|
||||
* propagates unchanged.
|
||||
*/
|
||||
public static DiscoveredModule builtIn(IrisModule module) {
|
||||
return new DiscoveredModule(module, HOST_CLASSPATH);
|
||||
Objects.requireNonNull(module, "module");
|
||||
return new DiscoveredModule(module, readInfo(module), HOST_CLASSPATH, Source.HOST);
|
||||
}
|
||||
|
||||
/** A pack module whose {@code info()} the jar loader has already read and checked. */
|
||||
static DiscoveredModule fromJar(IrisModule module, ModuleInfo info, String origin) {
|
||||
return new DiscoveredModule(module, info, origin, Source.PACK);
|
||||
}
|
||||
|
||||
private static ModuleInfo readInfo(IrisModule module) {
|
||||
return Objects.requireNonNull(module.info(), () -> "info() of " + module.getClass().getName() + " returned null");
|
||||
}
|
||||
|
||||
public IrisModule module() {
|
||||
return module;
|
||||
}
|
||||
|
||||
/** The module's identity, as {@code info()} returned it at discovery. */
|
||||
public ModuleInfo info() {
|
||||
return info;
|
||||
}
|
||||
|
||||
/** The jar path relative to the pack, or "host classpath" for built-in modules. */
|
||||
public String origin() {
|
||||
return origin;
|
||||
}
|
||||
|
||||
Source source() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return info.id() + " from " + origin;
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ public record LoadedModules(FeatureCatalog catalog, ResolvedGraph placement, Res
|
||||
stages.install(discovered);
|
||||
discovered.module().register(registry);
|
||||
} catch (LinkageError error) {
|
||||
throw new IllegalArgumentException("Module " + discovered.module().info().id() + " from "
|
||||
throw new IllegalArgumentException("Module " + discovered.info().id() + " from "
|
||||
+ discovered.origin() + " failed to link or initialize a class while installing: " + error
|
||||
+ "; a missing method or field means the jar was built against a different Iris API", error);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -83,8 +84,7 @@ record ModuleJar(Path path, String origin, String entryClass, List<JarClass> cla
|
||||
var type = loadEntryClass(packLoader);
|
||||
requireDefinedHere(type);
|
||||
var module = instantiate(type);
|
||||
requireIdentity(module);
|
||||
return new DiscoveredModule(module, origin);
|
||||
return DiscoveredModule.fromJar(module, readIdentity(module), origin);
|
||||
}
|
||||
|
||||
/** Opens the jar with the runtime's multi-release version, like the pack class loader does, so
|
||||
@@ -197,9 +197,9 @@ record ModuleJar(Path path, String origin, String entryClass, List<JarClass> cla
|
||||
}
|
||||
}
|
||||
|
||||
private void requireIdentity(IrisModule module) {
|
||||
private ModuleInfo readIdentity(IrisModule module) {
|
||||
try {
|
||||
Objects.requireNonNull(module.info(), "info() returned null");
|
||||
return Objects.requireNonNull(module.info(), "info() returned null");
|
||||
} catch (RuntimeException | LinkageError exception) {
|
||||
throw brokenEntryPoint("info() failed: " + exception, exception);
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ public final class PackModules implements AutoCloseable {
|
||||
*/
|
||||
private static List<MissingClasses> withoutDeclaredOptional(List<MissingClasses> missingClasses, List<DiscoveredModule> modules) {
|
||||
var optionalPackages = modules.stream()
|
||||
.collect(Collectors.toMap(DiscoveredModule::origin, found -> found.module().info().optionalPackages()));
|
||||
.collect(Collectors.toMap(DiscoveredModule::origin, found -> found.info().optionalPackages()));
|
||||
return missingClasses.stream()
|
||||
.map(jar -> jar.outside(optionalPackages.get(jar.origin())))
|
||||
.filter(Predicate.not(MissingClasses::isEmpty))
|
||||
|
||||
@@ -23,7 +23,7 @@ final class PackModulesSummary {
|
||||
}
|
||||
|
||||
private static String describe(DiscoveredModule discovered) {
|
||||
var info = discovered.module().info();
|
||||
var info = discovered.info();
|
||||
return info.name() + " (" + info.id() + " " + info.version() + ") from " + discovered.origin();
|
||||
}
|
||||
}
|
||||
@@ -29,12 +29,11 @@ public final class StageGraphBuilder implements StageGraph {
|
||||
|
||||
public void install(DiscoveredModule discovered) {
|
||||
Objects.requireNonNull(discovered, "discovered");
|
||||
var module = discovered.module();
|
||||
var moduleId = Objects.requireNonNull(module.info(), "module info").id();
|
||||
var moduleId = discovered.info().id();
|
||||
claimModule(moduleId, discovered.origin());
|
||||
installingModule = moduleId;
|
||||
try {
|
||||
module.install(this);
|
||||
discovered.module().install(this);
|
||||
} finally {
|
||||
installingModule = null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DiscoveredModuleTest {
|
||||
/** Answers with its first identity once, then with another one. */
|
||||
private static final class ShiftingIdentity implements IrisModule {
|
||||
private int infoCalls;
|
||||
|
||||
public ModuleInfo info() {
|
||||
infoCalls++;
|
||||
return infoCalls == 1 ? ModuleInfo.of("first:core", "First", "1") : ModuleInfo.of("second:core", "Second", "2");
|
||||
}
|
||||
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(Stage.placement("first:core/mark")).run(context -> {});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void builtInReadsInfoOnceAndLoadingUsesOnlyThatValue() {
|
||||
var module = new ShiftingIdentity();
|
||||
var discovered = DiscoveredModule.builtIn(module);
|
||||
|
||||
var loaded = LoadedModules.load(List.of(discovered), Map.of());
|
||||
|
||||
assertEquals("first:core", discovered.info().id().toString());
|
||||
assertEquals(List.of("first:core/mark"),
|
||||
loaded.placement().stages().stream().map(stage -> stage.stage().id().toString()).toList());
|
||||
assertEquals(1, module.infoCalls);
|
||||
}
|
||||
|
||||
@Test
|
||||
void builtInWhoseInfoIsNullFailsNamingTheClass() {
|
||||
var module = new IrisModule() {
|
||||
public ModuleInfo info() { return null; }
|
||||
public void install(StageGraph graph) {}
|
||||
};
|
||||
|
||||
var error = assertThrows(NullPointerException.class, () -> DiscoveredModule.builtIn(module));
|
||||
|
||||
assertTrue(error.getMessage().contains(module.getClass().getName()), error.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.example.placement.MossModule;
|
||||
import com.volmit.example.placement.SnowModule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -7,11 +8,15 @@ import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PackModulesSummaryTest {
|
||||
private static DiscoveredModule pack(IrisModule module, String origin) {
|
||||
return DiscoveredModule.fromJar(module, module.info(), origin);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listsNameIdVersionAndJarOfEveryModuleInLoadOrder() {
|
||||
var modules = List.of(
|
||||
new DiscoveredModule(new MossModule(), "modules/moss.jar"),
|
||||
new DiscoveredModule(new SnowModule(), "modules/snow.jar"));
|
||||
pack(new MossModule(), "modules/moss.jar"),
|
||||
pack(new SnowModule(), "modules/snow.jar"));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 2 modules:
|
||||
@@ -22,7 +27,7 @@ class PackModulesSummaryTest {
|
||||
|
||||
@Test
|
||||
void singleModuleReadsAsOneModule() {
|
||||
var modules = List.of(new DiscoveredModule(new MossModule(), "modules/moss.jar"));
|
||||
var modules = List.of(pack(new MossModule(), "modules/moss.jar"));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 1 module:
|
||||
|
||||
@@ -96,7 +96,7 @@ class PackModulesTest {
|
||||
assertEquals(List.of(origin("alpha.jar"), origin("beta.jar")),
|
||||
packModules.modules().stream().map(DiscoveredModule::origin).toList());
|
||||
assertEquals(List.of("alpha:core", "beta:core"),
|
||||
packModules.modules().stream().map(found -> found.module().info().id().toString()).toList());
|
||||
packModules.modules().stream().map(found -> found.info().id().toString()).toList());
|
||||
assertNotSame(PackModules.class.getClassLoader(), packModules.modules().getFirst().module().getClass().getClassLoader());
|
||||
assertTrue(packModules.missingClasses().isEmpty());
|
||||
|
||||
@@ -539,6 +539,42 @@ class PackModulesTest {
|
||||
"Exposed package 'example.*' is not a package name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void packModuleInfoIsReadOnceAtDiscovery() {
|
||||
var answersOnce = """
|
||||
private static boolean answered;
|
||||
|
||||
public ModuleInfo info() {
|
||||
if (answered) throw new IllegalStateException("info() called twice");
|
||||
answered = true;
|
||||
return ModuleInfo.of("once:core", "Once", "1.0.0");
|
||||
}
|
||||
""";
|
||||
new TestModuleJar()
|
||||
.source("pack.once.Once", """
|
||||
package pack.once;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.stage.*;
|
||||
|
||||
public final class Once implements IrisModule {
|
||||
%s
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(Stage.placement("once:core/mark")).run(context -> {});
|
||||
}
|
||||
}
|
||||
""".formatted(answersOnce))
|
||||
.entryPoint("pack.once.Once")
|
||||
.writeInto(pack, "once.jar");
|
||||
|
||||
try (var packModules = PackModules.load(pack)) {
|
||||
var loaded = assertDoesNotThrow(() -> LoadedModules.load(packModules.modules(), Map.of()));
|
||||
assertEquals("once:core", packModules.modules().getFirst().info().id().toString());
|
||||
assertEquals(1, loaded.placement().stages().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void methodMissingAtInstallFailsNamingTheJar(@TempDir Path build) {
|
||||
var libraryWithMethod = new TestModuleJar()
|
||||
|
||||
Reference in new issue
Block a user