8167614: Avoid module dependency from jdk.dynalink to jdk.internal.module of java.base module
Reviewed-by: jlaskey, alanb
This commit is contained in:
parent
7975bee5dc
commit
c299fc425d
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -389,6 +389,15 @@ public final class Context {
|
||||
// is created, and invalidated forever once the second global is created.
|
||||
private final AtomicReference<GlobalConstants> 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
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user