Skip to content

Commit

Permalink
fix: broken serialization of NamedDomainObjectFactory on Gradle 7
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgraef committed Dec 1, 2024
1 parent 706bc67 commit f1c4ea7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CacheabilityFunctionalTest extends BaseFunctionalTest {

when:
gradleRunner
.withArguments(':spotbugsMain', '--no-configuration-cache', '--build-cache')
.withArguments(':spotbugsMain', '--build-cache')
.build()
BuildResult result = gradleRunner
.withArguments(':spotbugsMain', '--build-cache')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ spotbugsMain {
when:
gradleRunner
.withArguments(":spotbugsMain", "--no-configuration-cache")
.withArguments(":spotbugsMain")
.build()
def result = gradleRunner
.withArguments(":spotbugsMain")
Expand Down Expand Up @@ -486,7 +486,7 @@ public class MyFoo {
when:
BuildResult result = gradleRunner
.withArguments('--debug', "--no-configuration-cache", "spotbugsMain")
.withArguments('--debug', "spotbugsMain")
.build()
then:
Expand Down
32 changes: 21 additions & 11 deletions src/main/kotlin/com/github/spotbugs/snom/SpotBugsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.InvalidUserDataException
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.NamedDomainObjectFactory
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
Expand Down Expand Up @@ -308,17 +309,26 @@ abstract class SpotBugsTask : DefaultTask(), VerificationTask {

init {
val objects = project.objects
reports = objects.domainObjectContainer(SpotBugsReport::class.java) { name: String ->
when (name) {
"html" -> objects.newInstance(SpotBugsHtmlReport::class.java, name, objects, this)
"xml" -> objects.newInstance(SpotBugsXmlReport::class.java, name, objects, this)
"text" -> objects.newInstance(SpotBugsTextReport::class.java, name, objects, this)
"sarif" -> objects.newInstance(SpotBugsSarifReport::class.java, name, objects, this)
else -> throw InvalidUserDataException("$name is invalid as the report name")
}.also {
(outputs as org.gradle.api.tasks.TaskOutputs).file(it.outputLocation)
}
}
val taskRef = this
reports = objects.domainObjectContainer(
SpotBugsReport::class.java,
// This anonymous object is necessary
// Otherwise the serialization of this lambda is broken with config-cache on Gradle 7
@Suppress("ObjectLiteralToLambda")
object : NamedDomainObjectFactory<SpotBugsReport> {
override fun create(name: String): SpotBugsReport {
return when (name) {
"html" -> objects.newInstance(SpotBugsHtmlReport::class.java, name, objects, taskRef)
"xml" -> objects.newInstance(SpotBugsXmlReport::class.java, name, objects, taskRef)
"text" -> objects.newInstance(SpotBugsTextReport::class.java, name, objects, taskRef)
"sarif" -> objects.newInstance(SpotBugsSarifReport::class.java, name, objects, taskRef)
else -> throw InvalidUserDataException("$name is invalid as the report name")
}.also {
(outputs as org.gradle.api.tasks.TaskOutputs).file(it.outputLocation)
}
}
},
)
description = "Run SpotBugs analysis."
group = JavaBasePlugin.VERIFICATION_GROUP
}
Expand Down

0 comments on commit f1c4ea7

Please sign in to comment.