diff --git a/examples/lib/bld/bld-wrapper.properties b/examples/lib/bld/bld-wrapper.properties index 1ffe3ff..29786b7 100644 --- a/examples/lib/bld/bld-wrapper.properties +++ b/examples/lib/bld/bld-wrapper.properties @@ -1,7 +1,7 @@ bld.downloadExtensionJavadoc=false bld.downloadExtensionSources=true bld.downloadLocation= -bld.extension-kotlin=com.uwyn.rife2:bld-kotlin:1.0.4 +bld.extension-kotlin=com.uwyn.rife2:bld-kotlin:1.0.5-SNAPSHOT bld.repositories=MAVEN_LOCAL,MAVEN_CENTRAL,RIFE2_SNAPSHOTS,RIFE2_RELEASES bld.sourceDirectories= bld.version=2.2.1 diff --git a/examples/src/bld/java/com/example/ExampleBuild.java b/examples/src/bld/java/com/example/ExampleBuild.java index 556678a..cd44399 100644 --- a/examples/src/bld/java/com/example/ExampleBuild.java +++ b/examples/src/bld/java/com/example/ExampleBuild.java @@ -3,6 +3,8 @@ package com.example; import rife.bld.BuildCommand; import rife.bld.Project; import rife.bld.extension.CompileKotlinOperation; +import rife.bld.extension.kotlin.CompileOptions; +import rife.bld.extension.kotlin.JvmOptions; import java.io.File; import java.util.List; @@ -58,12 +60,14 @@ public class ExampleBuild extends Project { @BuildCommand(summary = "Compiles the Kotlin project") @Override public void compile() throws Exception { + var options = new CompileOptions().verbose(true); + options.jvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED); // The source code located in src/main/kotlin and src/test/kotlin will be compiled - var op = new CompileKotlinOperation() + new CompileKotlinOperation() // .kotlinHome("path/to/kotlin") // .kotlinc("path/to/kotlinc") - .fromProject(this); - op.compileOptions().verbose(true); - op.execute(); + .compileOptions(options) + .fromProject(this) + .execute(); } } diff --git a/src/main/java/rife/bld/extension/kotlin/CompileOptions.java b/src/main/java/rife/bld/extension/kotlin/CompileOptions.java index 19ecbf6..30f0a4e 100644 --- a/src/main/java/rife/bld/extension/kotlin/CompileOptions.java +++ b/src/main/java/rife/bld/extension/kotlin/CompileOptions.java @@ -37,7 +37,7 @@ public class CompileOptions { private final Collection advancedOptions_ = new ArrayList<>(); private final Collection argFile_ = new ArrayList<>(); private final Collection classpath_ = new ArrayList<>(); - private final Collection jvmOptions_ = new ArrayList<>(); + private final JvmOptions jvmOptions_ = new JvmOptions(); private final Collection optIn_ = new ArrayList<>(); private final Collection options_ = new ArrayList<>(); private final Collection plugin_ = new ArrayList<>(); @@ -709,12 +709,12 @@ public class CompileOptions { * * @return the JVM options */ - public Collection jvmOptions() { + public JvmOptions jvmOptions() { return jvmOptions_; } /** - * Pass an option directly to Java Virtual Machine + * Pass an option directly to the Java Virtual Machine * * @param jvmOptions the JVM options * @return this operation instance @@ -725,7 +725,7 @@ public class CompileOptions { } /** - * Pass an option directly to JVM + * Pass an option directly to the Java Virtual Machine * * @param jvmOptions one or more JVM option * @return this operation instance diff --git a/src/main/java/rife/bld/extension/kotlin/JvmOptions.java b/src/main/java/rife/bld/extension/kotlin/JvmOptions.java new file mode 100644 index 0000000..b050b83 --- /dev/null +++ b/src/main/java/rife/bld/extension/kotlin/JvmOptions.java @@ -0,0 +1,61 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package rife.bld.extension.kotlin; + +import rife.tools.StringUtils; + +import java.io.Serial; +import java.util.ArrayList; +import java.util.List; + +/** + * Java Virtual Machine options. + * + * @author Erik C. Thauvin + * @since 1.0.5 + */ +@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"; + + /** + * Modules that are permitted to perform restricted native operations. + * The module name can also be {@link #ALL_UNNAMED}. + * + * @return this list of options + */ + public JvmOptions enableNativeAccess(String... modules) { + return enableNativeAccess(List.of(modules)); + } + + /** + * Modules that are permitted to perform restricted native operations. + * The module name can also be {@link #ALL_UNNAMED}. + * + * @return this list of options + */ + public JvmOptions enableNativeAccess(List modules) { + add("--enable-native-access=" + StringUtils.join(modules, ",")); + return this; + } +} diff --git a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java index 9fffc25..1689525 100644 --- a/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java +++ b/src/test/java/rife/bld/extension/CompileKotlinOperationTest.java @@ -23,6 +23,7 @@ import rife.bld.BaseProject; import rife.bld.blueprints.BaseProjectBlueprint; import rife.bld.extension.kotlin.CompileOptions; import rife.bld.extension.kotlin.CompilerPlugin; +import rife.bld.extension.kotlin.JvmOptions; import rife.tools.FileUtils; import java.io.File; @@ -177,9 +178,10 @@ class CompileKotlinOperationTest { op.compileOptions().verbose(true); op.compileOptions().jdkRelease("17"); + op.compileOptions().jvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED); var args = op.compileOptions().args(); - var matches = List.of("-Xjdk-release=17", "-no-stdlib", "-verbose"); + var matches = List.of("-Xjdk-release=17", "-J--enable-native-access=ALL-UNNAMED", "-no-stdlib", "-verbose"); assertThat(args).as(args + " == " + matches).isEqualTo(matches); op.execute(); diff --git a/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java b/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java new file mode 100644 index 0000000..eb2d59a --- /dev/null +++ b/src/test/java/rife/bld/extension/kotlin/JvmOptionsTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package rife.bld.extension.kotlin; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +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.args()).as("args()").containsExactly("-J--enable-native-access=ALL-UNNAMED"); + + compileOptions = new CompileOptions().jvmOptions(new JvmOptions().enableNativeAccess("m1", "m2")); + assertThat(compileOptions.jvmOptions()).as("m1,m2").containsExactly("--enable-native-access=m1,m2"); + assertThat(compileOptions.args()).as("args(m1,m2)").containsExactly("-J--enable-native-access=m1,m2"); + } + + @Test + void testEnableNativeAccess() { + 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", "m2"); + assertThat(options).as("m1,m2").containsExactly("--enable-native-access=m1,m2"); + } + + @Test + void testJvmOptions() { + var compileOptions = new CompileOptions().jvmOptions("option1", "option2"); + assertThat(compileOptions.jvmOptions()).as("option1,option2").containsExactly("option1", "option2"); + assertThat(compileOptions.args()).as("args()").containsExactly("-Joption1", "-Joption2"); + + compileOptions = new CompileOptions().jvmOptions(List.of("option1", "option2")); + assertThat(compileOptions.jvmOptions()).as("List.of(option1,option2)").containsExactly("option1", "option2"); + assertThat(compileOptions.args()).as("args(list)").containsExactly("-Joption1", "-Joption2"); + + compileOptions = compileOptions.jvmOptions(new JvmOptions().enableNativeAccess(JvmOptions.ALL_UNNAMED)); + assertThat(compileOptions.jvmOptions()).as("List.of(option1,option2,ALL_UNNAMED)") + .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"); + } +}