Repository navigation
Move SpotBugs and forbidden-apis configuration to convention plugins #12933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 5 commits into
master
from
bdu/spotbugs-forbidden-apis-conventions
Oct 21, 2026
+521
−158
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d89ef3f
refactor: Move spotbugs and spotless to convention plugins
bric3 15b256e
refactor: Separate task level behavior from plugin conventin default
bric3 7c8a575
refactor: Improve configurability of spotbugs task per sourceset
bric3 491fc43
fix: correct JDK tunneling socket reads
bric3 b34800f
fix: restrict access to the Karate test events handler
bric3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
buildSrc/src/main/kotlin/dd-trace-java.conventions.forbidden-apis.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| plugins { | ||
| java | ||
| id("de.thetaphi.forbiddenapis") | ||
| } | ||
|
|
||
| forbiddenApis { | ||
| signaturesFiles = files(rootDir.resolve("gradle/forbiddenApiFilters/main.txt")) | ||
| ignoreSignaturesOfMissingClasses = true | ||
| ignoreFailures = false | ||
| } | ||
|
|
||
| tasks.matching { it.name.startsWith("forbiddenApis") && it.name.endsWith("Test") }.configureEach { | ||
| enabled = false | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly(versionCatalogs.named("libs").findLibrary("forbiddenapis").get()) | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
buildSrc/src/main/kotlin/dd-trace-java.conventions.spotbugs.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import com.github.spotbugs.snom.SpotBugsTask | ||
|
|
||
| plugins { | ||
| java | ||
| id("com.github.spotbugs") | ||
| } | ||
|
|
||
| spotbugs { | ||
| ignoreFailures = false | ||
| // Exclude known types and packages | ||
| excludeFilter = rootDir.resolve("gradle/spotbugFilters/exclude.xml") | ||
| showProgress = !providers.environmentVariable("CI").isPresent | ||
| } | ||
|
|
||
| val productionTaskName = Regex("spotbugsMain(?:_java\\d+)?") | ||
|
|
||
| tasks.withType<SpotBugsTask>().configureEach { | ||
| when { | ||
| project.path.startsWith(":dd-smoke-tests") -> { | ||
| enabled = false | ||
| } | ||
| productionTaskName.matches(name) -> { | ||
| omitVisitors = listOf( | ||
| "ConstructorThrow", | ||
| "DefaultEncodingDetector", | ||
| "DoInsideDoPrivileged", | ||
| "DontUseEnum", | ||
| "DroppedException", | ||
| "FindDeadLocalStores", | ||
| "FindHEmismatch", | ||
| "FindNullDeref", | ||
| "FindReturnRef", | ||
| "FindRunInvocations", | ||
| "FindUselessControlFlow", | ||
| "InitializationChain", | ||
| "LazyInit", | ||
| "LoadOfKnownNullValue", | ||
| "LostLoggerDueToWeakReference", | ||
| "MethodReturnCheck", | ||
| "MutableStaticFields", | ||
| "Naming", | ||
| "RuntimeExceptionCapture", | ||
| "SerializableIdiom", | ||
| "UnreadFields", | ||
| ) | ||
| } | ||
| else -> { | ||
| // static-analysis-rules skill: enable selected test tasks here with their own detector configuration. | ||
| enabled = false | ||
| } | ||
| } | ||
|
|
||
| launcher = javaToolchains.launcherFor { | ||
| languageVersion = JavaLanguageVersion.current() | ||
| } | ||
|
|
||
| val reportFile = layout.buildDirectory.file("reports/spotbugs/$name.html") | ||
| reports { | ||
| create("html") { | ||
| required = true | ||
| outputLocation = reportFile | ||
| setStylesheet("fancy-hist.xsl") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val libs = versionCatalogs.named("libs") | ||
| dependencies { | ||
| compileOnly(libs.findLibrary("spotbugs-annotations").get()) | ||
| testImplementation(libs.findLibrary("jsr305").get()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
buildSrc/src/test/kotlin/datadog/gradle/plugin/quality/ForbiddenApisConventionPluginTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| package datadog.gradle.plugin.quality | ||
|
|
||
| import datadog.gradle.plugin.GradleFixture | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.gradle.testkit.runner.TaskOutcome | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import java.io.File | ||
|
|
||
| class ForbiddenApisConventionPluginTest : GradleFixture() { | ||
| @BeforeEach | ||
| fun setup() { | ||
| writeSettings("""rootProject.name = "forbidden-apis-convention-test"""") | ||
| writeFile("gradle/libs.versions.toml", File("../gradle/libs.versions.toml").readText()) | ||
| writeFile( | ||
| "gradle/forbiddenApiFilters/main.txt", | ||
| """ | ||
| java.lang.System#exit(int) | ||
| missing.OptionalDependency | ||
| """ | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `preserves defaults and name-based exclusions for late source sets and tasks`() { | ||
| addSubproject( | ||
| "library", | ||
| """ | ||
| import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis | ||
|
|
||
| plugins { | ||
| id("dd-trace-java.conventions.forbidden-apis") | ||
| } | ||
|
|
||
| sourceSets.create("latestDepTest") | ||
| sourceSets.create("main_java11") | ||
| sourceSets.create("testFixtures") | ||
| sourceSets.create("jmh") | ||
| tasks.register("forbiddenApisSyntheticTest") | ||
| tasks.register("unrelatedTest") | ||
|
|
||
| tasks.register("verifyDefaults") { | ||
| doLast { | ||
| val mainFilter = rootProject.file("gradle/forbiddenApiFilters/main.txt") | ||
| tasks.withType<CheckForbiddenApis>().forEach { | ||
| check(it.signaturesFiles.files == setOf(mainFilter)) | ||
| check(it.ignoreSignaturesOfMissingClasses) | ||
| check(!it.ignoreFailures) | ||
| } | ||
| listOf("forbiddenApisTest", "forbiddenApisLatestDepTest", "forbiddenApisSyntheticTest").forEach { | ||
| check(!tasks.getByName(it).enabled) | ||
| } | ||
| listOf("forbiddenApisMain", "forbiddenApisMain_java11", "forbiddenApisTestFixtures", "forbiddenApisJmh", "unrelatedTest").forEach { | ||
| check(tasks.getByName(it).enabled) | ||
| } | ||
| val aggregate = tasks.getByName("forbiddenApis") | ||
| val checkTask = tasks.getByName("check") | ||
| check(aggregate in checkTask.taskDependencies.getDependencies(checkTask)) | ||
| check(tasks.getByName("forbiddenApisMain_java11") in aggregate.taskDependencies.getDependencies(aggregate)) | ||
| val dependency = configurations.getByName("compileOnly").dependencies.single { | ||
| it.group == "de.thetaphi" && it.name == "forbiddenapis" | ||
| } | ||
| check(dependency.version == libs.versions.forbiddenapis.get()) | ||
| check(configurations.getByName("runtimeClasspath").allDependencies.none { it.name == "forbiddenapis" }) | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
|
|
||
| val result = run(":library:verifyDefaults") | ||
|
|
||
| assertThat(result.task(":library:verifyDefaults")?.outcome) | ||
| .describedAs(result.output).isEqualTo(TaskOutcome.SUCCESS) | ||
| } | ||
|
|
||
| @Test | ||
| fun `preserves extension and typed task overrides`() { | ||
| writeFile("gradle/forbiddenApiFilters/instrumentation.txt", "java.lang.Runtime#exit(int)") | ||
| writeRootProject( | ||
| """ | ||
| import de.thetaphi.forbiddenapis.gradle.CheckForbiddenApis | ||
|
|
||
| plugins { | ||
| id("dd-trace-java.conventions.forbidden-apis") | ||
| } | ||
|
|
||
| forbiddenApis { | ||
| signaturesFiles += files("gradle/forbiddenApiFilters/instrumentation.txt") | ||
| ignoreFailures = true | ||
| } | ||
| tasks.named<CheckForbiddenApis>("forbiddenApisMain") { | ||
| failOnMissingClasses = false | ||
| } | ||
| tasks.named("forbiddenApisTest") { | ||
| enabled = true | ||
| } | ||
|
|
||
| tasks.register("verifyOverrides") { | ||
| doLast { | ||
| val main = tasks.named<CheckForbiddenApis>("forbiddenApisMain").get() | ||
| check(main.signaturesFiles.files == setOf(file("gradle/forbiddenApiFilters/main.txt"), file("gradle/forbiddenApiFilters/instrumentation.txt"))) | ||
| check(main.ignoreFailures) | ||
| check(!main.failOnMissingClasses) | ||
| check(tasks.getByName("forbiddenApisTest").enabled) | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
|
|
||
| val result = run("verifyOverrides") | ||
|
|
||
| assertThat(result.task(":verifyOverrides")?.outcome) | ||
| .describedAs(result.output).isEqualTo(TaskOutcome.SUCCESS) | ||
| } | ||
|
|
||
| @Test | ||
| fun `fails check on a forbidden call and allows compile-only suppression`() { | ||
| writeRootProject( | ||
| """ | ||
| plugins { | ||
| id("dd-trace-java.conventions.forbidden-apis") | ||
| } | ||
| repositories { | ||
| mavenCentral() | ||
| } | ||
| """ | ||
| ) | ||
| writeJavaSource("Example", "public class Example { public void exit() { System.exit(0); } }") | ||
|
|
||
| val failed = run("check", expectFailure = true) | ||
|
|
||
| assertThat(failed.task(":forbiddenApisMain")?.outcome) | ||
| .describedAs(failed.output).isEqualTo(TaskOutcome.FAILED) | ||
| assertThat(failed.output).contains("java.lang.System#exit(int)") | ||
|
|
||
| writeJavaSource( | ||
| "Example", | ||
| """ | ||
| import de.thetaphi.forbiddenapis.SuppressForbidden; | ||
|
|
||
| public class Example { | ||
| @SuppressForbidden | ||
| public void exit() { System.exit(0); } | ||
| } | ||
| """ | ||
| ) | ||
|
|
||
| val suppressed = run("check") | ||
|
|
||
| assertThat(suppressed.task(":forbiddenApisMain")?.outcome) | ||
| .describedAs(suppressed.output).isEqualTo(TaskOutcome.SUCCESS) | ||
| assertThat(suppressed.task(":forbiddenApisTest")?.outcome).isEqualTo(TaskOutcome.SKIPPED) | ||
| assertThat(suppressed.task(":check")?.outcome).isEqualTo(TaskOutcome.SUCCESS) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.