From 5ca06f4d819b69a5b15e97b19153b2fcbe6e9103 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 21 Mar 2025 11:29:44 -0700 Subject: [PATCH 1/2] Add support for illegal native access modes --- .../rife/bld/extension/kotlin/JvmOptions.java | 34 +++++++++++++++++-- .../bld/extension/kotlin/JvmOptionsTest.java | 26 +++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/main/java/rife/bld/extension/kotlin/JvmOptions.java b/src/main/java/rife/bld/extension/kotlin/JvmOptions.java index 4c46989..926c6e7 100644 --- a/src/main/java/rife/bld/extension/kotlin/JvmOptions.java +++ b/src/main/java/rife/bld/extension/kotlin/JvmOptions.java @@ -31,18 +31,19 @@ import java.util.List; */ @SuppressWarnings("PMD.LooseCoupling") public class JvmOptions extends ArrayList { - @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 { * 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 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; + } + } } diff --git a/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java b/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java index eb2d59a..0dcfbef 100644 --- a/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java +++ b/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java @@ -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"); } } From 54228e314920426fbc8e800e4f6d723a557c5b9d Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 21 Mar 2025 11:35:12 -0700 Subject: [PATCH 2/2] Add tests for new compiler plugins --- .idea/misc.xml | 2 ++ .../rife/bld/extension/CompileKotlinOperationTest.java | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index ea8d1e3..593e427 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -9,6 +9,8 @@ + + diff --git a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java index 1689525..2c9032d 100644 --- a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java +++ b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java @@ -104,9 +104,10 @@ class CompileKotlinOperationTest { .testSourceFiles(List.of(new File("tfile3"), new File("tfile4"))) .testSourceFiles(new File("tfile5"), new File("tfile6")) .plugins("plugin1", "plugin2") - .plugins(CompilerPlugin.KOTLIN_SERIALIZATION, CompilerPlugin.ASSIGNMENT) + .plugins(CompilerPlugin.KOTLIN_SERIALIZATION, CompilerPlugin.ASSIGNMENT, CompilerPlugin.COMPOSE) .plugins(new File("lib/compile"), CompilerPlugin.LOMBOK, CompilerPlugin.POWER_ASSERT) - .plugins(Path.of("lib/compile"), CompilerPlugin.NOARG, CompilerPlugin.ALL_OPEN) + .plugins(Path.of("lib/compile"), CompilerPlugin.NOARG, CompilerPlugin.ALL_OPEN, + CompilerPlugin.KOTLIN_IMPORTS_DUMPER) .plugins("lib/compile", CompilerPlugin.KOTLINX_SERIALIZATION, CompilerPlugin.SAM_WITH_RECEIVER) .plugins(List.of("plugin3", "plugin4")); @@ -134,10 +135,12 @@ class CompileKotlinOperationTest { softly.assertThat(op.plugins()).as("plugins").contains("plugin1", "plugin2", "plugin3", "plugin4", "/kotlin_home/lib/kotlin-serialization-compiler-plugin.jar", "/kotlin_home/lib/assignment-compiler-plugin.jar", + "/kotlin_home/lib/compose-compiler-plugin.jar", new File("lib/compile", "lombok-compiler-plugin.jar").getAbsolutePath(), new File("lib/compile", "power-assert-compiler-plugin.jar").getAbsolutePath(), new File("lib/compile", "noarg-compiler-plugin.jar").getAbsolutePath(), new File("lib/compile", "allopen-compiler-plugin.jar").getAbsolutePath(), + new File("lib/compile", "kotlin-imports-dumper-compiler-plugin.jar").getAbsolutePath(), new File("lib/compile", "kotlinx-serialization-compiler-plugin.jar").getAbsolutePath(), new File("lib/compile", "sam-with-receiver-compiler-plugin.jar").getAbsolutePath()); } @@ -315,6 +318,8 @@ class CompileKotlinOperationTest { .fromProject(new BaseProject()) .plugins(CompilerPlugin.ALL_OPEN, CompilerPlugin.ASSIGNMENT, + CompilerPlugin.COMPOSE, + CompilerPlugin.KOTLIN_IMPORTS_DUMPER, CompilerPlugin.KOTLINX_SERIALIZATION, CompilerPlugin.KOTLIN_SERIALIZATION, CompilerPlugin.LOMBOK,