かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot + npm + Geb で入力フォームを作ってテストする ( その74 )( FindBugs 3.0.1 → SpotBugs 3.1.3 に切り替える )

概要

記事一覧はこちらです。

Spring Boot + npm + Geb で入力フォームを作ってテストする ( 番外編 )( IntelliJ IDEA に Rainbow Brackets をインストールする ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • バージョンアップ時に FindBugs で警告が出たので確認しようと思いましたが、この機会に SpotBugs に切り替えてしまおうと思います。
    • 今回から JDK を 8u181 に変更しています。

参照したサイト・書籍

目次

  1. build.gradle を変更する
  2. config/findbugs/findbugs-exclude.xml → config/spotbugs/spotbugs-exclude-filter.xml に変更する
  3. clean タスク → Rebuild Project → build タスクを実行する
  4. 出力された警告を解消する
    1. null になっている可能性があるメソッドの戻り値を利用しています。(Bug type NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE )
    2. target は,非 null でなければならないが null 可能としてマークされています。(Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE)
  5. 再び clean タスク → Rebuild Project → build タスクを実行する
  6. IntelliJ IDEA から FindBugs-IDEA Plugin をアンインストールする

手順

build.gradle を変更する

FindBugs 3.0 から SpotBugs 3.1 への移行ガイド を参考に build.gradle の以下の点を変更します。

plugins {
    ..........
    id "checkstyle"
    id "com.github.spotbugs" version "1.6.2"
    id "pmd"
    ..........
}

..........

spotbugs {
    toolVersion = '3.1.3'
    ignoreFailures = true
    effort = "max"
    excludeFilter = file("${rootProject.projectDir}/config/spotbugs/spotbugs-exclude-filter.xml")
    spotbugsTest.enabled = false
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

..........

dependencies {
    def spockVersion = "1.1-groovy-2.4"
    def domaVersion = "2.19.2"
    def lombokVersion = "1.18.0"
    def errorproneVersion = "2.3.1"
    def powermockVersion = "2.0.0-beta.5"
    def seleniumVersion = "3.13.0"
    def spotbugsVersion = "3.1.3"

    ..........

    // for SpotBugs
    compileOnly("com.github.spotbugs:spotbugs:${spotbugsVersion}")
    compileOnly("net.jcip:jcip-annotations:1.0")
    compileOnly("com.github.spotbugs:spotbugs-annotations:${spotbugsVersion}")
    testImplementation("com.google.code.findbugs:jsr305:3.0.2")
}
  • plugins block の以下の点を変更します。
    • id "findbugs"id "com.github.spotbugs" version "1.6.2" に変更します。
  • タスク名を findbugsspotbugs に変更し、以下の点を変更します。
    • toolVersion = '3.0.1'toolVersion = '3.1.3' に変更します。
    • excludeFilter = file("${rootProject.projectDir}/config/findbugs/findbugs-exclude.xml")excludeFilter = file("${rootProject.projectDir}/config/spotbugs/spotbugs-exclude-filter.xml") に変更します。
  • tasks.withType(FindBugs)tasks.withType(com.github.spotbugs.SpotBugsTask) に変更します。
  • dependencies block の以下の点を変更します。
    • 以下の4行を追加します。
      • def spotbugsVersion = "3.1.3"
      • compileOnly("com.github.spotbugs:spotbugs:${spotbugsVersion}")
      • compileOnly("net.jcip:jcip-annotations:1.0")
      • compileOnly("com.github.spotbugs:spotbugs-annotations:${spotbugsVersion}")
    • 以下の1行を SpotBugs のライブラリの記述の下に移動します。
      • testImplementation("com.google.code.findbugs:jsr305:3.0.2")

変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。

config/findbugs/findbugs-exclude.xml → config/spotbugs/spotbugs-exclude-filter.xml に変更する

config/findbugs/findbugs-exclude.xml → config/spotbugs/spotbugs-exclude-filter.xml にフォルダ名、ファイル名を変更します。また フィルタファイル を参考にファイルの内容の以下の点を変更します。

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter
        xmlns="https://github.com/spotbugs/filter/3.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
    <Match>
        <Class name="ksbysample.webapp.lending.web.lendingapp.LendingappService"/>
        <Bug pattern="DLS_DEAD_LOCAL_STORE"/>
    </Match>
</FindBugsFilter>
  • <FindBugsFilter><FindBugsFilter xmlns="https://github.com/spotbugs/filter/3.0.0" ...> に変更します。

clean タスク → Rebuild Project → build タスクを実行する

clean タスク → Rebuild Project → build タスクを実行してみると BUILD SUCCESSFUL の文字が出力されました。ただし SpotBugs rule violations were found. のメッセージが出るのは FindBugs の時と変わりませんでした。

f:id:ksby:20180804145611p:plain

レポートファイルは build/reports/spotbugs/main.html に作成されており、開いてみると FindBugs の時と同じく警告が5件出ています。

f:id:ksby:20180804150233p:plain

出力された警告を解消する

null になっている可能性があるメソッドの戻り値を利用しています。(Bug type NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE

警告が出ている原因の箇所は src/main/java/ksbysample/webapp/bootnpmgeb/aspect/logging/RequestAndResponseLogger.java の以下の画像の赤線で囲んだところでした。RequestContextHolder.getRequestAttributes().getResponse() の戻り値が null の可能性があるので警告が出ています。

f:id:ksby:20180804153814p:plain

Optional を使って以下のように変更します。

f:id:ksby:20180804165110p:plain

target は,非 null でなければならないが null 可能としてマークされています。(Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE)

警告が出ている原因の箇所は src/main/java/ksbysample/webapp/bootnpmgeb/web/inquiry/form/InquiryInput02FormValidator.java の以下の画像の赤線で囲んだところでした。

f:id:ksby:20180804165539p:plain

これは validate メソッドの引数を Object target@Nonnull Object target に変更します。

再び clean タスク → Rebuild Project → build タスクを実行する

再び clean タスク → Rebuild Project → build タスクを実行すると、今度は SpotBugs の警告は出ずに BUILD SUCCESSFUL が出力されました。

f:id:ksby:20180804171646p:plain

修正した内容は Spring Boot 2系とは関係ないように思えるのですが、なぜバージョンアップしたら警告が出るようになったのかが分からないですね。。。

IntelliJ IDEA から FindBugs-IDEA Plugin をアンインストールする

FindBugs を使用しなくなったので FindBugs-IDEA Plugin をアンインストールします。

f:id:ksby:20180804172746p:plain

履歴

2018/08/04
初版発行。
2018/08/29
SpotBugs が正常に動作していなかったので build.gradle の spotbugs タスクの以下の点を修正した。
* sourceSets = [project.sourceSets.main] を削除した
* spotbugsTest.enabled = false を追加した