Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -65,6 +66,38 @@ 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.
// 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 { 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 {
toolchain {
// Always compile using Java 21. The "test" task below overrides this for
Expand All @@ -80,7 +113,10 @@ tasks.withType<JavaCompile>().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")
}
Expand Down Expand Up @@ -181,9 +217,26 @@ tasks.withType<JavaCompile>().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<JavaCompile>())
// Error Prone's fixes are not formatted.
finalizedBy("spotlessJavaApply")
}

// PMD linter

pmd {
Expand All @@ -201,6 +254,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(
Expand Down