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; } }