Skip to content

Commit

Permalink
feat: rewrite the implementation into Kotlin (#924)
Browse files Browse the repository at this point in the history
Translate codes in `src/main/groovy` into Kotlin.

BREAKING CHANGE: This plugin has been rewritten in Kotlin, and it may break the binary compatibility of public API. Intentional changes are listed as follows:

### Changes for Groovy buildscripts

About `effort` and `reportLevel` properties of `SpotBugsTask` and `SpotBugsExtension`, Groovy buildscripts should use use `valueOf(String)` method explicitly. This limitation is caused by a [known issue of the Groovy language](https://discuss.kotlinlang.org/t/bug-cannot-use-kotlin-enum-from-groovy/1521):

```groovy
// before (v5)
spotbugs {
    effort = 'default'
    reportLevel = 'default'
}

// after (v6)
spotbugs {
    effort = Effort.valueOf('DEFAULT')
    reportLevel = Confidence.valueOf('DEFAULT')
}
```

### Changes for Kotlin buildscripts

It is recommended to use Gradle 8.2 or later, then you can enjoy the [simple property assignment](https://docs.gradle.org/8.2/release-notes.html#simple-property-assignment-in-kotlin-dsl-enabled-by-default) feature by default:

```kotlin
// legacy (Gradle 8.1 and older)
import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort

spotbugs {
    effort.set(Effort.DEFAULT)
    reportLevel.set(Confidence.DEFAULT)
}

// new (Gradle 8.2 and later)
import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort

spotbugs {
    effort = Effort.DEFAULT
    reportLevel = Confidence.DEFAULT
}
```

It is also possible to use string values, however, it is not recommended due to lack of type-safety:

```kotlin
// new (Gradle 8.2 and later)
import com.github.spotbugs.snom.assign

spotbugs {
    effort = "DEFAULT"
    reportLevel = "DEFAULT"
}
```
  • Loading branch information
KengoTODA authored Aug 13, 2023
1 parent 8076fb3 commit bcf4706
Show file tree
Hide file tree
Showing 58 changed files with 2,040 additions and 2,201 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ on:
push:
branches:
- master
- v6
pull_request:
branches:
- master
- v6

jobs:
CodeQL-Build:
Expand All @@ -29,15 +31,14 @@ jobs:
with:
distribution: 'temurin'
java-version: 11
cache: gradle
- name: Gradle Wrapper Validation
uses: gradle/wrapper-validation-action@v1

- name: Build
run: |
./gradlew spotbugsMain
- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: detekt --scan
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v1
with:
# Path to SARIF file relative to the root of the repository
sarif_file: build/reports/spotbugs/main.sarif
sarif_file: build/reports/detekt/detekt.sarif
8 changes: 5 additions & 3 deletions .github/workflows/javadoc.yml → .github/workflows/dokka.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master

jobs:
javadoc:
runs-on: ubuntu-latest
Expand All @@ -16,14 +17,15 @@ jobs:
with:
distribution: 'temurin'
java-version: 11
cache: gradle
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: npm
- name: Generate Groovydoc
run: ./gradlew groovydoc
uses: gradle/gradle-build-action@v2
with:
arguments: dokkaHtml
- name: Prepare to Deploy
run: |
npm ci
Expand All @@ -33,4 +35,4 @@ jobs:
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
FOLDER: build/docs/groovydoc/
FOLDER: build/dokka/html/
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
gradle: ['7.6.2', '8.0']
gradle: ['7.6.2', '8.1', '8.2']
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -39,7 +39,7 @@ jobs:
arguments: build -Dsnom.test.functional.gradle=${{ matrix.gradle }} --scan
- run: |
echo Verifying the java version used in class files...
cd build/classes/groovy/main
cd build/classes/kotlin/main
javap -v com.github.spotbugs.snom.SpotBugsPlugin | grep -q 'major version: 52'
- name: Run Semantic Release
run: |
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ $RECYCLE.BIN/
### Gradle ###
.gradle
build/
gradle.properties

# Ignore Gradle GUI config
gradle-app.setting
Expand Down
13 changes: 13 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions .idea/runConfigurations.xml

This file was deleted.

121 changes: 61 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,45 @@ Refer [the Gradle Plugin portal](https://plugins.gradle.org/plugin/com.github.sp

Configure `spotbugs` extension to configure the behaviour of tasks:

```kotlin
// require Gradle 8.2+
import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort
spotbugs {
ignoreFailures = false
showStackTraces = true
showProgress = true
effort = Effort.DEFAULT
reportLevel = Confidence.DEFAULT
visitors = listOf("FindSqlInjection", "SwitchFallthrough")
omitVisitors = listOf("FindNonShortCircuit")
reportsDir = file("$buildDir/spotbugs")
includeFilter = file("include.xml")
excludeFilter = file("exclude.xml")
baselineFile = file("baseline.xml")
onlyAnalyze = listOf("com.foobar.MyClass", "com.foobar.mypkg.*")
maxHeapSize = "1g"
extraArgs = listOf("-nested:false")
jvmArgs = listOf("-Duser.language=ja")
}
```

<details>
<summary>with Groovy DSL</summary>

```groovy
import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort
spotbugs {
ignoreFailures = false
showStackTraces = true
showProgress = true
effort = 'default'
reportLevel = 'default'
// https://discuss.kotlinlang.org/t/bug-cannot-use-kotlin-enum-from-groovy/1521
// https://touk.pl/blog/2018/05/28/testing-kotlin-with-spock-part-2-enum-with-instance-method/
effort = Effort.valueOf('DEFAULT')
reportLevel = Confidence.valueOf('DEFAULT')
visitors = [ 'FindSqlInjection', 'SwitchFallthrough' ]
omitVisitors = [ 'FindNonShortCircuit' ]
reportsDir = file("$buildDir/spotbugs")
Expand All @@ -48,63 +80,41 @@ spotbugs {
jvmArgs = [ '-Duser.language=ja' ]
}
```

<details>
<summary>with Kotlin DSL</summary>

```kotlin
spotbugs {
ignoreFailures.set(false)
showStackTraces.set(true)
showProgress.set(true)
effort.set(com.github.spotbugs.snom.Effort.DEFAULT)
reportLevel.set(com.github.spotbugs.snom.Confidence.DEFAULT)
visitors.set(listOf("FindSqlInjection", "SwitchFallthrough"))
omitVisitors.set(listOf("FindNonShortCircuit"))
reportsDir.set(file("$buildDir/spotbugs"))
includeFilter.set(file("include.xml"))
excludeFilter.set(file("exclude.xml"))
baselineFile.set(file("baseline.xml"))
onlyAnalyze.set(listOf("com.foobar.MyClass", "com.foobar.mypkg.*"))
maxHeapSize.set("1g")
extraArgs.set(listOf("-nested:false"))
jvmArgs.set(listOf("-Duser.language=ja"))
}
```
</details>

Configure `spotbugsPlugin` to apply any SpotBugs plugin:

```groovy
```kotlin
dependencies {
spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
spotbugsPlugins("com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0")
}
```

<details>
<summary>with Kotlin DSL</summary>
<summary>with Groovy DSL</summary>

```kotlin
```groovy
dependencies {
spotbugsPlugins("com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0")
spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.12.0'
}
```
</details>

Configure `spotbugs` to choose your favorite SpotBugs version:

```groovy
```kotlin
dependencies {
spotbugs 'com.github.spotbugs:spotbugs:4.7.1'
spotbugs("com.github.spotbugs:spotbugs:4.7.1")
}
```


<details>
<summary>with Kotlin DSL</summary>
<summary>with Groovy DSL</summary>

```kotlin
```groovy
dependencies {
spotbugs("com.github.spotbugs:spotbugs:4.7.1")
spotbugs 'com.github.spotbugs:spotbugs:4.7.1'
}
```
</details>
Expand All @@ -125,6 +135,19 @@ TBU
Configure [`SpotBugsTask`](https://spotbugs-gradle-plugin.netlify.com/com/github/spotbugs/snom/spotbugstask) directly,
to set task-specific properties.

```kotlin
// require Gradle 8.2+
tasks.spotbugsMain {
reports.create("html") {
required = true
outputLocation = file("$buildDir/reports/spotbugs.html")
setStylesheet("fancy-hist.xsl")
}
}
```

<details>
<summary>with Groovy DSL</summary>
```groovy
// Example to configure HTML report
spotbugsMain {
Expand All @@ -137,47 +160,25 @@ spotbugsMain {
}
}
```

<details>
<summary>with Kotlin DSL</summary>

```kotlin
tasks.spotbugsMain {
reports.create("html") {
required.set(true)
outputLocation.set(file("$buildDir/reports/spotbugs.html"))
setStylesheet("fancy-hist.xsl")
}
}
```
</details>

## SpotBugs version mapping

By default, this Gradle Plugin uses the SpotBugs version listed in this table.
By default, this Gradle Plugin uses the SpotBugs version listed in the following table.

You can change SpotBugs version by [the `toolVersion` property of the spotbugs extension](https://spotbugs-gradle-plugin.netlify.com/com/github/spotbugs/snom/spotbugsextension#toolVersion) or the `spotbugs` configuration.

| Gradle Plugin | SpotBugs |
|--------------:|---------:|
| 6.0.0 | 4.7.3 |
| 5.1.x | 4.7.3 |
| 5.0.13 | 4.7.3 |
| 5.0.12 | 4.7.2 |
| 5.0.9 | 4.7.1 |
| 5.0.7 | 4.7.0 |
| 5.0.4 | 4.5.3 |
| 5.0.3 | 4.5.2 |
| 5.0.2 | 4.5.1 |
| 4.7.10 | 4.5.0 |
| 4.7.8 | 4.4.2 |
| 4.7.5 | 4.4.1 |
| 4.7.3 | 4.4.0 |
| 4.7.2 | 4.3.0 |
| 4.6.1 | 4.2.1 |
| 4.5.0 | 4.1.1 |
| 4.4.4 | 4.0.6 |
| 4.4.2 | 4.0.5 |
| 4.0.7 | 4.0.2 |
| 4.0.0 | 4.0.0 |

### Refer the version in the build script

Expand Down
Loading

0 comments on commit bcf4706

Please sign in to comment.