Skip to content

Commit

Permalink
refactor: Kotlin idioms (#1063)
Browse files Browse the repository at this point in the history
* Replace String.format

* Use `check` function to check version requirement

* Suppress magic nums in SemanticVersion

* Refine Kdoc

* Suppress MaxLineLength for PATTERN

* Suppress MaxLineLength for SUPPORTED_VERSION

* Fix MaxLineLength for SpotBugsExtension

* Fix MaxLineLength for SpotBugsRunnerForHybrid

* Remove lines

* Fix TooGenericExceptionCaught for SpotBugsRunner

* Suppress LongMethod

* Remove baseline file

* Fix style

* Fix detekt issues

* Replace @see

* Replace Stream usages

* Use buildList

* Rearrange

* Use more Sequence

* Replace Path usages

* Don't use File::toURI

* Remove Optional usages

* Rename toCommandLineOption

* Rearrange

* Use error

* Use apply

* Rearrange

* Remove redundant this

* Remove separator params

* Replace hasProperty usage

* Use require

* Refine comment

* Clean up unnecessary context params

* Less nesting

* Simplify Action types

* Inline getSourceSetContainer

* Suppress unused for Extensions.kt

* Api dump

* Mark internal classes

* Refined comments by AI

* Inline values
  • Loading branch information
Goooler authored Dec 7, 2023
1 parent b1760a0 commit ca33175
Show file tree
Hide file tree
Showing 20 changed files with 354 additions and 524 deletions.
3 changes: 1 addition & 2 deletions api/spotbugs-gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public abstract class com/github/spotbugs/snom/Confidence : java/lang/Enum {
public static final field MEDIUM Lcom/github/spotbugs/snom/Confidence;
public synthetic fun <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public abstract fun toCommandLineOption ()Ljava/util/Optional;
public static fun valueOf (Ljava/lang/String;)Lcom/github/spotbugs/snom/Confidence;
public static fun values ()[Lcom/github/spotbugs/snom/Confidence;
}
Expand Down Expand Up @@ -80,6 +79,7 @@ public abstract class com/github/spotbugs/snom/SpotBugsReport : org/gradle/api/r
public synthetic fun configure (Lgroovy/lang/Closure;)Ljava/lang/Object;
public fun configure (Lgroovy/lang/Closure;)Lorg/gradle/api/reporting/Report;
public final fun configure (Lorg/gradle/api/Action;)Lorg/gradle/api/reporting/Report;
public abstract fun getCommandLineOption ()Ljava/lang/String;
public final fun getDestination ()Ljava/io/File;
public fun getDisplayName ()Ljava/lang/String;
public fun getOutputLocation ()Lorg/gradle/api/file/RegularFileProperty;
Expand All @@ -95,7 +95,6 @@ public abstract class com/github/spotbugs/snom/SpotBugsReport : org/gradle/api/r
public final fun setEnabled (Z)V
public fun setStylesheet (Ljava/lang/String;)V
public fun setStylesheet (Lorg/gradle/api/resources/TextResource;)V
public abstract fun toCommandLineOption ()Ljava/lang/String;
}

public abstract class com/github/spotbugs/snom/SpotBugsTask : org/gradle/api/DefaultTask, org/gradle/api/tasks/VerificationTask {
Expand Down
31 changes: 0 additions & 31 deletions detekt-baseline.xml

This file was deleted.

26 changes: 12 additions & 14 deletions src/main/kotlin/com/github/spotbugs/snom/Confidence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package com.github.spotbugs.snom

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

/**
Expand All @@ -22,21 +21,19 @@ import org.gradle.api.tasks.Internal
*
* ### Usage
*
* Set via the {@code spotbugs} extension to configure all tasks in your project:
* Set via the [SpotBugsExtension] to configure all tasks in your project:
* ```kotlin
* // require Gradle 8.2+
* import com.github.spotbugs.snom.Confidence
* spotbugs {
* reportLevel = Confidence.LOW
* reportLevel = com.github.spotbugs.snom.Confidence.LOW
* }
* ```
*
* Or via [SpotBugsTask] to configure the specific task in your project:
* ```kotlin
* // require Gradle 8.2+
* import com.github.spotbugs.snom.Confidence
* spotbugsMain { // or name of another task
* reportLevel = Confidence.LOW
* reportLevel = com.github.spotbugs.snom.Confidence.LOW
* }
* ```
*
Expand All @@ -45,24 +42,25 @@ import org.gradle.api.tasks.Internal
enum class Confidence {
/** The report level to report all detected bugs in the report. */
LOW {
override fun toCommandLineOption(): Optional<String> = Optional.of("-low")
override val commandLineOption: String = "-low"
},

/** The report level to report medium and high priority detected bugs in the report. */
MEDIUM {
override fun toCommandLineOption(): Optional<String> = Optional.of("-medium")
override val commandLineOption: String = "-medium"
},

/** The default level that provides the same feature with {@link #MEDIUM}. */
/** The default level that provides the same feature with [MEDIUM]. */
DEFAULT {
override fun toCommandLineOption(): Optional<String> = Optional.empty()
override val commandLineOption: String? = null
},

/** The report level to report high priority detected bugs in the report. */
HIGH {
override fun toCommandLineOption(): Optional<String> = Optional.of("-high")
}, ;
override val commandLineOption: String = "-high"
},
;

@Internal("This is internally used property so no need to refer to judge out-of-date or not.")
abstract fun toCommandLineOption(): Optional<String>
@get:Internal("This is internally used property so no need to refer to judge out-of-date or not.")
internal abstract val commandLineOption: String?
}
6 changes: 2 additions & 4 deletions src/main/kotlin/com/github/spotbugs/snom/Effort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ package com.github.spotbugs.snom
* Set via the `spotbugs` extension to configure all tasks in your project:
* ```kotlin
* // require Gradle 8.2+
* import com.github.spotbugs.snom.Effort
* spotbugs {
* effort = Effort.LESS
* effort = com.github.spotbugs.snom.Effort.LESS
* }
* ```
*
* Or via [SpotBugsTask] to configure the specific task in your project:
* ```kotlin
* // require Gradle 8.2+
* import com.github.spotbugs.snom.Effort
* spotbugsMain { // or name of another task
* effort = Effort.MAX
* effort = com.github.spotbugs.snom.Effort.MAX
* }
* ```
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/github/spotbugs/snom/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package com.github.spotbugs.snom

import org.gradle.api.provider.Property
Expand Down
112 changes: 35 additions & 77 deletions src/main/kotlin/com/github/spotbugs/snom/SpotBugsBasePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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
Expand All @@ -31,52 +30,33 @@ class SpotBugsBasePlugin : Plugin<Project> {
val extension = createExtension(project)
createConfiguration(project, extension)
createPluginConfiguration(project.configurations)
val enableWorkerApi = getPropertyOrDefault(project, FEATURE_FLAG_WORKER_API, "true")
project
.tasks
.withType(SpotBugsTask::class.java)
.configureEach { task ->
task.init(
extension,
enableWorkerApi.toBoolean(),
)
}
val enableWorkerApi = project.providers.gradleProperty(FEATURE_FLAG_WORKER_API).getOrElse("true")
project.tasks.withType(SpotBugsTask::class.java).configureEach {
it.init(extension, enableWorkerApi.toBoolean())
}
}

private fun createExtension(project: Project): SpotBugsExtension {
val extension =
project
.extensions
.create(
SpotBugsPlugin.EXTENSION_NAME,
SpotBugsExtension::class.java,
)
extension.ignoreFailures.convention(false)
extension.showStackTraces.convention(false)
extension.projectName.convention(project.provider { project.name })
extension.release.convention(
project.provider {
project.version.toString()
},
)
return project.extensions.create(SpotBugsPlugin.EXTENSION_NAME, SpotBugsExtension::class.java).apply {
ignoreFailures.convention(false)
showStackTraces.convention(false)
projectName.convention(project.provider { project.name })
release.convention(
project.provider {
project.version.toString()
},
)

// ReportingBasePlugin should be applied before we create this SpotBugsExtension instance
val baseReportsDir =
project.extensions.getByType(
ReportingExtension::class.java,
).baseDirectory
extension
.reportsDir
.convention(
baseReportsDir.map { directory: Directory ->
directory.dir(
DEFAULT_REPORTS_DIR_NAME,
)
// ReportingBasePlugin should be applied before we create this SpotBugsExtension instance
val baseReportsDir = project.extensions.getByType(ReportingExtension::class.java).baseDirectory
reportsDir.convention(
baseReportsDir.map {
it.dir(DEFAULT_REPORTS_DIR_NAME)
},
)
extension.useAuxclasspathFile.convention(true)
extension.useJavaToolchains.convention(true)
return extension
useAuxclasspathFile.convention(true)
useJavaToolchains.convention(true)
}
}

private fun createConfiguration(
Expand All @@ -91,37 +71,30 @@ class SpotBugsBasePlugin : Plugin<Project> {
it.setDescription("configuration for the SpotBugs engine")
it.setVisible(false)
it.setTransitive(true)
it.defaultDependencies { d ->
val dep =
project
.dependencies
.create("com.github.spotbugs:spotbugs:" + extension.toolVersion.get())
d.add(dep)
it.defaultDependencies { deps ->
val dep = project.dependencies.create("com.github.spotbugs:spotbugs:" + extension.toolVersion.get())
deps.add(dep)
}
}

configs.register(
SpotBugsPlugin.SLF4J_CONFIG_NAME,
) {
configs.register(SpotBugsPlugin.SLF4J_CONFIG_NAME) {
it.description = "configuration for the SLF4J provider to run SpotBugs"
it.setVisible(false)
it.setTransitive(true)
it.defaultDependencies { d ->
val dep =
project
.dependencies
.create("org.slf4j:slf4j-simple:" + props.getProperty("slf4j-version"))
d.add(dep)
it.defaultDependencies { deps ->
val dep = project.dependencies.create("org.slf4j:slf4j-simple:" + props.getProperty("slf4j-version"))
deps.add(dep)
}
}
}

fun loadProperties(): Properties {
val url = SpotBugsPlugin::class.java.classLoader.getResource("spotbugs-gradle-plugin.properties")
url ?: error("spotbugs-gradle-plugin.properties not found")
try {
url!!.openStream().use { input ->
url.openStream().use {
val prop = Properties()
prop.load(input)
prop.load(it)
return prop
}
} catch (e: IOException) {
Expand All @@ -130,35 +103,19 @@ class SpotBugsBasePlugin : Plugin<Project> {
}

private fun createPluginConfiguration(configs: ConfigurationContainer) {
configs.register(
SpotBugsPlugin.PLUGINS_CONFIG_NAME,
) {
configs.register(SpotBugsPlugin.PLUGINS_CONFIG_NAME) {
it.setDescription("configuration for the external SpotBugs plugins")
it.setVisible(false)
it.setTransitive(false)
}
}

fun verifyGradleVersion(version: GradleVersion) {
if (version < SUPPORTED_VERSION) {
val message =
String.format(
"Gradle version %s is unsupported. Please use %s or later.",
version,
SUPPORTED_VERSION,
)
throw IllegalArgumentException(message)
require(version >= SUPPORTED_VERSION) {
"Gradle version $version is unsupported. Please use $SUPPORTED_VERSION or later."
}
}

private fun getPropertyOrDefault(
project: Project,
propertyName: String,
defaultValue: String,
): String {
return if (project.hasProperty(propertyName)) project.property(propertyName).toString() else defaultValue
}

companion object {
private const val FEATURE_FLAG_WORKER_API = "com.github.spotbugs.snom.worker"
private const val DEFAULT_REPORTS_DIR_NAME = "spotbugs"
Expand All @@ -167,6 +124,7 @@ class SpotBugsBasePlugin : Plugin<Project> {
* Supported Gradle version described at [official manual site](http://spotbugs.readthedocs.io/en/latest/gradle.html).
* The convention API provides replacement from 7.1 and later, so we use this value as minimal required version.
*/
@Suppress("MaxLineLength")
private val SUPPORTED_VERSION = GradleVersion.version("7.1")
}
}
27 changes: 14 additions & 13 deletions src/main/kotlin/com/github/spotbugs/snom/SpotBugsExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property

/**
* The extension to configure the SpotBugs Gradle plugin. Most of properties in this extension will be used as the default property of all {@link SpotBugsTask}.
* All properties are optional.
* [SpotBugsExtension] is an extension used to set up the SpotBugs Gradle plugin.
* All properties in this extension act as default properties for all instances of [SpotBugsTask] are optional.
*
* ### Usage
* After you apply the SpotBugs Gradle plugin to project, write extension like below:
* Once you've applied the SpotBugs Gradle plugin to your project, configure it as shown below:
*
* ```kotlin
* // require Gradle 8.2+
* import com.github.spotbugs.snom.Confidence
* import com.github.spotbugs.snom.Effort
* // Required: Gradle 8.2 or higher
* spotbugs {
* ignoreFailures = false
* showStackTraces = true
* showProgress = true
* effort = Effort.DEFAULT
* reportLevel = Confidence.DEFAULT
* effort = com.github.spotbugs.snom.Effort.DEFAULT
* reportLevel = com.github.spotbugs.snom.Confidence.DEFAULT
* visitors = listOf("FindSqlInjection", "SwitchFallthrough")
* omitVisitors = listOf("FindNonShortCircuit")
* reportsDir = file("$buildDir/spotbugs")
Expand Down Expand Up @@ -89,7 +88,7 @@ interface SpotBugsExtension {
* Property to set the filter file to limit which bug should be reported.
*
* Note that this property will NOT limit which bug should be detected. To limit the target classes to analyze,
* use [#onlyAnalyze] instead.
* use [onlyAnalyze] instead.
* To limit the visitors (detectors) to run, use [visitors] and [omitVisitors] instead.
*
* See also [SpotBugs Manual about Filter file](https://spotbugs.readthedocs.io/en/stable/filter.html).
Expand All @@ -108,8 +107,8 @@ interface SpotBugsExtension {
val excludeFilter: RegularFileProperty

/**
* Property to set the baseline file. This file is a Spotbugs result file, and all bugs reported in this file will not be
* reported in the final output.
* Property to set the baseline file. This file is a Spotbugs result file, and all bugs reported in this file
* will not be reported in the final output.
*/
val baselineFile: RegularFileProperty

Expand All @@ -119,7 +118,8 @@ interface SpotBugsExtension {
val onlyAnalyze: ListProperty<String>

/**
* Property to specify the name of project. Some reporting formats use this property. Default value is the name of your Gradle project.
* Property to specify the name of project. Some reporting formats use this property.
* Default value is the name of your Gradle project.
*/
val projectName: Property<String>

Expand All @@ -136,7 +136,8 @@ interface SpotBugsExtension {
val extraArgs: ListProperty<String>

/**
* Property to specify the extra arguments for JVM process. Default value is empty so JVM process will get no extra argument.
* Property to specify the extra arguments for JVM process. Default value is empty so JVM process will get
* no extra argument.
*/
val jvmArgs: ListProperty<String>

Expand Down
Loading

0 comments on commit ca33175

Please sign in to comment.