mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Report JDK classes that modules cannot see as their own load error
A reference to a class of a JDK module outside the platform class
loader, such as jdk.compiler's com.sun.source.tree.Tree, was reported as
"exists in Iris but is hidden", because the host loader (the application
loader in tests) has it. It now has its own category, listed with the
module:
Module jar modules/user.jar uses classes of JDK modules that are not available to modules:
pack.user.User -> com.sun.source.tree.Tree (JDK module jdk.compiler)
Modules can use only the JDK modules of the platform class loader, ...
UnavailableJdkModules maps the packages of every system module that the
boot layer does not define to the boot or platform loader, from module
descriptors only, so nothing is loaded. Classes the platform loader
provides (java.sql, java.compiler) still resolve.
This commit is contained in:
1 parent
57774fade0
commit
ec80683aa1
4 files changed
+122
-5
No files matched your search
@@ -14,7 +14,8 @@ import java.util.function.Predicate;
|
||||
*
|
||||
* <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.
|
||||
* that Iris has but hides, a class of a JDK module packs cannot see, or a class missing from a
|
||||
* package Iris exposes always fails the load.
|
||||
*/
|
||||
final class PackReferenceCheck {
|
||||
private static final int MAX_REPORTED_REFERENCES_PER_JAR = 10;
|
||||
@@ -30,6 +31,11 @@ final class PackReferenceCheck {
|
||||
"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."),
|
||||
/** A JDK module outside the platform class loader, such as jdk.compiler. */
|
||||
UNAVAILABLE_JDK_CLASS(
|
||||
"uses classes of JDK modules that are not available to modules:",
|
||||
"Modules can use only the JDK modules of the platform class loader, such as java.base, java.sql and"
|
||||
+ " java.net.http."),
|
||||
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.");
|
||||
@@ -60,7 +66,8 @@ final class PackReferenceCheck {
|
||||
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.
|
||||
/** Fails when a jar uses a class Iris hides, a class of a JDK module packs cannot see, 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, ExposedHostClassLoader host) {
|
||||
@@ -89,6 +96,7 @@ final class PackReferenceCheck {
|
||||
private Resolution resolveOutsidePack(String className) {
|
||||
if (host.hasClass(className)) return Resolution.PROVIDED;
|
||||
if (host.exposed().exposes(className)) return Resolution.MISSING_HOST_CLASS;
|
||||
if (UnavailableJdkModules.moduleOf(className).isPresent()) return Resolution.UNAVAILABLE_JDK_CLASS;
|
||||
if (host.hostHasClass(className)) return Resolution.HIDDEN_HOST_CLASS;
|
||||
return Resolution.ABSENT;
|
||||
}
|
||||
@@ -110,7 +118,13 @@ final class PackReferenceCheck {
|
||||
return text.append(failure.advice).toString();
|
||||
}
|
||||
|
||||
private record Reference(String source, String target, Resolution resolution) {}
|
||||
private record Reference(String source, String target, Resolution resolution) {
|
||||
String describe() {
|
||||
var text = source + " -> " + target;
|
||||
if (resolution != Resolution.UNAVAILABLE_JDK_CLASS) return text;
|
||||
return text + " (JDK module " + UnavailableJdkModules.moduleOf(target).orElseThrow() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
/** The references of one jar the pack loader would not resolve. */
|
||||
private record JarReferences(ModuleJar jar, List<Reference> unresolved) {
|
||||
@@ -128,8 +142,8 @@ final class PackReferenceCheck {
|
||||
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'));
|
||||
references.stream().limit(MAX_REPORTED_REFERENCES_PER_JAR)
|
||||
.forEach(reference -> text.append(INDENT).append(reference.describe()).append('\n'));
|
||||
int hidden = references.size() - MAX_REPORTED_REFERENCES_PER_JAR;
|
||||
if (hidden > 0) text.append(INDENT).append("and ").append(hidden).append(" more\n");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.lang.module.ModuleFinder;
|
||||
import java.lang.module.ModuleReference;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** The JDK modules whose classes pack modules cannot use, by package. Packs see the JDK through the
|
||||
* platform class loader, which provides the modules defined to it and to the boot loader. Other JDK
|
||||
* modules, such as {@code jdk.compiler} or {@code jdk.jshell}, are defined to the application class
|
||||
* loader, or not resolved at all, so a pack never sees them. Judged from module descriptors, so
|
||||
* nothing is loaded.
|
||||
*/
|
||||
final class UnavailableJdkModules {
|
||||
private static final char PACKAGE_SEPARATOR = '.';
|
||||
|
||||
private UnavailableJdkModules() {}
|
||||
|
||||
/** The JDK module that holds the class's package, if packs cannot use that module. */
|
||||
static Optional<String> moduleOf(String binaryName) {
|
||||
int separator = binaryName.lastIndexOf(PACKAGE_SEPARATOR);
|
||||
if (separator < 0) return Optional.empty();
|
||||
return Optional.ofNullable(ModulesByPackage.UNAVAILABLE.get(binaryName.substring(0, separator)));
|
||||
}
|
||||
|
||||
private static boolean platformLoaderProvides(String moduleName) {
|
||||
return ModuleLayer.boot().findModule(moduleName)
|
||||
.filter(module -> isPlatformOrBootLoader(module.getClassLoader()))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
/** The boot loader is {@code null}; the platform loader delegates to it. */
|
||||
private static boolean isPlatformOrBootLoader(ClassLoader loader) {
|
||||
return loader == null || loader == ClassLoader.getPlatformClassLoader();
|
||||
}
|
||||
|
||||
/** Built on first use, once per runtime: the JDK's modules do not change while it runs. */
|
||||
private static final class ModulesByPackage {
|
||||
static final Map<String, String> UNAVAILABLE = ModuleFinder.ofSystem().findAll().stream()
|
||||
.map(ModuleReference::descriptor)
|
||||
.filter(descriptor -> !platformLoaderProvides(descriptor.name()))
|
||||
.flatMap(descriptor -> descriptor.packages().stream().map(name -> Map.entry(name, descriptor.name())))
|
||||
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
private ModulesByPackage() {}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user