From c299fc425d76ec16716aad94eb2b34002ff94c86 Mon Sep 17 00:00:00 2001 From: Athijegannathan Sundararajan Date: Wed, 12 Oct 2016 22:42:23 +0530 Subject: [PATCH] 8167614: Avoid module dependency from jdk.dynalink to jdk.internal.module of java.base module Reviewed-by: jlaskey, alanb --- .../beans/CallerSensitiveDynamicMethod.java | 52 +------------------ .../jdk/nashorn/internal/runtime/Context.java | 9 ++++ .../internal/runtime/ScriptLoader.java | 19 +++++-- .../internal/runtime/StructureLoader.java | 1 + 4 files changed, 27 insertions(+), 54 deletions(-) diff --git a/nashorn/src/jdk.dynalink/share/classes/jdk/dynalink/beans/CallerSensitiveDynamicMethod.java b/nashorn/src/jdk.dynalink/share/classes/jdk/dynalink/beans/CallerSensitiveDynamicMethod.java index 467da4b9c75..016873489d5 100644 --- a/nashorn/src/jdk.dynalink/share/classes/jdk/dynalink/beans/CallerSensitiveDynamicMethod.java +++ b/nashorn/src/jdk.dynalink/share/classes/jdk/dynalink/beans/CallerSensitiveDynamicMethod.java @@ -100,7 +100,6 @@ import jdk.dynalink.CallSiteDescriptor; import jdk.dynalink.SecureLookupSupplier; import jdk.dynalink.internal.AccessControlContextFactory; import jdk.dynalink.linker.support.Lookup; -import jdk.internal.module.Modules; import jdk.internal.reflect.CallerSensitive; @@ -180,57 +179,10 @@ class CallerSensitiveDynamicMethod extends SingleDynamicMethod { } private static MethodHandle unreflect(final MethodHandles.Lookup lookup, final Method m) { - try { - return Lookup.unreflect(lookup, m); - } catch (final IllegalAccessError iae) { - if (addModuleRead(lookup, m)) { - try { - return Lookup.unreflect(lookup, m); - } catch (final IllegalAccessError e2) { - // fall through and throw original error as cause - } - } - throw iae; - } + return Lookup.unreflect(lookup, m); } private static MethodHandle unreflectConstructor(final MethodHandles.Lookup lookup, final Constructor c) { - try { - return Lookup.unreflectConstructor(lookup, c); - } catch (final IllegalAccessError iae) { - if (addModuleRead(lookup, c)) { - try { - return Lookup.unreflectConstructor(lookup, c); - } catch (final IllegalAccessError e2) { - // fall through and throw original error as cause - } - } - throw iae; - } - } - - - private static boolean addModuleRead(final MethodHandles.Lookup lookup, final Executable e) { - // Don't add module read link if this is not a CallerSensitive member - if (!e.isAnnotationPresent(CallerSensitive.class)) { - return false; - } - - // If the lookup is public lookup, don't bother adding module read link! - // public lookup cannot unreflect caller sensitives anyway! - if (lookup == MethodHandles.publicLookup()) { - return false; - } - - // try to add missing module read from using module to declararing module! - final Class declClass = e.getDeclaringClass(); - final Module useModule = lookup.lookupClass().getModule(); - final Module declModule = declClass.getModule(); - if (useModule != null && declModule != null && declModule.isExported(declClass.getPackageName())) { - Modules.addReads(useModule, declModule); - return true; - } - - return false; + return Lookup.unreflectConstructor(lookup, c); } } diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java index 1c8c0219ee0..96bfda4ebef 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java @@ -389,6 +389,15 @@ public final class Context { // is created, and invalidated forever once the second global is created. private final AtomicReference globalConstantsRef = new AtomicReference<>(); + // Are java.sql, java.sql.rowset modules found in the system? + static final boolean javaSqlFound, javaSqlRowsetFound; + + static { + final Layer boot = Layer.boot(); + javaSqlFound = boot.findModule("java.sql").isPresent(); + javaSqlRowsetFound = boot.findModule("java.sql.rowset").isPresent(); + } + /** * Get the current global scope * @return the current global scope diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java index b6310a7ba9e..c37f1994f92 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptLoader.java @@ -68,12 +68,23 @@ final class ScriptLoader extends NashornLoader { private Module createModule(final String moduleName) { final Module structMod = context.getStructLoader().getModule(); - final ModuleDescriptor descriptor - = new ModuleDescriptor.Builder(moduleName) + final ModuleDescriptor.Builder builder = + new ModuleDescriptor.Builder(moduleName) + .requires("java.base") + .requires("java.logging") .requires(NASHORN_MODULE.getName()) .requires(structMod.getName()) - .conceals(SCRIPTS_PKG) - .build(); + .conceals(SCRIPTS_PKG); + + if (Context.javaSqlFound) { + builder.requires("java.sql"); + } + + if (Context.javaSqlRowsetFound) { + builder.requires("java.sql.rowset"); + } + + final ModuleDescriptor descriptor = builder.build(); final Module mod = Context.createModuleTrusted(structMod.getLayer(), descriptor, this); loadModuleManipulator(); diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java index d9950e7be3f..be7d38be2a0 100644 --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StructureLoader.java @@ -64,6 +64,7 @@ final class StructureLoader extends NashornLoader { private Module createModule(final String moduleName) { final ModuleDescriptor descriptor = new ModuleDescriptor.Builder(moduleName) + .requires("java.base") .requires(NASHORN_MODULE.getName()) .conceals(SCRIPTS_PKG) .build();