From 2627b10f314896c8a7a9a3b9b5c5da4d4fff8157 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Thu, 19 Nov 2026 12:11:46 -0800 Subject: [PATCH 1/2] Add an `errorProneFix` task that applies Error Prone's suggested fixes --- build.gradle.kts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 03d76f53..f78f0013 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -65,6 +65,14 @@ val addOpensArgs = javacExportTargets.map { "--add-opens=$it" } // Compilation +// True if the `errorProneFix` task was requested on the command line. Then each compilation +// rewrites its source files in place, applying Error Prone's suggested fixes. +// Gradle matches task names case-insensitively, so also recognize, say, "errorpronefix". +val errorProneFix = + gradle.startParameter.taskNames.any { + it.replaceFirst(Regex("^:"), "").equals("errorProneFix", ignoreCase = true) + } + java { toolchain { // Always compile using Java 21. The "test" task below overrides this for @@ -80,7 +88,10 @@ tasks.withType().configureEach { // from the JVM that runs Gradle. That worker does not inherit the // `org.gradle.jvmargs` heap setting in gradle.properties, so set it here too. options.forkOptions.jvmArgs = (options.forkOptions.jvmArgs ?: emptyList()) + "-Xmx6g" - options.compilerArgs.add("-Werror") + // With -Werror, a warning stops javac before Error Prone applies its fixes. + if (!errorProneFix) { + options.compilerArgs.add("-Werror") + } // "-processing" avoids javac warning "No processor claimed any of these annotations". options.compilerArgs.add("-Xlint:all,-processing") } @@ -181,7 +192,24 @@ tasks.withType().configureEach { disable("ExtendsObject") // Incorrect when using the Checker Framework. disable("InlineMeSuggester") // `@InlineMe` requires a dependency on error_prone_annotations. disable("ReferenceEquality") // Use Interning Checker instead. + + if (errorProneFix) { + errorproneArgs.add("-XepPatchLocation:IN_PLACE") + } } + if (errorProneFix) { + // Always run Error Prone, even if the compiled classes are up to date or cached. + outputs.upToDateWhen { false } + outputs.cacheIf { false } + } +} + +tasks.register("errorProneFix") { + group = "verification" + description = "Applies Error Prone's suggested fixes to the source files." + dependsOn(tasks.withType()) + // Error Prone's fixes are not formatted. + finalizedBy("spotlessJavaApply") } // PMD linter @@ -201,6 +229,11 @@ tasks.named("pmdTest") { // Checker Framework pluggable type-checking checkerFramework { + // The `errorProneFix` task needs only Error Prone. Also, a Checker Framework error would stop + // javac before Error Prone applies its fixes. + if (errorProneFix) { + skipCheckerFramework = true + } version = libs.versions.checker.framework.get() checkers = listOf( From da2fc0d89197efec788d67b63635a2777aec3e00 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Thu, 19 Nov 2026 12:42:57 -0800 Subject: [PATCH 2/2] Recognize abbreviations of the errorProneFix task name --- build.gradle.kts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f78f0013..0c9185e6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,4 @@ +import java.util.regex.Pattern import net.ltgt.gradle.errorprone.errorprone import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.external.javadoc.StandardJavadocDocletOptions @@ -67,10 +68,34 @@ val addOpensArgs = javacExportTargets.map { "--add-opens=$it" } // True if the `errorProneFix` task was requested on the command line. Then each compilation // rewrites its source files in place, applying Error Prone's suggested fixes. -// Gradle matches task names case-insensitively, so also recognize, say, "errorpronefix". +// This must be known when the build is configured, before Gradle builds its task graph, so it +// examines the task names on the command line. Gradle accepts not only a task's full name, but +// also a case-insensitive variant (such as "errorpronefix") and a camel-case abbreviation (such +// as "ePF" or "errorPr"), so recognize all of those. An abbreviation that matches another task +// as well is ambiguous, and Gradle rejects it. val errorProneFix = - gradle.startParameter.taskNames.any { - it.replaceFirst(Regex("^:"), "").equals("errorProneFix", ignoreCase = true) + gradle.startParameter.taskNames.any { requested -> + val name = requested.removePrefix(":") + if (name.isEmpty() || name.startsWith("-") || name.contains(":")) { + return@any false + } + // This mimics Gradle's NameMatcher: each camel-case part of the requested name must be a + // prefix of the corresponding part of the task name. + val regex = StringBuilder() + val boundary = Pattern.compile("""\b|\p{javaUpperCase}|\p{Digit}""").matcher(name) + var pos = 0 + while (boundary.find()) { + if (boundary.start() > pos) { + regex.append(Pattern.quote(name.substring(pos, boundary.start()))) + regex.append("""[\p{javaLowerCase}\p{Digit}]*""") + } + pos = boundary.start() + } + if (pos < name.length) { + regex.append(Pattern.quote(name.substring(pos))) + regex.append("""[\p{javaLowerCase}\p{Digit}]*""") + } + Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE).matcher("errorProneFix").lookingAt() } java {