From ec80683aa12883804979fdda62db5823979d2fa8 Mon Sep 17 00:00:00 2001 From: piexel Date: Mon, 5 Oct 2026 20:59:45 +0200 Subject: [PATCH] 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. --- .../com/volmit/host/PackReferenceCheck.java | 24 ++++++++-- .../volmit/host/UnavailableJdkModules.java | 48 +++++++++++++++++++ .../java/com/volmit/host/PackModulesTest.java | 29 +++++++++++ .../host/UnavailableJdkModulesTest.java | 26 ++++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/volmit/host/UnavailableJdkModules.java create mode 100644 src/test/java/com/volmit/host/UnavailableJdkModulesTest.java diff --git a/src/main/java/com/volmit/host/PackReferenceCheck.java b/src/main/java/com/volmit/host/PackReferenceCheck.java index 0d97b2fc9..5cde8d5eb 100644 --- a/src/main/java/com/volmit/host/PackReferenceCheck.java +++ b/src/main/java/com/volmit/host/PackReferenceCheck.java @@ -14,7 +14,8 @@ import java.util.function.Predicate; * *

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 requireResolvable(List 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 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"); } diff --git a/src/main/java/com/volmit/host/UnavailableJdkModules.java b/src/main/java/com/volmit/host/UnavailableJdkModules.java new file mode 100644 index 000000000..463fb2786 --- /dev/null +++ b/src/main/java/com/volmit/host/UnavailableJdkModules.java @@ -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 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 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() {} + } +} diff --git a/src/test/java/com/volmit/host/PackModulesTest.java b/src/test/java/com/volmit/host/PackModulesTest.java index 85f77355d..7da4f5f8c 100644 --- a/src/test/java/com/volmit/host/PackModulesTest.java +++ b/src/test/java/com/volmit/host/PackModulesTest.java @@ -275,6 +275,35 @@ class PackModulesTest { failure(() -> PackModules.load(pack))); } + @Test + void referenceToAJdkModuleOutsideThePlatformLoaderFailsNamingTheModule() { + module("pack.user.User", "user:core", using("com.sun.source.tree.Tree", "java.sql.Connection"), NO_STAGES) + .writeInto(pack, "user.jar"); + + assertEquals("Module jar " + origin("user.jar") + " uses classes of JDK modules that are not available to modules:\n" + + " pack.user.User -> com.sun.source.tree.Tree (JDK module jdk.compiler)\n" + + "Modules can use only the JDK modules of the platform class loader, such as java.base, java.sql and" + + " java.net.http.", + failure(() -> PackModules.load(pack))); + } + + @Test + void oneFailureReportsEveryKindOfUnusableClass() { + module("pack.user.User", "user:core", using("org.junit.jupiter.api.Test", "com.sun.source.tree.Tree"), NO_STAGES) + .compileAgainstHostOf(Test.class) + .writeInto(pack, "user.jar"); + + assertEquals("Module jar " + origin("user.jar") + " uses classes that exist in Iris but are hidden from modules:\n" + + " pack.user.User -> org.junit.jupiter.api.Test\n" + + "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.\n" + + "Module jar " + origin("user.jar") + " uses classes of JDK modules that are not available to modules:\n" + + " pack.user.User -> com.sun.source.tree.Tree (JDK module jdk.compiler)\n" + + "Modules can use only the JDK modules of the platform class loader, such as java.base, java.sql and" + + " java.net.http.", + failure(() -> PackModules.load(pack))); + } + @Test void classesNobodyHasAreReportedWithoutFailingTheLoad(@TempDir Path build) { var optionalLibrary = new TestModuleJar() diff --git a/src/test/java/com/volmit/host/UnavailableJdkModulesTest.java b/src/test/java/com/volmit/host/UnavailableJdkModulesTest.java new file mode 100644 index 000000000..64fd630b7 --- /dev/null +++ b/src/test/java/com/volmit/host/UnavailableJdkModulesTest.java @@ -0,0 +1,26 @@ +package com.volmit.host; + +import org.junit.jupiter.api.Test; +import java.util.Optional; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class UnavailableJdkModulesTest { + @Test + void namesJdkModulesOutsideThePlatformLoader() { + assertEquals(Optional.of("jdk.compiler"), UnavailableJdkModules.moduleOf("com.sun.source.tree.Tree")); + assertEquals(Optional.of("jdk.jshell"), UnavailableJdkModules.moduleOf("jdk.jshell.JShell")); + } + + @Test + void modulesOfTheBootAndPlatformLoadersAreAvailable() { + assertEquals(Optional.empty(), UnavailableJdkModules.moduleOf("java.lang.String")); + assertEquals(Optional.empty(), UnavailableJdkModules.moduleOf("java.sql.Connection")); + assertEquals(Optional.empty(), UnavailableJdkModules.moduleOf("javax.tools.ToolProvider")); + } + + @Test + void classesOutsideTheJdkAreNotJdkModules() { + assertEquals(Optional.empty(), UnavailableJdkModules.moduleOf("org.slf4j.Logger")); + assertEquals(Optional.empty(), UnavailableJdkModules.moduleOf("TopLevel")); + } +}