diff --git a/src/main/java/com/volmit/host/ApiAndJdkClassLoader.java b/src/main/java/com/volmit/host/ApiAndJdkClassLoader.java
deleted file mode 100644
index 40af12f60..000000000
--- a/src/main/java/com/volmit/host/ApiAndJdkClassLoader.java
+++ /dev/null
@@ -1,78 +0,0 @@
-package com.volmit.host;
-
-import java.io.IOException;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.Objects;
-
-/** Parent of every pack class loader. It shows a pack the Iris API and the JDK and nothing else:
- * API classes come from the loader that defines the API, every other class from the platform class
- * loader. Iris internals and the libraries Iris bundles stay hidden even when they sit in the API's
- * loader, so a module can use only the API, the JDK and the classes of its own pack's jars.
- *
- *
Resources follow the same rule, by path: {@code com/volmit/...} from the API's loader,
- * everything else from the platform loader. A class and its class file therefore always come from
- * the same place, and a pack never sees Iris's own resources, such as its service files.
- *
- *
This loader defines no classes itself.
- */
-final class ApiAndJdkClassLoader extends ClassLoader {
- /** Every class in a package with this prefix belongs to the Iris API. Module classes must not use it. */
- static final String API_PACKAGE_PREFIX = "com.volmit.";
- private static final String API_RESOURCE_PREFIX = API_PACKAGE_PREFIX.replace('.', '/');
- private static final String CLASS_FILE_SUFFIX = ".class";
- private static final String NAME = "iris-api-and-jdk";
-
- static {
- registerAsParallelCapable();
- }
-
- private final ClassLoader apiLoader;
-
- ApiAndJdkClassLoader(ClassLoader apiLoader) {
- super(NAME, ClassLoader.getPlatformClassLoader());
- this.apiLoader = Objects.requireNonNull(apiLoader, "apiLoader");
- }
-
- static boolean isApiClass(String binaryName) {
- return binaryName.startsWith(API_PACKAGE_PREFIX);
- }
-
- /** Whether this loader can load the class, judged by its class file so that nothing is loaded. */
- boolean hasClass(String binaryName) {
- return getResource(classFile(binaryName)) != null;
- }
-
- /** Whether the API's loader has the class, whether or not this loader shows it to packs. In
- * production that loader also holds Iris internals and the libraries Iris bundles.
- */
- boolean hostHasClass(String binaryName) {
- return apiLoader.getResource(classFile(binaryName)) != null;
- }
-
- @Override
- protected Class> loadClass(String name, boolean resolve) throws ClassNotFoundException {
- if (isApiClass(name)) return apiLoader.loadClass(name);
- return super.loadClass(name, resolve);
- }
-
- @Override
- public URL getResource(String name) {
- if (isApiResource(name)) return apiLoader.getResource(name);
- return super.getResource(name);
- }
-
- @Override
- public Enumeration getResources(String name) throws IOException {
- if (isApiResource(name)) return apiLoader.getResources(name);
- return super.getResources(name);
- }
-
- private static String classFile(String binaryName) {
- return binaryName.replace('.', '/') + CLASS_FILE_SUFFIX;
- }
-
- private static boolean isApiResource(String name) {
- return name.startsWith(API_RESOURCE_PREFIX);
- }
-}
diff --git a/src/main/java/com/volmit/host/ExposedHostClassLoader.java b/src/main/java/com/volmit/host/ExposedHostClassLoader.java
new file mode 100644
index 000000000..979ac5cb9
--- /dev/null
+++ b/src/main/java/com/volmit/host/ExposedHostClassLoader.java
@@ -0,0 +1,76 @@
+package com.volmit.host;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Objects;
+
+/** Parent of every pack class loader. It shows a pack the exposed host packages and the JDK and
+ * nothing else: classes in an {@link ExposedPackages exposed package}, always including the Iris
+ * API, come from the host's loader, every other class from the platform class loader. Iris
+ * internals and the libraries Iris bundles stay hidden even though they sit in the host's loader,
+ * so a module can use only the exposed packages, the JDK and the classes of its own pack's jars.
+ *
+ * Resources follow the same rule, by path: {@code com/volmit/...} and the paths of other
+ * exposed packages from the host's loader, everything else from the platform loader. A class and
+ * its class file therefore always come from the same place, and a pack never sees Iris's own
+ * resources, such as its service files.
+ *
+ *
This loader defines no classes itself.
+ */
+final class ExposedHostClassLoader extends ClassLoader {
+ private static final String CLASS_FILE_SUFFIX = ".class";
+ private static final String NAME = "iris-exposed-host-and-jdk";
+
+ static {
+ registerAsParallelCapable();
+ }
+
+ private final ClassLoader hostLoader;
+ private final ExposedPackages exposed;
+
+ /** {@code hostLoader} is the loader that defines the API; in production the Iris plugin's. */
+ ExposedHostClassLoader(ClassLoader hostLoader, ExposedPackages exposed) {
+ super(NAME, ClassLoader.getPlatformClassLoader());
+ this.hostLoader = Objects.requireNonNull(hostLoader, "hostLoader");
+ this.exposed = Objects.requireNonNull(exposed, "exposed");
+ }
+
+ ExposedPackages exposed() {
+ return exposed;
+ }
+
+ /** Whether this loader can load the class, judged by its class file so that nothing is loaded. */
+ boolean hasClass(String binaryName) {
+ return getResource(classFile(binaryName)) != null;
+ }
+
+ /** Whether the host's loader has the class, whether or not this loader shows it to packs. In
+ * production that loader also holds Iris internals and the libraries Iris bundles.
+ */
+ boolean hostHasClass(String binaryName) {
+ return hostLoader.getResource(classFile(binaryName)) != null;
+ }
+
+ @Override
+ protected Class> loadClass(String name, boolean resolve) throws ClassNotFoundException {
+ if (exposed.exposes(name)) return hostLoader.loadClass(name);
+ return super.loadClass(name, resolve);
+ }
+
+ @Override
+ public URL getResource(String name) {
+ if (exposed.exposesResource(name)) return hostLoader.getResource(name);
+ return super.getResource(name);
+ }
+
+ @Override
+ public Enumeration getResources(String name) throws IOException {
+ if (exposed.exposesResource(name)) return hostLoader.getResources(name);
+ return super.getResources(name);
+ }
+
+ private static String classFile(String binaryName) {
+ return binaryName.replace('.', '/') + CLASS_FILE_SUFFIX;
+ }
+}
diff --git a/src/main/java/com/volmit/host/ExposedPackages.java b/src/main/java/com/volmit/host/ExposedPackages.java
new file mode 100644
index 000000000..cda1329a8
--- /dev/null
+++ b/src/main/java/com/volmit/host/ExposedPackages.java
@@ -0,0 +1,88 @@
+package com.volmit.host;
+
+import com.volmit.IrisModule;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+import java.util.TreeSet;
+
+/** The host packages pack modules may use: always the Iris API, plus whatever the host adds, such
+ * as the packages of built-in modules whose stages packs may require. A package is exposed with
+ * all its subpackages, and only whole package names match: {@code com.volmit} does not expose
+ * {@code com.volmitx}.
+ */
+final class ExposedPackages {
+ /** The root package of the Iris API. Module classes must not use it. */
+ static final String API_PACKAGE = IrisModule.class.getPackageName();
+ private static final char PACKAGE_SEPARATOR = '.';
+ private static final char PATH_SEPARATOR = '/';
+ private static final String PACKAGE_SEPARATOR_PATTERN = "\\.";
+
+ private final List packages;
+
+ private ExposedPackages(List packages) {
+ this.packages = List.copyOf(packages);
+ }
+
+ static ExposedPackages apiOnly() {
+ return apiAnd(List.of());
+ }
+
+ /** The API plus {@code hostPackages}. A trailing dot is accepted; anything that is not a package
+ * name, such as a blank string or a wildcard, is rejected.
+ */
+ static ExposedPackages apiAnd(Collection hostPackages) {
+ Objects.requireNonNull(hostPackages, "hostPackages");
+ var packages = new TreeSet();
+ packages.add(API_PACKAGE);
+ hostPackages.forEach(name -> packages.add(requirePackageName(name)));
+ return new ExposedPackages(List.copyOf(packages));
+ }
+
+ static boolean isApiClass(String binaryName) {
+ return isInPackage(binaryName, API_PACKAGE);
+ }
+
+ boolean exposes(String binaryName) {
+ return packages.stream().anyMatch(name -> isInPackage(binaryName, name));
+ }
+
+ /** Whether a resource path, such as {@code com/volmit/IrisModule.class}, lies in an exposed package. */
+ boolean exposesResource(String path) {
+ return exposes(path.replace(PATH_SEPARATOR, PACKAGE_SEPARATOR));
+ }
+
+ /** The exposed package a class is in; the most specific one when exposed packages nest. */
+ String packageOf(String binaryName) {
+ return packages.stream()
+ .filter(name -> isInPackage(binaryName, name))
+ .max(Comparator.comparingInt(String::length))
+ .orElseThrow(() -> new IllegalArgumentException(binaryName + " is not in an exposed package"));
+ }
+
+ private static boolean isInPackage(String binaryName, String packageName) {
+ return binaryName.startsWith(packageName)
+ && binaryName.length() > packageName.length()
+ && binaryName.charAt(packageName.length()) == PACKAGE_SEPARATOR;
+ }
+
+ private static String requirePackageName(String name) {
+ Objects.requireNonNull(name, "exposed package");
+ var withoutTrailingDot = name.endsWith(String.valueOf(PACKAGE_SEPARATOR)) ? name.substring(0, name.length() - 1) : name;
+ if (isPackageName(withoutTrailingDot)) return withoutTrailingDot;
+ throw new IllegalArgumentException("Exposed package '" + name + "' is not a package name; name a package"
+ + " such as art.arcane.iris.engine, which exposes that package and its subpackages");
+ }
+
+ private static boolean isPackageName(String name) {
+ return Arrays.stream(name.split(PACKAGE_SEPARATOR_PATTERN, -1)).allMatch(ExposedPackages::isIdentifier);
+ }
+
+ private static boolean isIdentifier(String segment) {
+ return !segment.isEmpty()
+ && Character.isJavaIdentifierStart(segment.codePointAt(0))
+ && segment.codePoints().skip(1).allMatch(Character::isJavaIdentifierPart);
+ }
+}
diff --git a/src/main/java/com/volmit/host/ModuleJar.java b/src/main/java/com/volmit/host/ModuleJar.java
index cd36af894..831af0463 100644
--- a/src/main/java/com/volmit/host/ModuleJar.java
+++ b/src/main/java/com/volmit/host/ModuleJar.java
@@ -51,15 +51,13 @@ record ModuleJar(Path path, String origin, String entryClass, List cla
}
/** Reads the single entry class a jar names and the classes it holds. The origin is the jar
- * path relative to its pack. Fails if the jar holds a class of the Iris API or a class file that
- * cannot be parsed.
+ * path relative to its pack. Fails if a class file cannot be parsed.
*/
static ModuleJar read(Path packRoot, Path jar) {
var origin = packRoot.relativize(jar).toString();
try (var file = openAsClassLoaderSeesIt(jar)) {
var entryClass = requireSingleEntryClass(origin, readEntryClasses(file));
var classEntries = findClassEntries(file);
- requireNoApiClasses(origin, classEntries);
return new ModuleJar(jar, origin, entryClass, readClasses(origin, file, classEntries));
} catch (IOException exception) {
throw new UncheckedIOException("Module jar " + origin + " is not a readable jar", exception);
@@ -140,22 +138,6 @@ record ModuleJar(Path path, String origin, String entryClass, List cla
&& !entryName.startsWith(META_INF);
}
- /** The API package prefix is reserved for the API that Iris provides at runtime. The pack loader
- * asks the host for those classes first, so a packaged API class would be silently replaced by the
- * host's copy, and a module's own class there could be replaced by a later API class of that name.
- */
- private static void requireNoApiClasses(String origin, List classEntries) {
- var apiClasses = classEntries.stream()
- .map(entry -> toBinaryName(entry.getName()))
- .filter(ApiAndJdkClassLoader::isApiClass)
- .toList();
- if (apiClasses.isEmpty()) return;
- var others = apiClasses.size() == 1 ? "" : " and " + (apiClasses.size() - 1) + " more";
- throw new IllegalArgumentException("Module jar " + origin + " contains Iris API class " + apiClasses.getFirst()
- + others + "; Iris provides the API at runtime, so mark the Iris API dependency compileOnly"
- + " (Maven: provided) and keep module classes out of " + ApiAndJdkClassLoader.API_PACKAGE_PREFIX + "*");
- }
-
private static List readClasses(String origin, JarFile file, List classEntries) throws IOException {
var classes = new ArrayList(classEntries.size());
for (var entry : classEntries) {
diff --git a/src/main/java/com/volmit/host/PackClassCheck.java b/src/main/java/com/volmit/host/PackClassCheck.java
index 53ef87c73..784d835bf 100644
--- a/src/main/java/com/volmit/host/PackClassCheck.java
+++ b/src/main/java/com/volmit/host/PackClassCheck.java
@@ -10,13 +10,44 @@ import java.util.List;
final class PackClassCheck {
private PackClassCheck() {}
- /** Fails when a class is in two jars, or when a class uses a class that Iris hides from modules
- * or that is missing from the API. Returns the classes that nobody has, per jar; they fail only
- * when code that needs them runs.
+ /** Fails when a jar holds a class of an exposed host package, when a class is in two jars, or
+ * when a class uses a class that Iris hides from modules or that is missing from an exposed
+ * package. Returns the classes that nobody has, per jar; they fail only when code that needs them
+ * runs.
*/
- static List requireLinkable(List jars, ApiAndJdkClassLoader apiAndJdk) {
+ static List requireLinkable(List jars, ExposedHostClassLoader host) {
+ jars.forEach(jar -> requireNoHostClasses(jar, host.exposed()));
requireDistinctClasses(jars);
- return PackReferenceCheck.requireResolvable(jars, apiAndJdk);
+ return PackReferenceCheck.requireResolvable(jars, host);
+ }
+
+ /** Exposed packages belong to the host. The pack loader asks the host for their classes first, so
+ * a packaged copy would be silently replaced by the host's, and a module's own class there could be
+ * replaced by a later host class of that name.
+ */
+ private static void requireNoHostClasses(ModuleJar jar, ExposedPackages exposed) {
+ var hostClasses = jar.classNames().stream().filter(exposed::exposes).toList();
+ if (hostClasses.isEmpty()) return;
+ var apiClasses = hostClasses.stream().filter(ExposedPackages::isApiClass).toList();
+ throw apiClasses.isEmpty() ? shippedHostClasses(jar, hostClasses, exposed) : shippedApiClasses(jar, apiClasses);
+ }
+
+ private static IllegalArgumentException shippedApiClasses(ModuleJar jar, List apiClasses) {
+ return new IllegalArgumentException("Module jar " + jar.origin() + " contains Iris API class "
+ + firstAndCount(apiClasses) + "; Iris provides the API at runtime, so mark the Iris API dependency"
+ + " compileOnly (Maven: provided) and keep module classes out of " + ExposedPackages.API_PACKAGE + ".*");
+ }
+
+ private static IllegalArgumentException shippedHostClasses(ModuleJar jar, List hostClasses, ExposedPackages exposed) {
+ var hostPackage = exposed.packageOf(hostClasses.getFirst());
+ return new IllegalArgumentException("Module jar " + jar.origin() + " contains class " + firstAndCount(hostClasses)
+ + " of package " + hostPackage + ", which Iris provides to modules at runtime; compile against it"
+ + " compileOnly (Maven: provided) instead of shipping it, and keep module classes out of " + hostPackage + ".*");
+ }
+
+ private static String firstAndCount(List classNames) {
+ var others = classNames.size() == 1 ? "" : " and " + (classNames.size() - 1) + " more";
+ return classNames.getFirst() + others;
}
/** The shared pack loader would otherwise silently use whichever copy comes first in file name order. */
diff --git a/src/main/java/com/volmit/host/PackModules.java b/src/main/java/com/volmit/host/PackModules.java
index 734e5bfec..1f2a4c358 100644
--- a/src/main/java/com/volmit/host/PackModules.java
+++ b/src/main/java/com/volmit/host/PackModules.java
@@ -8,6 +8,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
@@ -17,9 +18,11 @@ import java.util.Objects;
*
* All jars of a pack share one class loader. A pack jar can therefore name another pack jar's
* stage constants in {@code requires(...)}. Besides the pack's own classes, that loader sees only
- * the Iris API and the JDK ({@link ApiAndJdkClassLoader}); packs do not see each other or Iris
- * internals. Before the loader exists, the load fails if a jar holds API classes, if a class is in
- * two jars, or if a class uses a class that Iris hides from modules or that is missing from the API.
+ * the exposed host packages and the JDK ({@link ExposedHostClassLoader}); packs do not see each
+ * other or Iris internals. The Iris API is always exposed; the host may expose more packages, such
+ * as those of built-in modules whose stages packs may require. Before the loader exists, the load
+ * fails if a jar holds classes of an exposed package, if a class is in two jars, or if a class uses
+ * a class that Iris hides from modules or that is missing from an exposed package.
* Classes that nobody has are not an error, because the JVM only needs them when code that uses
* them runs; they are reported as {@link #missingClasses()}.
*
@@ -44,17 +47,27 @@ public final class PackModules implements AutoCloseable {
this.loader = loader;
}
+ /** Loads the modules of the pack at {@code packRoot}, exposing only the Iris API. */
+ public static PackModules load(Path packRoot) {
+ return load(packRoot, List.of());
+ }
+
/** Loads the modules of the pack at {@code packRoot}. A pack without a modules directory has
* no modules. Errors name the offending jar relative to the pack root.
+ *
+ * @param exposedHostPackages host packages pack modules may use besides the Iris API, which is
+ * always exposed, such as the packages of built-in modules whose stages packs may require.
+ * Each exposes the package and its subpackages from the loader that defines the API. A
+ * trailing dot is accepted; blank names and wildcards are rejected.
*/
- public static PackModules load(Path packRoot) {
+ public static PackModules load(Path packRoot, Collection exposedHostPackages) {
Objects.requireNonNull(packRoot, "packRoot");
+ var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages));
var jars = findModuleJars(packRoot).stream()
.map(jar -> ModuleJar.read(packRoot, jar))
.toList();
- var apiAndJdk = new ApiAndJdkClassLoader(IrisModule.class.getClassLoader());
- var missingClasses = PackClassCheck.requireLinkable(jars, apiAndJdk);
- var packModules = defineModules(packRoot, jars, missingClasses, apiAndJdk);
+ var missingClasses = PackClassCheck.requireLinkable(jars, host);
+ var packModules = defineModules(packRoot, jars, missingClasses, host);
logLoad(packName(packRoot), packModules);
return packModules;
}
@@ -98,8 +111,8 @@ public final class PackModules implements AutoCloseable {
}
private static PackModules defineModules(
- Path packRoot, List jars, List missingClasses, ClassLoader apiAndJdk) {
- var loader = newPackClassLoader(packRoot, jars, apiAndJdk);
+ Path packRoot, List jars, List missingClasses, ClassLoader host) {
+ var loader = newPackClassLoader(packRoot, jars, host);
try {
return new PackModules(createModules(jars, loader), missingClasses, loader);
} catch (RuntimeException | Error failure) {
@@ -108,9 +121,9 @@ public final class PackModules implements AutoCloseable {
}
}
- private static URLClassLoader newPackClassLoader(Path packRoot, List jars, ClassLoader apiAndJdk) {
+ private static URLClassLoader newPackClassLoader(Path packRoot, List jars, ClassLoader host) {
var urls = jars.stream().map(ModuleJar::url).toArray(URL[]::new);
- return new URLClassLoader("pack:" + packName(packRoot), urls, apiAndJdk);
+ return new URLClassLoader("pack:" + packName(packRoot), urls, host);
}
private static void logLoad(String packName, PackModules packModules) {
diff --git a/src/main/java/com/volmit/host/PackReferenceCheck.java b/src/main/java/com/volmit/host/PackReferenceCheck.java
index 11298c979..0d97b2fc9 100644
--- a/src/main/java/com/volmit/host/PackReferenceCheck.java
+++ b/src/main/java/com/volmit/host/PackReferenceCheck.java
@@ -22,7 +22,7 @@ final class PackReferenceCheck {
/** Where the pack loader would find a referenced class. */
private enum Resolution {
- /** In the pack, a package the host exposes, or the JDK. */
+ /** In the pack, an exposed host package, or the JDK. */
PROVIDED,
/** Nowhere: an optional dependency, which only fails when code that needs it runs. */
ABSENT,
@@ -53,9 +53,9 @@ final class PackReferenceCheck {
private final Set packClasses = new HashSet<>();
private final Map outsidePack = new HashMap<>();
- private final ApiAndJdkClassLoader host;
+ private final ExposedHostClassLoader host;
- private PackReferenceCheck(List jars, ApiAndJdkClassLoader host) {
+ private PackReferenceCheck(List jars, ExposedHostClassLoader host) {
this.host = host;
jars.forEach(jar -> packClasses.addAll(jar.classNames()));
}
@@ -63,7 +63,7 @@ final class PackReferenceCheck {
/** Fails when a jar uses a class Iris hides or a class missing from a package Iris exposes.
* Returns the classes nobody has, per jar, for jars that use any.
*/
- static List requireResolvable(List jars, ApiAndJdkClassLoader host) {
+ static List requireResolvable(List jars, ExposedHostClassLoader host) {
var check = new PackReferenceCheck(jars, host);
var references = jars.stream().map(check::resolve).toList();
requireNoFailures(references);
@@ -88,7 +88,7 @@ final class PackReferenceCheck {
private Resolution resolveOutsidePack(String className) {
if (host.hasClass(className)) return Resolution.PROVIDED;
- if (ApiAndJdkClassLoader.isApiClass(className)) return Resolution.MISSING_HOST_CLASS;
+ if (host.exposed().exposes(className)) return Resolution.MISSING_HOST_CLASS;
if (host.hostHasClass(className)) return Resolution.HIDDEN_HOST_CLASS;
return Resolution.ABSENT;
}
diff --git a/src/test/java/com/volmit/host/ApiAndJdkClassLoaderTest.java b/src/test/java/com/volmit/host/ExposedHostClassLoaderTest.java
similarity index 66%
rename from src/test/java/com/volmit/host/ApiAndJdkClassLoaderTest.java
rename to src/test/java/com/volmit/host/ExposedHostClassLoaderTest.java
index 546c61fa8..e087b78cd 100644
--- a/src/test/java/com/volmit/host/ApiAndJdkClassLoaderTest.java
+++ b/src/test/java/com/volmit/host/ExposedHostClassLoaderTest.java
@@ -1,16 +1,17 @@
package com.volmit.host;
import com.volmit.IrisModule;
+import example.builtin.CarveModule;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
-class ApiAndJdkClassLoaderTest {
+class ExposedHostClassLoaderTest {
private static final ClassLoader HOST = IrisModule.class.getClassLoader();
private static final String HOST_LIBRARY_SERVICE_FILE = "META-INF/services/org.junit.platform.engine.TestEngine";
- private final ApiAndJdkClassLoader loader = new ApiAndJdkClassLoader(HOST);
+ private final ExposedHostClassLoader loader = new ExposedHostClassLoader(HOST, ExposedPackages.apiOnly());
private static String classFile(Class> type) {
return type.getName().replace('.', '/') + ".class";
@@ -24,9 +25,21 @@ class ApiAndJdkClassLoaderTest {
}
@Test
- void hidesHostClassesOutsideTheApi() {
+ void hidesHostClassesOutsideTheExposedPackages() {
assertDoesNotThrow(() -> HOST.loadClass(Test.class.getName()));
assertThrows(ClassNotFoundException.class, () -> loader.loadClass(Test.class.getName()));
+ assertThrows(ClassNotFoundException.class, () -> loader.loadClass(CarveModule.class.getName()));
+ }
+
+ @Test
+ void loadsClassesAndResourcesOfExtraExposedPackagesFromTheHost() throws Exception {
+ var exposing = new ExposedHostClassLoader(HOST, ExposedPackages.apiAnd(List.of(CarveModule.class.getPackageName())));
+
+ assertSame(CarveModule.class, exposing.loadClass(CarveModule.class.getName()));
+ assertSame(IrisModule.class, exposing.loadClass(IrisModule.class.getName()));
+ assertNotNull(exposing.getResource(classFile(CarveModule.class)));
+ assertTrue(exposing.hasClass(CarveModule.class.getName()));
+ assertThrows(ClassNotFoundException.class, () -> exposing.loadClass(Test.class.getName()));
}
@Test
@@ -34,6 +47,7 @@ class ApiAndJdkClassLoaderTest {
assertNotNull(loader.getResource(classFile(IrisModule.class)));
assertNotNull(loader.getResource(classFile(List.class)));
assertNull(loader.getResource(classFile(Test.class)));
+ assertNull(loader.getResource(classFile(CarveModule.class)));
assertFalse(Collections.list(HOST.getResources(HOST_LIBRARY_SERVICE_FILE)).isEmpty());
assertTrue(Collections.list(loader.getResources(HOST_LIBRARY_SERVICE_FILE)).isEmpty());
}
diff --git a/src/test/java/com/volmit/host/ExposedPackagesTest.java b/src/test/java/com/volmit/host/ExposedPackagesTest.java
new file mode 100644
index 000000000..d34de5f2f
--- /dev/null
+++ b/src/test/java/com/volmit/host/ExposedPackagesTest.java
@@ -0,0 +1,64 @@
+package com.volmit.host;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import java.util.List;
+import static org.junit.jupiter.api.Assertions.*;
+
+class ExposedPackagesTest {
+ @Test
+ void apiIsAlwaysExposedWithItsSubpackages() {
+ var exposed = ExposedPackages.apiOnly();
+
+ assertEquals("com.volmit", ExposedPackages.API_PACKAGE);
+ assertTrue(exposed.exposes("com.volmit.IrisModule"));
+ assertTrue(exposed.exposes("com.volmit.host.PackModules"));
+ assertTrue(ExposedPackages.apiAnd(List.of("example.builtin")).exposes("com.volmit.IrisModule"));
+ }
+
+ @Test
+ void onlyWholePackageNamesMatch() {
+ var exposed = ExposedPackages.apiAnd(List.of("example.builtin"));
+
+ assertTrue(exposed.exposes("example.builtin.CarveModule"));
+ assertTrue(exposed.exposes("example.builtin.deep.Stage"));
+ assertFalse(exposed.exposes("example.builtinx.CarveModule"));
+ assertFalse(exposed.exposes("example.Other"));
+ assertFalse(exposed.exposes("com.volmitx.Fake"));
+ assertFalse(exposed.exposes("com.volmit"));
+ }
+
+ @Test
+ void resourcesMatchByPath() {
+ var exposed = ExposedPackages.apiAnd(List.of("example.builtin"));
+
+ assertTrue(exposed.exposesResource("example/builtin/CarveModule.class"));
+ assertTrue(exposed.exposesResource("example/builtin/carve.json"));
+ assertTrue(exposed.exposesResource("com/volmit/IrisModule.class"));
+ assertFalse(exposed.exposesResource("example/builtinx/carve.json"));
+ assertFalse(exposed.exposesResource("META-INF/services/com.volmit.IrisModule"));
+ }
+
+ @Test
+ void trailingDotIsAccepted() {
+ assertTrue(ExposedPackages.apiAnd(List.of("example.builtin.")).exposes("example.builtin.CarveModule"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"", " ", ".", "example..builtin", ".example", "example.*", "example.builtin.**", "1example", "example builtin"})
+ void rejectsWhatIsNotAPackageName(String name) {
+ var message = assertThrows(IllegalArgumentException.class, () -> ExposedPackages.apiAnd(List.of(name))).getMessage();
+
+ assertTrue(message.startsWith("Exposed package '" + name + "' is not a package name"), message);
+ }
+
+ @Test
+ void namesTheMostSpecificExposedPackageOfAClass() {
+ var exposed = ExposedPackages.apiAnd(List.of("example", "example.builtin"));
+
+ assertEquals("example.builtin", exposed.packageOf("example.builtin.CarveModule"));
+ assertEquals("example", exposed.packageOf("example.Other"));
+ assertEquals("com.volmit", exposed.packageOf("com.volmit.IrisModule"));
+ }
+}
diff --git a/src/test/java/com/volmit/host/PackModulesTest.java b/src/test/java/com/volmit/host/PackModulesTest.java
index 1cd68f555..c865c688e 100644
--- a/src/test/java/com/volmit/host/PackModulesTest.java
+++ b/src/test/java/com/volmit/host/PackModulesTest.java
@@ -4,6 +4,7 @@ import com.volmit.IrisModule;
import com.volmit.example.placement.AnchorModule;
import com.volmit.example.placement.PlacementModules;
import com.volmit.support.TestModuleJar;
+import example.builtin.CarveModule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;
@@ -26,6 +27,9 @@ class PackModulesTest {
private static final String VERSIONED_SHARED_CLASS = "META-INF/versions/9/pack/shared/Util.class";
private static final String DEFINED_PROPERTY = "irisapi.test.packModuleConstructed";
private static final String OPTIONAL_CLASS = "org.optional.Feature";
+ private static final String BUILT_IN_PACKAGE = CarveModule.class.getPackageName();
+ private static final String REQUIRES_BUILT_IN_STAGE =
+ "graph.define(Stage.placement(\"user:core/carved\")).requires(example.builtin.CarveModule.CLEAR).run(context -> {});";
@TempDir
Path pack;
@@ -346,6 +350,63 @@ class PackModulesTest {
}
}
+ @Test
+ void packJarRequiresAStageOfABuiltInModuleInAnExposedPackage() {
+ module("pack.user.User", "user:core", NO_MEMBERS, REQUIRES_BUILT_IN_STAGE)
+ .compileAgainstHostOf(CarveModule.class)
+ .writeInto(pack, "user.jar");
+
+ try (var packModules = PackModules.load(pack, List.of(BUILT_IN_PACKAGE))) {
+ var modules = new ArrayList<>(List.of(DiscoveredModule.builtIn(new CarveModule())));
+ modules.addAll(packModules.modules());
+ var order = LoadedModules.load(modules, Map.of()).placement().stages().stream()
+ .map(stage -> stage.stage().id().toString()).toList();
+ assertEquals(List.of("iris:carve/clear", "user:core/carved"), order);
+ }
+ }
+
+ @Test
+ void builtInModuleOutsideTheExposedPackagesIsHidden() {
+ module("pack.user.User", "user:core", NO_MEMBERS, REQUIRES_BUILT_IN_STAGE)
+ .compileAgainstHostOf(CarveModule.class)
+ .writeInto(pack, "user.jar");
+
+ assertMentions(failure(() -> PackModules.load(pack)),
+ "Module jar " + origin("user.jar") + " uses classes that exist in Iris but are hidden from modules:\n"
+ + " pack.user.User -> example.builtin.CarveModule\n");
+ }
+
+ @Test
+ void referenceToAMissingClassOfAnExposedPackagePointsAtADifferentBuild(@TempDir Path build) {
+ var newerBuiltIns = new TestModuleJar()
+ .source("example.builtin.Newer", "package example.builtin; public final class Newer {}")
+ .writeInto(build, "newer-built-ins.jar");
+ module("pack.user.User", "user:core", "static Object probe() { return example.builtin.Newer.class; }", NO_STAGES)
+ .compileAgainst(newerBuiltIns)
+ .writeInto(pack, "user.jar");
+
+ assertMentions(failure(() -> PackModules.load(pack, List.of(BUILT_IN_PACKAGE))),
+ "pack.user.User -> example.builtin.Newer", "built against a different Iris API or Iris version");
+ }
+
+ @Test
+ void classOfAnExposedHostPackageInAJarFails() {
+ module("pack.alpha.Alpha", "alpha:core", NO_MEMBERS, NO_STAGES)
+ .source("example.builtin.Copy", "package example.builtin; public final class Copy {}")
+ .writeInto(pack, "alpha.jar");
+
+ assertEquals("Module jar " + origin("alpha.jar") + " contains class example.builtin.Copy of package example.builtin,"
+ + " which Iris provides to modules at runtime; compile against it compileOnly (Maven: provided)"
+ + " instead of shipping it, and keep module classes out of example.builtin.*",
+ failure(() -> PackModules.load(pack, List.of(BUILT_IN_PACKAGE))));
+ }
+
+ @Test
+ void invalidExposedPackageFailsBeforeReadingJars() {
+ assertMentions(failure(() -> PackModules.load(pack, List.of("example.*"))),
+ "Exposed package 'example.*' is not a package name");
+ }
+
@Test
void methodMissingAtInstallFailsNamingTheJar(@TempDir Path build) {
var libraryWithMethod = new TestModuleJar()
diff --git a/src/test/java/example/builtin/CarveModule.java b/src/test/java/example/builtin/CarveModule.java
new file mode 100644
index 000000000..64a03b9ae
--- /dev/null
+++ b/src/test/java/example/builtin/CarveModule.java
@@ -0,0 +1,24 @@
+package example.builtin;
+
+import com.volmit.IrisModule;
+import com.volmit.ModuleInfo;
+import com.volmit.stage.PlacementStage;
+import com.volmit.stage.Stage;
+import com.volmit.stage.StageGraph;
+
+/** A built-in module outside the API's package, like the engine modules inside Iris. Pack jars can
+ * require its stages only when the host exposes this package.
+ */
+public final class CarveModule implements IrisModule {
+ public static final PlacementStage CLEAR = Stage.placement("iris:carve/clear");
+
+ @Override
+ public ModuleInfo info() {
+ return ModuleInfo.of("iris:carve", "Carve", "test");
+ }
+
+ @Override
+ public void install(StageGraph graph) {
+ graph.define(CLEAR).run(context -> {});
+ }
+}