Skip to content

Commit

Permalink
Configure strong encapsulation jvm args for all 9+ JDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
tbroyer committed Oct 10, 2022
1 parent 0a50bed commit 423cbfa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ so you would have to configure those JVM arguments yourself in this case.
See [the ErrorProne docs](https://errorprone.info/docs/installation#java-9-and-newer) for the full list of `--add-opens` and `--add-exports` needed
(use `-J--add-opens` if using `executable`, `--add-opens` if using `javaHome`).

Note that the plugin also configures the JVM arguments for any JDK between 9 and 15 to silence related warnings,
but they will then only be used if the task is explicitly configured for forking.

[jep396]: https://openjdk.java.net/jeps/396

## Custom Error Prone checks
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/net/ltgt/gradle/errorprone/ErrorPronePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ class ErrorPronePlugin : Plugin<Project> {
compilerVersion?.let {
if (it < JavaVersion.VERSION_1_8) throw UnsupportedOperationException(TOO_OLD_TOOLCHAIN_ERROR_MESSAGE)
if (it.needsForking) options.isFork = true
when {
it == JavaVersion.VERSION_1_8 -> configureErrorProneJavac()
it >= JavaVersion.VERSION_16 -> configureForJava16plus()
if (it == JavaVersion.VERSION_1_8) {
configureErrorProneJavac()
} else {
configureForJava9plus()
}
}
}
Expand Down Expand Up @@ -157,8 +158,8 @@ class ErrorPronePlugin : Plugin<Project> {
private val JavaVersion.needsForking get() =
this == JavaVersion.VERSION_1_8 || this >= JavaVersion.VERSION_16

private fun JavaCompile.configureForJava16plus() {
// https://github.com/google/error-prone/issues/1157#issuecomment-769289564
private fun JavaCompile.configureForJava9plus() {
// https://errorprone.info/docs/installation#java-9-and-newer
options.forkOptions.jvmArgs!!.addAll(JVM_ARGS_STRONG_ENCAPSULATION)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Java8IntegrationTest : AbstractPluginIntegrationTest() {
// then
assertThat(result.task(":compileJava")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.output).contains(FORKED)
assertThat(result.output).doesNotContain(JVM_ARG_BOOTCLASSPATH)
assertThat(result.output).contains(JVM_ARGS_STRONG_ENCAPSULATION)
// Check that the configured jvm arg is preserved
assertThat(result.output).contains(jvmArg("-XshowSettings"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ToolchainsIntegrationTest : AbstractPluginIntegrationTest() {
assertThat(result.task(":compileJava")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.output).contains(NOT_FORKED)
assertThat(result.output).doesNotContain(JVM_ARG_BOOTCLASSPATH)
assertThat(result.output).doesNotContain(JVM_ARGS_STRONG_ENCAPSULATION)
assertThat(result.output).contains(JVM_ARGS_STRONG_ENCAPSULATION)
// Check that the configured jvm arg is preserved
assertThat(result.output).contains(jvmArg("-XshowSettings"))
}
Expand All @@ -179,7 +179,7 @@ class ToolchainsIntegrationTest : AbstractPluginIntegrationTest() {
assertThat(result.task(":compileJava")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.output).contains(FORKED)
assertThat(result.output).doesNotContain(JVM_ARG_BOOTCLASSPATH)
assertThat(result.output).doesNotContain(JVM_ARGS_STRONG_ENCAPSULATION)
assertThat(result.output).contains(JVM_ARGS_STRONG_ENCAPSULATION)
// Check that the configured jvm arg is preserved
assertThat(result.output).contains(jvmArg("-XshowSettings"))
}
Expand Down

0 comments on commit 423cbfa

Please sign in to comment.