mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Let pack jars use classes nobody has, as optional dependencies
The class check was stricter than the JVM: a bundled library that references optional integrations failed the whole pack, although that code only runs when the classes exist. Each missing reference is now classified: - the host has the class but hides it (Iris internals, Iris's copy of a library): load error, saying modules cannot use Iris's classes; - missing from the API: load error, as before (different Iris build); - nobody has it: the pack loads. PackModules.missingClasses() lists the classes per jar and the load logs one capped WARNING per pack, formatted by MissingClassesWarning. Reference resolution moves from PackClassCheck to PackReferenceCheck.
This commit is contained in:
1 parent
b7b3993a8d
commit
98bd9a9c12
9 files changed
+326
-91
No files matched your search
@@ -40,7 +40,14 @@ final class ApiAndJdkClassLoader extends ClassLoader {
|
||||
|
||||
/** Whether this loader can load the class, judged by its class file so that nothing is loaded. */
|
||||
boolean hasClass(String binaryName) {
|
||||
return getResource(binaryName.replace('.', '/') + CLASS_FILE_SUFFIX) != null;
|
||||
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
|
||||
@@ -61,6 +68,10 @@ final class ApiAndJdkClassLoader extends ClassLoader {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Classes a module jar uses that neither its pack, Iris nor the JDK has, usually optional
|
||||
* dependencies of a library the jar bundles. The jar still loads; code that needs one of these
|
||||
* classes throws {@link NoClassDefFoundError} when it runs.
|
||||
*
|
||||
* @param origin the jar path relative to its pack
|
||||
* @param classNames the missing classes as sorted binary names
|
||||
*/
|
||||
public record MissingClasses(String origin, List<String> classNames) {
|
||||
public MissingClasses {
|
||||
Objects.requireNonNull(origin, "origin");
|
||||
classNames = List.copyOf(classNames);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** The one warning a pack load writes when its jars use classes nobody has. One line per jar,
|
||||
* with a capped list of classes, so a large bundled library cannot flood the log.
|
||||
*/
|
||||
final class MissingClassesWarning {
|
||||
static final int MAX_LISTED_CLASSES_PER_JAR = 5;
|
||||
private static final String INDENT = " ";
|
||||
|
||||
private MissingClassesWarning() {}
|
||||
|
||||
/** Expects at least one jar; a pack without missing classes logs no warning. */
|
||||
static String describe(String packName, List<MissingClasses> jars) {
|
||||
var text = new StringBuilder("Pack ").append(packName)
|
||||
.append(": module jars use classes that neither the pack, Iris nor the JDK has, usually optional"
|
||||
+ " dependencies of a bundled library. The jars load, but code that needs these classes throws"
|
||||
+ " NoClassDefFoundError when it runs:");
|
||||
for (var jar : jars) {
|
||||
text.append('\n').append(INDENT).append(jar.origin()).append(": ").append(listClasses(jar.classNames()));
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
private static String listClasses(List<String> classNames) {
|
||||
var listed = String.join(", ", classNames.subList(0, Math.min(classNames.size(), MAX_LISTED_CLASSES_PER_JAR)));
|
||||
int unlisted = classNames.size() - MAX_LISTED_CLASSES_PER_JAR;
|
||||
return unlisted > 0 ? listed + " and " + unlisted + " more" : listed;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,22 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** Checks the classes of all jars of one pack before its class loader exists, so a pack that
|
||||
* cannot link fails at load with the jar to fix, not later with a {@link NoClassDefFoundError}
|
||||
* during install or generation.
|
||||
*/
|
||||
final class PackClassCheck {
|
||||
private static final int MAX_REPORTED_REFERENCES_PER_JAR = 10;
|
||||
private static final String INDENT = " ";
|
||||
|
||||
private PackClassCheck() {}
|
||||
|
||||
/** Fails when a class is in two jars, or when a class uses a class that is neither in the API,
|
||||
* the JDK nor a jar of the pack.
|
||||
/** 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.
|
||||
*/
|
||||
static void requireLinkable(List<ModuleJar> jars, ApiAndJdkClassLoader apiAndJdk) {
|
||||
static List<MissingClasses> requireLinkable(List<ModuleJar> jars, ApiAndJdkClassLoader apiAndJdk) {
|
||||
requireDistinctClasses(jars);
|
||||
requireResolvableReferences(jars, apiAndJdk);
|
||||
return PackReferenceCheck.requireResolvable(jars, apiAndJdk);
|
||||
}
|
||||
|
||||
/** The shared pack loader would otherwise silently use whichever copy comes first in file name order. */
|
||||
@@ -40,71 +35,4 @@ final class PackClassCheck {
|
||||
+ " 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 void requireResolvableReferences(List<ModuleJar> jars, ApiAndJdkClassLoader apiAndJdk) {
|
||||
var resolver = new ReferenceResolver(jars, apiAndJdk);
|
||||
var unresolved = jars.stream()
|
||||
.map(jar -> new UnresolvedReferences(jar, resolver.unresolvedIn(jar)))
|
||||
.filter(UnresolvedReferences::any)
|
||||
.toList();
|
||||
if (!unresolved.isEmpty()) throw unresolvedReferences(unresolved);
|
||||
}
|
||||
|
||||
private static IllegalArgumentException unresolvedReferences(List<UnresolvedReferences> jars) {
|
||||
var text = new StringBuilder();
|
||||
for (var jar : jars) jar.describeTo(text);
|
||||
text.append("A module can use only the Iris API, the JDK and the classes in its pack's module jars;"
|
||||
+ " ship any library it needs inside a module jar of the pack.");
|
||||
if (jars.stream().anyMatch(UnresolvedReferences::missesApiClasses)) {
|
||||
text.append(" A missing ").append(ApiAndJdkClassLoader.API_PACKAGE_PREFIX)
|
||||
.append("* class means the jar was built against a different Iris API.");
|
||||
}
|
||||
return new IllegalArgumentException(text.toString());
|
||||
}
|
||||
|
||||
/** Decides per referenced class whether the pack loader will find it. Classes outside the pack
|
||||
* are looked up once per pack.
|
||||
*/
|
||||
private static final class ReferenceResolver {
|
||||
private final Set<String> packClasses = new HashSet<>();
|
||||
private final Map<String, Boolean> outsidePack = new HashMap<>();
|
||||
private final ApiAndJdkClassLoader apiAndJdk;
|
||||
|
||||
ReferenceResolver(List<ModuleJar> jars, ApiAndJdkClassLoader apiAndJdk) {
|
||||
this.apiAndJdk = apiAndJdk;
|
||||
jars.forEach(jar -> packClasses.addAll(jar.classNames()));
|
||||
}
|
||||
|
||||
List<Reference> unresolvedIn(ModuleJar jar) {
|
||||
return jar.classes().stream()
|
||||
.flatMap(type -> type.references().stream().map(target -> new Reference(type.name(), target)))
|
||||
.filter(reference -> !resolves(reference.target()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private boolean resolves(String className) {
|
||||
return packClasses.contains(className) || outsidePack.computeIfAbsent(className, apiAndJdk::hasClass);
|
||||
}
|
||||
}
|
||||
|
||||
private record Reference(String source, String target) {}
|
||||
|
||||
private record UnresolvedReferences(ModuleJar jar, List<Reference> references) {
|
||||
boolean any() {
|
||||
return !references.isEmpty();
|
||||
}
|
||||
|
||||
boolean missesApiClasses() {
|
||||
return references.stream().anyMatch(reference -> ApiAndJdkClassLoader.isApiClass(reference.target()));
|
||||
}
|
||||
|
||||
void describeTo(StringBuilder text) {
|
||||
text.append("Module jar ").append(jar.origin())
|
||||
.append(" uses classes that are not in the Iris API, the JDK or the pack's module jars:\n");
|
||||
references.stream().limit(MAX_REPORTED_REFERENCES_PER_JAR).forEach(reference -> text.append(INDENT)
|
||||
.append(reference.source()).append(" -> ").append(reference.target()).append('\n'));
|
||||
int hidden = references.size() - MAX_REPORTED_REFERENCES_PER_JAR;
|
||||
if (hidden > 0) text.append(INDENT).append("and ").append(hidden).append(" more\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,10 +19,12 @@ import java.util.Objects;
|
||||
* 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 the loader would not find.
|
||||
* two jars, or if a class uses a class that Iris hides from modules or that is missing from the API.
|
||||
* 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()}.
|
||||
*
|
||||
* <p>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.
|
||||
* which the host can route to its own logging, and one WARNING listing the missing classes, if any.
|
||||
*
|
||||
* <p>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.
|
||||
@@ -33,10 +35,12 @@ public final class PackModules implements AutoCloseable {
|
||||
private static final System.Logger LOGGER = System.getLogger(PackModules.class.getName());
|
||||
|
||||
private final List<DiscoveredModule> modules;
|
||||
private final List<MissingClasses> missingClasses;
|
||||
private final URLClassLoader loader;
|
||||
|
||||
private PackModules(List<DiscoveredModule> modules, URLClassLoader loader) {
|
||||
private PackModules(List<DiscoveredModule> modules, List<MissingClasses> missingClasses, URLClassLoader loader) {
|
||||
this.modules = List.copyOf(modules);
|
||||
this.missingClasses = List.copyOf(missingClasses);
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
@@ -49,9 +53,9 @@ public final class PackModules implements AutoCloseable {
|
||||
.map(jar -> ModuleJar.read(packRoot, jar))
|
||||
.toList();
|
||||
var apiAndJdk = new ApiAndJdkClassLoader(IrisModule.class.getClassLoader());
|
||||
PackClassCheck.requireLinkable(jars, apiAndJdk);
|
||||
var packModules = defineModules(packRoot, jars, apiAndJdk);
|
||||
LOGGER.log(Level.INFO, () -> PackModulesSummary.describe(packName(packRoot), packModules.modules()));
|
||||
var missingClasses = PackClassCheck.requireLinkable(jars, apiAndJdk);
|
||||
var packModules = defineModules(packRoot, jars, missingClasses, apiAndJdk);
|
||||
logLoad(packName(packRoot), packModules);
|
||||
return packModules;
|
||||
}
|
||||
|
||||
@@ -60,6 +64,13 @@ public final class PackModules implements AutoCloseable {
|
||||
return modules;
|
||||
}
|
||||
|
||||
/** Per jar that uses any, in jar file name order, the classes that neither the pack, Iris nor the
|
||||
* JDK has. Empty when every class the jars use exists.
|
||||
*/
|
||||
public List<MissingClasses> missingClasses() {
|
||||
return missingClasses;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
@@ -86,10 +97,11 @@ public final class PackModules implements AutoCloseable {
|
||||
return Files.isRegularFile(path) && path.getFileName().toString().endsWith(JAR_EXTENSION);
|
||||
}
|
||||
|
||||
private static PackModules defineModules(Path packRoot, List<ModuleJar> jars, ClassLoader apiAndJdk) {
|
||||
private static PackModules defineModules(
|
||||
Path packRoot, List<ModuleJar> jars, List<MissingClasses> missingClasses, ClassLoader apiAndJdk) {
|
||||
var loader = newPackClassLoader(packRoot, jars, apiAndJdk);
|
||||
try {
|
||||
return new PackModules(createModules(jars, loader), loader);
|
||||
return new PackModules(createModules(jars, loader), missingClasses, loader);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
closeAfterFailure(loader, failure);
|
||||
throw failure;
|
||||
@@ -101,6 +113,12 @@ public final class PackModules implements AutoCloseable {
|
||||
return new URLClassLoader("pack:" + packName(packRoot), urls, apiAndJdk);
|
||||
}
|
||||
|
||||
private static void logLoad(String packName, PackModules packModules) {
|
||||
LOGGER.log(Level.INFO, () -> PackModulesSummary.describe(packName, packModules.modules()));
|
||||
if (packModules.missingClasses().isEmpty()) return;
|
||||
LOGGER.log(Level.WARNING, () -> MissingClassesWarning.describe(packName, packModules.missingClasses()));
|
||||
}
|
||||
|
||||
private static String packName(Path packRoot) {
|
||||
var name = packRoot.getFileName();
|
||||
return name == null ? packRoot.toString() : name.toString();
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Decides for every class a pack's jars use whether the pack loader will find it, and why not
|
||||
* when it won't. Nothing is loaded: the host's classes are judged by their class files.
|
||||
*
|
||||
* <p>The JVM resolves a class only when code first needs it, so a class nobody has is not an
|
||||
* error: bundled libraries reference optional dependencies they only use when present. A class
|
||||
* that Iris has but hides, or that is missing from a package Iris exposes, always fails the load.
|
||||
*/
|
||||
final class PackReferenceCheck {
|
||||
private static final int MAX_REPORTED_REFERENCES_PER_JAR = 10;
|
||||
private static final String INDENT = " ";
|
||||
|
||||
/** Where the pack loader would find a referenced class. */
|
||||
private enum Resolution {
|
||||
/** In the pack, a package the host exposes, or the JDK. */
|
||||
PROVIDED,
|
||||
/** Nowhere: an optional dependency, which only fails when code that needs it runs. */
|
||||
ABSENT,
|
||||
HIDDEN_HOST_CLASS(
|
||||
"uses classes that exist in Iris but are hidden from modules:",
|
||||
"Modules cannot use Iris's own classes or its copies of libraries; ship the library in a module jar"
|
||||
+ " of the pack, or use the Iris API."),
|
||||
MISSING_HOST_CLASS(
|
||||
"uses classes that this Iris does not have, in packages Iris provides to modules:",
|
||||
"The jar was built against a different Iris API or Iris version.");
|
||||
|
||||
private final String header;
|
||||
private final String advice;
|
||||
|
||||
Resolution() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
Resolution(String header, String advice) {
|
||||
this.header = header;
|
||||
this.advice = advice;
|
||||
}
|
||||
|
||||
boolean failsLoad() {
|
||||
return header != null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Set<String> packClasses = new HashSet<>();
|
||||
private final Map<String, Resolution> outsidePack = new HashMap<>();
|
||||
private final ApiAndJdkClassLoader host;
|
||||
|
||||
private PackReferenceCheck(List<ModuleJar> jars, ApiAndJdkClassLoader host) {
|
||||
this.host = host;
|
||||
jars.forEach(jar -> packClasses.addAll(jar.classNames()));
|
||||
}
|
||||
|
||||
/** 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<MissingClasses> requireResolvable(List<ModuleJar> jars, ApiAndJdkClassLoader host) {
|
||||
var check = new PackReferenceCheck(jars, host);
|
||||
var references = jars.stream().map(check::resolve).toList();
|
||||
requireNoFailures(references);
|
||||
return references.stream()
|
||||
.filter(JarReferences::missesClasses)
|
||||
.map(JarReferences::missingClasses)
|
||||
.toList();
|
||||
}
|
||||
|
||||
private JarReferences resolve(ModuleJar jar) {
|
||||
var references = jar.classes().stream()
|
||||
.flatMap(type -> type.references().stream().map(target -> new Reference(type.name(), target, resolve(target))))
|
||||
.filter(reference -> reference.resolution() != Resolution.PROVIDED)
|
||||
.toList();
|
||||
return new JarReferences(jar, references);
|
||||
}
|
||||
|
||||
private Resolution resolve(String className) {
|
||||
if (packClasses.contains(className)) return Resolution.PROVIDED;
|
||||
return outsidePack.computeIfAbsent(className, this::resolveOutsidePack);
|
||||
}
|
||||
|
||||
private Resolution resolveOutsidePack(String className) {
|
||||
if (host.hasClass(className)) return Resolution.PROVIDED;
|
||||
if (ApiAndJdkClassLoader.isApiClass(className)) return Resolution.MISSING_HOST_CLASS;
|
||||
if (host.hostHasClass(className)) return Resolution.HIDDEN_HOST_CLASS;
|
||||
return Resolution.ABSENT;
|
||||
}
|
||||
|
||||
private static void requireNoFailures(List<JarReferences> jars) {
|
||||
var failures = Arrays.stream(Resolution.values())
|
||||
.filter(Resolution::failsLoad)
|
||||
.map(failure -> describeFailure(failure, jars))
|
||||
.filter(Predicate.not(String::isEmpty))
|
||||
.toList();
|
||||
if (failures.isEmpty()) return;
|
||||
throw new IllegalArgumentException(String.join("\n", failures));
|
||||
}
|
||||
|
||||
private static String describeFailure(Resolution failure, List<JarReferences> jars) {
|
||||
var text = new StringBuilder();
|
||||
for (var jar : jars) jar.describeTo(failure, text);
|
||||
if (text.isEmpty()) return "";
|
||||
return text.append(failure.advice).toString();
|
||||
}
|
||||
|
||||
private record Reference(String source, String target, Resolution resolution) {}
|
||||
|
||||
/** The references of one jar the pack loader would not resolve. */
|
||||
private record JarReferences(ModuleJar jar, List<Reference> unresolved) {
|
||||
boolean missesClasses() {
|
||||
return unresolved.stream().anyMatch(reference -> reference.resolution() == Resolution.ABSENT);
|
||||
}
|
||||
|
||||
MissingClasses missingClasses() {
|
||||
var classNames = new TreeSet<String>();
|
||||
with(Resolution.ABSENT).forEach(reference -> classNames.add(reference.target()));
|
||||
return new MissingClasses(jar.origin(), List.copyOf(classNames));
|
||||
}
|
||||
|
||||
void describeTo(Resolution failure, StringBuilder text) {
|
||||
var references = with(failure);
|
||||
if (references.isEmpty()) return;
|
||||
text.append("Module jar ").append(jar.origin()).append(' ').append(failure.header).append('\n');
|
||||
references.stream().limit(MAX_REPORTED_REFERENCES_PER_JAR).forEach(reference -> text.append(INDENT)
|
||||
.append(reference.source()).append(" -> ").append(reference.target()).append('\n'));
|
||||
int hidden = references.size() - MAX_REPORTED_REFERENCES_PER_JAR;
|
||||
if (hidden > 0) text.append(INDENT).append("and ").append(hidden).append(" more\n");
|
||||
}
|
||||
|
||||
private List<Reference> with(Resolution resolution) {
|
||||
return unresolved.stream().filter(reference -> reference.resolution() == resolution).toList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user