Files
Iris/src/main/java/com/volmit/host/ExposedHostClassLoader.java
T
piexel 13a9300ab4 Let the host expose extra packages, such as built-in modules, to packs
A single API_PACKAGE_PREFIX made built-in modules in Iris packages
(art.arcane.iris...) invisible, so pack jars could not require their
stages. ExposedPackages now holds the packages packs may use: always
the API (IrisModule's package), plus what the host passes to the new
PackModules.load(packRoot, exposedHostPackages). Names must be package
names (trailing dot accepted) and match whole packages only.

ApiAndJdkClassLoader becomes ExposedHostClassLoader and routes classes
and resources of every exposed package to the host loader. A jar that
ships a class of any exposed package fails; the compileOnly API hint
stays for the API, other packages get their own message. That check
moves from ModuleJar to PackClassCheck. A reference missing from any
exposed package is the "different build" error.

Tests use a built-in module in example.builtin, outside com.volmit.,
which a pack jar can require only when that package is exposed.
2026-10-05 20:30:20 +02:00

77 lines
3.0 KiB
Java

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.
*
* <p>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.
*
* <p>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<URL> 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;
}
}