diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java index c0d4a2debf33..53cb2f65fb6b 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java @@ -24,7 +24,6 @@ */ package com.oracle.svm.util; -import java.util.NoSuchElementException; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -90,6 +89,13 @@ public static void accessModuleByClass(Access access, Class accessingClass, C accessModuleByClass(access, accessingClass, declaringClass.getModule(), declaringClass.getPackageName()); } + @SuppressWarnings("serial") + public static final class ModuleSupportError extends Error { + private ModuleSupportError(String message) { + super(message); + } + } + /** * Open or export packages {@code packageNames} in the module named {@code moduleName} to module * of given {@code accessingClass}. If {@code accessingClass} is null packages are opened or @@ -98,10 +104,18 @@ public static void accessModuleByClass(Access access, Class accessingClass, C */ @Platforms(Platform.HOSTED_ONLY.class) public static void accessPackagesToClass(Access access, Class accessingClass, boolean optional, String moduleName, String... packageNames) { - Module declaringModule = getModule(moduleName, optional); - if (declaringModule == null) { - return; + Objects.requireNonNull(moduleName); + Optional module = ModuleLayer.boot().findModule(moduleName); + if (module.isEmpty()) { + if (optional) { + return; + } + String accessor = accessingClass != null ? "class " + accessingClass.getTypeName() : "ALL-UNNAMED"; + String message = access.name().toLowerCase() + " of packages from module " + moduleName + " to " + + accessor + " failed. No module named " + moduleName + " in boot layer."; + throw new ModuleSupportError(message); } + Module declaringModule = module.get(); Objects.requireNonNull(packageNames); Set packages = packageNames.length > 0 ? Set.of(packageNames) : declaringModule.getPackages(); for (String packageName : packages) { @@ -109,19 +123,6 @@ public static void accessPackagesToClass(Access access, Class accessingClass, } } - @Platforms(Platform.HOSTED_ONLY.class) - private static Module getModule(String moduleName, boolean optional) { - Objects.requireNonNull(moduleName); - Optional declaringModuleOpt = ModuleLayer.boot().findModule(moduleName); - if (declaringModuleOpt.isEmpty()) { - if (optional) { - return null; - } - throw new NoSuchElementException(moduleName); - } - return declaringModuleOpt.get(); - } - @Platforms(Platform.HOSTED_ONLY.class) private static void accessModuleByClass(Access access, Class accessingClass, Module declaringModule, String packageName) { Module namedAccessingModule = null;