mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Let modules declare optional packages that need no missing-class warning
A module whose bundled library references optional integrations got a
WARNING on every pack load, with no way to say the classes are expected
to be absent. ModuleInfo now carries optionalPackages, set with
withOptionalPackages("org.slf4j", ...); ModuleInfo.of(id, name, version)
still declares none. Each package covers its subpackages.
Missing classes of a jar inside its own module's optional packages are
dropped from the warning and from PackModules.missingClasses(); other
jars are unaffected. The declaration comes from info(), so it is applied
after the entry classes exist. Load errors (hidden Iris classes, classes
missing from exposed packages) are decided before that and never depend
on it.
Package-name validation and whole-package matching move from
ExposedPackages to a new API value, PackageName, used by both.
This commit is contained in:
1 parent
3873894a22
commit
57774fade0
8 files changed
+241
-41
No files matched your search
@@ -1,7 +1,7 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import java.util.Arrays;
|
||||
import com.volmit.PackageName;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -16,13 +16,13 @@ import java.util.TreeSet;
|
||||
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 PackageName API = new PackageName(API_PACKAGE);
|
||||
private static final char PACKAGE_SEPARATOR = '.';
|
||||
private static final char PATH_SEPARATOR = '/';
|
||||
private static final String PACKAGE_SEPARATOR_PATTERN = "\\.";
|
||||
|
||||
private final List<String> packages;
|
||||
private final List<PackageName> packages;
|
||||
|
||||
private ExposedPackages(List<String> packages) {
|
||||
private ExposedPackages(List<PackageName> packages) {
|
||||
this.packages = List.copyOf(packages);
|
||||
}
|
||||
|
||||
@@ -35,18 +35,18 @@ final class ExposedPackages {
|
||||
*/
|
||||
static ExposedPackages apiAnd(Collection<String> hostPackages) {
|
||||
Objects.requireNonNull(hostPackages, "hostPackages");
|
||||
var packages = new TreeSet<String>();
|
||||
packages.add(API_PACKAGE);
|
||||
var packages = new TreeSet<PackageName>();
|
||||
packages.add(API);
|
||||
hostPackages.forEach(name -> packages.add(requirePackageName(name)));
|
||||
return new ExposedPackages(List.copyOf(packages));
|
||||
}
|
||||
|
||||
static boolean isApiClass(String binaryName) {
|
||||
return isInPackage(binaryName, API_PACKAGE);
|
||||
return API.contains(binaryName);
|
||||
}
|
||||
|
||||
boolean exposes(String binaryName) {
|
||||
return packages.stream().anyMatch(name -> isInPackage(binaryName, name));
|
||||
return packages.stream().anyMatch(name -> name.contains(binaryName));
|
||||
}
|
||||
|
||||
/** Whether a resource path, such as {@code com/volmit/IrisModule.class}, lies in an exposed package. */
|
||||
@@ -57,32 +57,16 @@ final class ExposedPackages {
|
||||
/** 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))
|
||||
.filter(name -> name.contains(binaryName))
|
||||
.map(PackageName::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) {
|
||||
private static PackageName 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);
|
||||
return PackageName.tryParse(name).orElseThrow(() -> 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"));
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.PackageName;
|
||||
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.
|
||||
* dependencies of a library the jar bundles, and that the jar's module does not declare
|
||||
* {@linkplain ModuleInfo#optionalPackages() optional}. 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
|
||||
@@ -15,4 +18,16 @@ public record MissingClasses(String origin, List<String> classNames) {
|
||||
Objects.requireNonNull(origin, "origin");
|
||||
classNames = List.copyOf(classNames);
|
||||
}
|
||||
|
||||
/** These classes without those in any of {@code packages}. */
|
||||
MissingClasses outside(List<PackageName> packages) {
|
||||
var remaining = classNames.stream()
|
||||
.filter(className -> packages.stream().noneMatch(name -> name.contains(className)))
|
||||
.toList();
|
||||
return new MissingClasses(origin, remaining);
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
return classNames.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** The module jars of one pack: every {@code <pack>/modules/*.jar}, one module per jar, ordered
|
||||
* by file name so the result never depends on directory listing order.
|
||||
@@ -24,7 +26,8 @@ import java.util.Objects;
|
||||
* 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()}.
|
||||
* them runs; they are reported as {@link #missingClasses()}, except those in the optional packages
|
||||
* the jar's module declares in its {@link com.volmit.ModuleInfo}.
|
||||
*
|
||||
* <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, and one WARNING listing the missing classes, if any.
|
||||
@@ -78,7 +81,8 @@ public final class PackModules implements AutoCloseable {
|
||||
}
|
||||
|
||||
/** 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.
|
||||
* JDK has and that are outside the optional packages the jar's module declares. Empty when every
|
||||
* class the jars use exists or is declared optional.
|
||||
*/
|
||||
public List<MissingClasses> missingClasses() {
|
||||
return missingClasses;
|
||||
@@ -114,7 +118,8 @@ public final class PackModules implements AutoCloseable {
|
||||
Path packRoot, List<ModuleJar> jars, List<MissingClasses> missingClasses, ClassLoader host) {
|
||||
var loader = newPackClassLoader(packRoot, jars, host);
|
||||
try {
|
||||
return new PackModules(createModules(jars, loader), missingClasses, loader);
|
||||
var modules = createModules(jars, loader);
|
||||
return new PackModules(modules, withoutDeclaredOptional(missingClasses, modules), loader);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
closeAfterFailure(loader, failure);
|
||||
throw failure;
|
||||
@@ -141,6 +146,19 @@ public final class PackModules implements AutoCloseable {
|
||||
return jars.stream().map(jar -> jar.createModule(loader)).toList();
|
||||
}
|
||||
|
||||
/** A module's optional packages excuse only its own jar's missing classes. They are read from
|
||||
* {@code info()}, so this runs after the entry classes exist; errors were decided before that and
|
||||
* never depend on them.
|
||||
*/
|
||||
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()));
|
||||
return missingClasses.stream()
|
||||
.map(jar -> jar.outside(optionalPackages.get(jar.origin())))
|
||||
.filter(Predicate.not(MissingClasses::isEmpty))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static void closeAfterFailure(URLClassLoader loader, Throwable failure) {
|
||||
try {
|
||||
loader.close();
|
||||
|
||||
Reference in new issue
Block a user