8327218: Add an ability to specify modules which should have native access enabled

Co-authored-by: Maurizio Cimadamore <mcimadamore@openjdk.org>
Reviewed-by: mcimadamore, erikj, alanb, ihse
This commit is contained in:
Jan Lahoda 2024-03-08 11:21:24 +00:00
parent d0d4912c3b
commit 27a03e0dc3
9 changed files with 123 additions and 26 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -143,10 +143,6 @@ public final class Module implements AnnotatedElement {
String loc = Objects.toString(uri, null);
Object[] packages = descriptor.packages().toArray();
defineModule0(this, isOpen, vs, loc, packages);
if (loader == null || loader == ClassLoaders.platformClassLoader()) {
// boot/builtin modules are always native
implAddEnableNativeAccess();
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -881,6 +881,24 @@ public final class ModuleLayer {
.findAny();
}
/**
* Updates the module with the given {@code name} in this layer
* to allow access to restricted methods.
*
* @param name the name of the module for which the native access
* should be enabled
* @return {@code true} iff the module is present in this layer,
* {@code false} otherwise
*/
boolean addEnableNativeAccess(String name) {
Module m = nameToModule.get(name);
if (m != null) {
m.implAddEnableNativeAccess();
return true;
} else {
return false;
}
}
/**
* Returns the {@code ClassLoader} for the module with the given name. If

View file

@ -2459,6 +2459,9 @@ public final class System {
public Module addEnableNativeAccess(Module m) {
return m.implAddEnableNativeAccess();
}
public boolean addEnableNativeAccess(ModuleLayer layer, String name) {
return layer.addEnableNativeAccess(name);
}
public void addEnableNativeAccessToAllUnnamed() {
Module.implAddEnableNativeAccessToAllUnnamed();
}