Skip to content

Commit

Permalink
Refine Kotlin styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Dec 3, 2023
1 parent b6c420e commit 57a4607
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 344 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/spotbugs/snom/Confidence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
package com.github.spotbugs.snom

import org.gradle.api.tasks.Internal
import java.util.Optional
import org.gradle.api.tasks.Internal

/**
* The [Confidence] is used to specify the level to report bugs. Lower level contains more
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
*/
package com.github.spotbugs.snom

import java.io.IOException
import java.io.UncheckedIOException
import java.util.Properties
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.file.Directory
import org.gradle.api.plugins.ReportingBasePlugin
import org.gradle.api.reporting.ReportingExtension
import org.gradle.util.GradleVersion
import java.io.IOException
import java.io.UncheckedIOException
import java.util.Properties

class SpotBugsBasePlugin : Plugin<Project> {
override fun apply(project: Project) {
Expand Down
173 changes: 85 additions & 88 deletions src/main/kotlin/com/github/spotbugs/snom/SpotBugsReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package com.github.spotbugs.snom

import groovy.lang.Closure
import java.io.File
import javax.inject.Inject
import org.gradle.api.Action
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
Expand All @@ -25,109 +27,104 @@ import org.gradle.api.reporting.SingleFileReport
import org.gradle.api.resources.TextResource
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import java.io.File
import javax.inject.Inject

abstract class SpotBugsReport
@Inject
constructor(
objects: ObjectFactory,
@get:Internal
protected val task: SpotBugsTask,
) :
SingleFileReport, CustomizableHtmlReport { // to expose CustomizableHtmlReport#setStylesheet to build script
private val destination: RegularFileProperty
private val isRequired: Property<Boolean>

init {
destination = objects.fileProperty()
isRequired = objects.property(Boolean::class.java).convention(true)
}
abstract class SpotBugsReport @Inject constructor(
objects: ObjectFactory,
@get:Internal
protected val task: SpotBugsTask,
) : SingleFileReport, CustomizableHtmlReport { // to expose CustomizableHtmlReport#setStylesheet to build script
private val destination: RegularFileProperty
private val isRequired: Property<Boolean>

init {
destination = objects.fileProperty()
isRequired = objects.property(Boolean::class.java).convention(true)
}

abstract fun toCommandLineOption(): String
abstract fun toCommandLineOption(): String

@Internal
@Deprecated("use {@link #getOutputLocation()} instead.")
fun getDestination(): File {
return destination.get().asFile
}
@Internal
@Deprecated("use {@link #getOutputLocation()} instead.")
fun getDestination(): File {
return destination.get().asFile
}

override fun getOutputLocation(): RegularFileProperty {
return destination
}
override fun getOutputLocation(): RegularFileProperty {
return destination
}

@Internal("This property returns always same value")
override fun getOutputType(): Report.OutputType {
return Report.OutputType.FILE
}
@Internal("This property returns always same value")
override fun getOutputType(): Report.OutputType {
return Report.OutputType.FILE
}

@Input
override fun getRequired(): Property<Boolean> {
return isRequired
}
@Input
override fun getRequired(): Property<Boolean> {
return isRequired
}

@get:Deprecated("use {@link #getRequired()} instead.")
@get:Internal
@set:Deprecated("use {@code getRequired().set(value)} instead.")
var isEnabled: Boolean
get() = isRequired.get()
set(b) {
isRequired.set(b)
}

@Deprecated("use {@code getRequired().set(provider)} instead.")
fun setEnabled(provider: Provider<Boolean>) {
isRequired.set(provider)
@get:Deprecated("use {@link #getRequired()} instead.")
@get:Internal
@set:Deprecated("use {@code getRequired().set(value)} instead.")
var isEnabled: Boolean
get() = isRequired.get()
set(b) {
isRequired.set(b)
}

@Deprecated("use {@code getOutputLocation().set(file)} instead.")
override fun setDestination(file: File) {
destination.set(file)
}
@Deprecated("use {@code getRequired().set(provider)} instead.")
fun setEnabled(provider: Provider<Boolean>) {
isRequired.set(provider)
}

@Deprecated("use {@code getOutputLocation().set(provider)} instead.")
fun setDestination(provider: Provider<File?>) {
destination.set(task.project.layout.file(provider))
}
@Deprecated("use {@code getOutputLocation().set(file)} instead.")
override fun setDestination(file: File) {
destination.set(file)
}

override fun configure(closure: Closure<in Report>): Report {
return configure { report ->
closure.delegate = report
closure.call(report)
}
}
@Deprecated("use {@code getOutputLocation().set(provider)} instead.")
fun setDestination(provider: Provider<File?>) {
destination.set(task.project.layout.file(provider))
}

@Suppress("MemberVisibilityCanBePrivate")
fun configure(action: Action<in Report>): Report {
action.execute(this)
return this
override fun configure(closure: Closure<in Report>): Report {
return configure { report ->
closure.delegate = report
closure.call(report)
}
}

@Internal("This property provides only a human readable name.")
override fun getDisplayName(): String {
return String.format("%s type report generated by the task %s", name, task.path)
}
@Suppress("MemberVisibilityCanBePrivate")
fun configure(action: Action<in Report>): Report {
action.execute(this)
return this
}

// TODO adding an @Input triggers 'cannot be serialized' exception
override fun getStylesheet(): TextResource? {
return null
}
@Internal("This property provides only a human readable name.")
override fun getDisplayName(): String {
return String.format("%s type report generated by the task %s", name, task.path)
}

override fun setStylesheet(textResource: TextResource?) {
throw UnsupportedOperationException(
String.format(
"stylesheet property is not available in the %s type report",
name,
),
)
}
// TODO adding an @Input triggers 'cannot be serialized' exception
override fun getStylesheet(): TextResource? {
return null
}

open fun setStylesheet(path: String?) {
throw UnsupportedOperationException(
String.format(
"stylesheet property is not available in the %s type report",
name,
),
)
}
override fun setStylesheet(textResource: TextResource?) {
throw UnsupportedOperationException(
String.format(
"stylesheet property is not available in the %s type report",
name,
),
)
}

open fun setStylesheet(path: String?) {
throw UnsupportedOperationException(
String.format(
"stylesheet property is not available in the %s type report",
name,
),
)
}
}
11 changes: 4 additions & 7 deletions src/main/kotlin/com/github/spotbugs/snom/SpotBugsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.github.spotbugs.snom.internal.SpotBugsRunnerForJavaExec
import com.github.spotbugs.snom.internal.SpotBugsSarifReport
import com.github.spotbugs.snom.internal.SpotBugsTextReport
import com.github.spotbugs.snom.internal.SpotBugsXmlReport
import javax.inject.Inject
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.InvalidUserDataException
Expand Down Expand Up @@ -49,7 +50,6 @@ import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService
import org.gradle.workers.WorkerExecutor
import org.slf4j.LoggerFactory
import javax.inject.Inject

/**
* The Gradle task to run the SpotBugs analysis. All properties are optional.
Expand Down Expand Up @@ -272,12 +272,9 @@ abstract class SpotBugsTask : DefaultTask(), VerificationTask {
@get:SkipWhenEmpty
var classes: FileCollection? = null
get() {
return field
?: (
classDirs.asFileTree.filter {
it.name.endsWith(".class")
}
)
return field ?: classDirs.asFileTree.filter {
it.name.endsWith(".class")
}
}

private var enableWorkerApi: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,76 @@ package com.github.spotbugs.snom.internal
import com.github.spotbugs.snom.SpotBugsPlugin
import com.github.spotbugs.snom.SpotBugsReport
import com.github.spotbugs.snom.SpotBugsTask
import javax.inject.Inject
import org.gradle.api.InvalidUserDataException
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.resources.TextResource
import org.gradle.api.resources.TextResourceFactory
import javax.inject.Inject

abstract class SpotBugsHtmlReport
@Inject
constructor(objects: ObjectFactory, task: SpotBugsTask) :
SpotBugsReport(objects, task) {
private val stylesheet: Property<TextResource>
abstract class SpotBugsHtmlReport @Inject constructor(
objects: ObjectFactory,
task: SpotBugsTask,
) : SpotBugsReport(objects, task) {
private val stylesheet: Property<TextResource>

init {
// the default reportsDir is "$buildDir/reports/spotbugs/${baseName}.html"
outputLocation.convention(task.reportsDir.file(task.getBaseName() + ".html"))
stylesheet = task.project.objects.property(TextResource::class.java)
}
init {
// the default reportsDir is "$buildDir/reports/spotbugs/${baseName}.html"
outputLocation.convention(task.reportsDir.file(task.getBaseName() + ".html"))
stylesheet = task.project.objects.property(TextResource::class.java)
}

override fun toCommandLineOption(): String {
return stylesheet.map {
"-html:" + it.asFile().absolutePath
}.getOrElse("-html")
}
override fun toCommandLineOption(): String {
return stylesheet.map {
"-html:" + it.asFile().absolutePath
}.getOrElse("-html")
}

override fun getStylesheet(): TextResource? = stylesheet.orNull
override fun getStylesheet(): TextResource? = stylesheet.orNull

private fun resolve(
path: String,
configuration: Configuration,
textResourceFactory: TextResourceFactory,
): TextResource {
val spotbugsJar =
configuration.files { dependency: Dependency -> dependency.group == "com.github.spotbugs" && dependency.name == "spotbugs" }
.find { it.isFile }
return if (spotbugsJar != null) {
textResourceFactory
.fromArchiveEntry(spotbugsJar, path)
} else {
throw InvalidUserDataException(
"The dependency on SpotBugs not found in 'spotbugs' configuration",
)
}
private fun resolve(
path: String,
configuration: Configuration,
textResourceFactory: TextResourceFactory,
): TextResource {
val spotbugsJar =
configuration.files { dependency: Dependency -> dependency.group == "com.github.spotbugs" && dependency.name == "spotbugs" }
.find { it.isFile }
return if (spotbugsJar != null) {
textResourceFactory
.fromArchiveEntry(spotbugsJar, path)
} else {
throw InvalidUserDataException(
"The dependency on SpotBugs not found in 'spotbugs' configuration",
)
}
}

override fun setStylesheet(textResource: TextResource?) {
stylesheet.set(textResource)
}
override fun setStylesheet(textResource: TextResource?) {
stylesheet.set(textResource)
}

override fun setStylesheet(path: String?) {
if (path == null) {
stylesheet.set(null as TextResource?)
} else {
val configuration =
task
.project
.configurations
.getByName(SpotBugsPlugin.CONFIG_NAME)
val textResourceFactory =
task
.project
.resources
.text
stylesheet.set(
task.project.provider {
resolve(path, configuration, textResourceFactory)
},
)
}
override fun setStylesheet(path: String?) {
if (path == null) {
stylesheet.set(null as TextResource?)
} else {
val configuration =
task
.project
.configurations
.getByName(SpotBugsPlugin.CONFIG_NAME)
val textResourceFactory =
task
.project
.resources
.text
stylesheet.set(
task.project.provider {
resolve(path, configuration, textResourceFactory)
},
)
}
}
}
Loading

0 comments on commit 57a4607

Please sign in to comment.