Add support for illegal native access modes
This commit is contained in:
parent
779e6d4b79
commit
5ca06f4d81
2 changed files with 56 additions and 4 deletions
|
@ -31,18 +31,19 @@ import java.util.List;
|
|||
*/
|
||||
@SuppressWarnings("PMD.LooseCoupling")
|
||||
public class JvmOptions extends ArrayList<String> {
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Keyword to enable native access for all code on the class path.
|
||||
*/
|
||||
public final static String ALL_UNNAMED = "ALL-UNNAMED";
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Modules that are permitted to perform restricted native operations.
|
||||
* The module name can also be {@link #ALL_UNNAMED}.
|
||||
*
|
||||
* @param modules the module names
|
||||
* @return this list of options
|
||||
*/
|
||||
public JvmOptions enableNativeAccess(String... modules) {
|
||||
|
@ -53,10 +54,37 @@ public class JvmOptions extends ArrayList<String> {
|
|||
* Modules that are permitted to perform restricted native operations.
|
||||
* The module name can also be {@link #ALL_UNNAMED}.
|
||||
*
|
||||
* @param modules the module names
|
||||
* @return this list of options
|
||||
*/
|
||||
public JvmOptions enableNativeAccess(Collection<String> modules) {
|
||||
add("--enable-native-access=" + StringUtils.join(modules, ","));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls what action the Java runtime takes when native access is not enabled for a module.
|
||||
*
|
||||
* @param access the access mode
|
||||
* @return this list of options
|
||||
*/
|
||||
public JvmOptions illegalNativeAccess(NativeAccess access) {
|
||||
add("--illegal-native-access=" + access.mode);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Illegal native access modes.
|
||||
*/
|
||||
public enum NativeAccess {
|
||||
ALLOW("allow"),
|
||||
DENY("deny"),
|
||||
WARN("warn");
|
||||
|
||||
public final String mode;
|
||||
|
||||
NativeAccess(String mode) {
|
||||
this.mode = mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,8 @@ class JvmOptionsTest {
|
|||
@Test
|
||||
void testCompileOptions() {
|
||||
var compileOptions = new CompileOptions().jvmOptions(new JvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED));
|
||||
assertThat(compileOptions.jvmOptions()).as(JvmOptions.ALL_UNNAMED).containsExactly("--enable-native-access=ALL-UNNAMED");
|
||||
assertThat(compileOptions.jvmOptions()).as(JvmOptions.ALL_UNNAMED)
|
||||
.containsExactly("--enable-native-access=ALL-UNNAMED");
|
||||
assertThat(compileOptions.args()).as("args()").containsExactly("-J--enable-native-access=ALL-UNNAMED");
|
||||
|
||||
compileOptions = new CompileOptions().jvmOptions(new JvmOptions().enableNativeAccess("m1", "m2"));
|
||||
|
@ -40,10 +41,25 @@ class JvmOptionsTest {
|
|||
var options = new JvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED);
|
||||
assertThat(options).as(JvmOptions.ALL_UNNAMED).containsExactly("--enable-native-access=ALL-UNNAMED");
|
||||
|
||||
options = new JvmOptions().enableNativeAccess("m1");
|
||||
assertThat(options).as("m1").containsExactly("--enable-native-access=m1");
|
||||
|
||||
options = new JvmOptions().enableNativeAccess("m1", "m2");
|
||||
assertThat(options).as("m1,m2").containsExactly("--enable-native-access=m1,m2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIllegalNativeAccess() {
|
||||
var options = new JvmOptions().illegalNativeAccess(JvmOptions.NativeAccess.ALLOW);
|
||||
assertThat(options).as("ALLOW").containsExactly("--illegal-native-access=allow");
|
||||
|
||||
options = new JvmOptions().illegalNativeAccess(JvmOptions.NativeAccess.DENY);
|
||||
assertThat(options).as("DENY").containsExactly("--illegal-native-access=deny");
|
||||
|
||||
options = new JvmOptions().illegalNativeAccess(JvmOptions.NativeAccess.WARN);
|
||||
assertThat(options).as("WARN").containsExactly("--illegal-native-access=warn");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testJvmOptions() {
|
||||
var compileOptions = new CompileOptions().jvmOptions("option1", "option2");
|
||||
|
@ -59,5 +75,13 @@ class JvmOptionsTest {
|
|||
.containsExactly("option1", "option2", "--enable-native-access=ALL-UNNAMED");
|
||||
assertThat(compileOptions.args()).as("args(option1,option2,ALL_UNNAMED)")
|
||||
.containsExactly("-Joption1", "-Joption2", "-J--enable-native-access=ALL-UNNAMED");
|
||||
|
||||
compileOptions = compileOptions.jvmOptions(new JvmOptions().illegalNativeAccess(JvmOptions.NativeAccess.ALLOW));
|
||||
assertThat(compileOptions.jvmOptions()).as("allow")
|
||||
.containsExactly("option1", "option2", "--enable-native-access=ALL-UNNAMED",
|
||||
"--illegal-native-access=allow");
|
||||
assertThat(compileOptions.args()).as("args(option1,option2,ALL_UNNAMED,allow)")
|
||||
.containsExactly("-Joption1", "-Joption2", "-J--enable-native-access=ALL-UNNAMED",
|
||||
"-J--illegal-native-access=allow");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue