diff --git a/src/java.base/share/classes/java/lang/Module.java b/src/java.base/share/classes/java/lang/Module.java index 5b1111a6d72a6..933c3c39b5e47 100644 --- a/src/java.base/share/classes/java/lang/Module.java +++ b/src/java.base/share/classes/java/lang/Module.java @@ -66,7 +66,9 @@ import jdk.internal.loader.ClassLoaders; import jdk.internal.misc.CDS; import jdk.internal.misc.Unsafe; +import jdk.internal.misc.VM; import jdk.internal.module.ModuleBootstrap; +import jdk.internal.module.ModuleBootstrap.IllegalNativeAccess; import jdk.internal.module.ModuleLoaderMap; import jdk.internal.module.ServicesCatalog; import jdk.internal.module.Resources; diff --git a/src/java.base/share/classes/jdk/internal/reflect/Reflection.java b/src/java.base/share/classes/jdk/internal/reflect/Reflection.java index 349ff1af9b5ce..38308ec6f853a 100644 --- a/src/java.base/share/classes/jdk/internal/reflect/Reflection.java +++ b/src/java.base/share/classes/jdk/internal/reflect/Reflection.java @@ -112,14 +112,15 @@ public static void ensureMemberAccess(Class currentClass, @ForceInline public static void ensureNativeAccess(Class currentClass, Class owner, String methodName) { - if (VM.isModuleSystemInited()) { - // if there is no caller class, act as if the call came from unnamed module of system class loader - Module module = currentClass != null ? - currentClass.getModule() : - ClassLoader.getSystemClassLoader().getUnnamedModule(); - class Holder { - static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); - } + // if there is no caller class, act as if the call came from unnamed module of system class loader + Module module = currentClass != null ? + currentClass.getModule() : + ClassLoader.getSystemClassLoader().getUnnamedModule(); + class Holder { + static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); + } + if (module != null) { + // not in init phase Holder.JLA.ensureNativeAccess(module, owner, methodName, currentClass); } }