かんがるーさんの日記

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

Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その6 )( FindBugs 3.0.1 → SpotBugs 3.1.7 に切り替える )

概要

記事一覧はこちらです。

Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その5 )( checkstyle を 7.8.1 → 8.12 に、PMD を 5.8.1 → 6.7.0 にバージョンアップする ) の続きです。

参照したサイト・書籍

目次

  1. build.gradle を変更する
  2. config/findbugs/findbugs-exclude.xml を削除する
  3. clean タスク → Rebuild Project → build タスクを実行する
  4. 出力された警告を解消する
    1. org.springframework.validation.Errors.rejectValue(String, String) の 非 null パラメータに null を渡しています。(Bug type NP_NONNULL_PARAM_VIOLATION)
    2. null になっている可能性があるメソッドの戻り値を利用しています。(Bug type NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE)
    3. target は,非 null でなければならないが null 可能としてマークされています。(Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE)
  5. 再度 clean タスク → Rebuild Project → build タスクを実行する
  6. 次回は。。。

手順

build.gradle を変更する

build.gradle の以下の点を変更します。

plugins {
    id "java"
    id "eclipse"
    id "idea"
    id "org.springframework.boot" version "2.0.4.RELEASE"
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
    id "groovy"
    id "checkstyle"
    id "com.github.spotbugs" version "1.6.4"
    id "pmd"
    id "net.ltgt.errorprone" version "0.0.16"
    id "de.undercouch.download" version "3.4.3"
}

..........

spotbugs {
    toolVersion = "3.1.7"
    ignoreFailures = true
    effort = "max"
    spotbugsTest.enabled = false
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

..........

dependencies {
    def jdbcDriver = "org.postgresql:postgresql:42.2.4"
    def spockVersion = "1.1-groovy-2.4"
    def domaVersion = "2.19.3"
    def lombokVersion = "1.18.2"
    def errorproneVersion = "2.3.1"
    def powermockVersion = "2.0.0-beta.5"
    def spotbugsVersion = "3.1.7"

    ..........

    // 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.4" に変更します。
  • タスク名を findbugsspotbugs に変更し、タスク内の以下の点を変更します。
    • toolVersion = "3.0.1"toolVersion = "3.1.7" に変更します。
    • sourceSets = [project.sourceSets.main] を削除します。
    • 警告を無視したい場合には @SuppressFBWarnings を付ければ回避できるので、excludeFilter = file("${rootProject.projectDir}/config/spotbugs/spotbugs-exclude.xml") を削除します。
    • spotbugsTest.enabled = false を追加します。
    • tasks.withType(FindBugs)tasks.withType(com.github.spotbugs.SpotBugsTask) に変更します。
  • dependencies block の以下の点を変更します。
    • 以下の4行を追加します。
      • def spotbugsVersion = "3.1.7"
      • 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")compileOnly("com.github.spotbugs:spotbugs-annotations:${spotbugsVersion}") の下に移動します。

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

config/findbugs/findbugs-exclude.xml を削除する

使用しなくなったので、config/findbugs/findbugs-exclude.xml を削除します。

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

clean タスク → Rebuild Project → build タスクを実行してみると spotbugsMain タスクで SpotBugs rule violations were found. のメッセージが出力されました。

f:id:ksby:20180926011753p:plain

build/reports/spotbugs/main.html を Firefox で開くと、High Priority Warnings が 4件、Medium Priority Warnings が 12件の合計 16件の警告が出ています。

f:id:ksby:20180926012138p:plain

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

org.springframework.validation.Errors.rejectValue(String, String) の 非 null パラメータに null を渡しています。(Bug type NP_NONNULL_PARAM_VIOLATION)

警告が出ている原因の箇所は src/main/java/ksbysample/webapp/lending/web/lendingapp/LendingappFormValidator.java の以下の画像の赤線で囲んだところと、

f:id:ksby:20180926065207p:plain

src/main/java/ksbysample/webapp/lending/web/lendingapproval/LendingapprovalFormValidator.java の以下の画像の赤線で囲んだところでした。

f:id:ksby:20180926073243p:plain

src/main/java/ksbysample/webapp/lending/web/lendingapp/LendingappFormValidator.java は以下のように対応します。

  • errors.rejectValue の第2引数 errcode に null を渡していましたが、非 null パラメータに null を渡しているので警告が出ていました。その後に呼び出している errors.reject の第1引数にしている errcode を渡すようにします(同じエラーのため)。
  • errors.reject の第2引数 defaultMessage に null を渡していましたが、非 null パラメータに null を渡しているので警告が出ていました。errors.reject は第1引数のみのものも定義されているので、第2引数の null を削除します。

f:id:ksby:20180926071224p:plain

src/main/java/ksbysample/webapp/lending/web/lendingapproval/LendingapprovalFormValidator.java は src/main/resources/messages_ja_JP.properties にメッセージを定義して errors.rejectValue で呼び出すように修正します。

LendingapprovalParamForm.lendingAppId.emptyerr=貸出申請IDが指定されていません。
LendingapprovalForm.lendingApp.nodataerr=指定された貸出申請IDでは貸出申請されておりません。
LendingapprovalForm.applyingBookFormList.approvalResult.notAllCheckedErr=全ての書籍で承認か却下を選択してください。
LendingapprovalForm.applyingBookFormList.approvalReason.empty=却下理由が入力されていません。
  • LendingapprovalForm.applyingBookFormList.approvalReason.empty=却下理由が入力されていません。 を追加します。

f:id:ksby:20180926220329p:plain

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

3つのソースで警告が出ており、1つ目は src/main/java/ksbysample/webapp/lending/aspect/logging/RequestAndResponseLogger.java の以下の画像の赤線で囲んだところでした。

f:id:ksby:20180926223026p:plain

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

f:id:ksby:20180926223747p:plain

2つ目は src/main/java/ksbysample/webapp/lending/service/calilapi/CalilApiService.java の以下の画像の赤線で囲んだところでした。

f:id:ksby:20180926230100p:plain

org.springframework.http.HttpEntity#getBody を見ると @Nullable アノテーションが付いていて、null の場合がありうるため警告が出ていました。

f:id:ksby:20180926230320p:plain

response.getBody() を変数に取得して null チェックをするように修正します。

f:id:ksby:20180926235422p:plain

src/main/resources/messages_ja_JP.properties に CalilApiService.checkapi.response.emptybody=蔵書検索APIのレスポンスボディが空です。 のメッセージを追加しています。

3つ目は src/main/java/ksbysample/webapp/lending/service/openweathermapapi/OpenWeatherMapApiService.java の以下の画像の赤線で囲んだところでした。

f:id:ksby:20180928010950p:plain

OpenWeatherMapApi 関連は JSONP のサンプルとして実装しましたが、AbstractJsonpResponseBodyAdvice も非推奨になったので全て削除します。以下のパッケージを削除します。

  • ksbysample.webapp.lending.service.openweathermapapi
  • ksbysample.webapp.lending.webapi.weather
  • ksbysample.webapp.lending.service.openweathermapapi

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

警告が出ているのは以下の3つのソースで、

  • src/main/java/ksbysample/webapp/lending/web/booklist/UploadBooklistFormValidator.java
  • src/main/java/ksbysample/webapp/lending/web/lendingapp/LendingappFormValidator.java
  • src/main/java/ksbysample/webapp/lending/web/lendingapproval/LendingapprovalFormValidator.java

以下の画像の赤線で囲んだところでした。

f:id:ksby:20180928073540p:plain

override 元の org.springframework.validation.Validator#validate では引数 target に @Nullable アノテーションが付いていますが、override した後では NonNull の想定で実装していて矛盾があるという警告でした。

f:id:ksby:20180928073818p:plain

Assert.notNull(target, "target must not be null"); を追加して null の場合には例外が throw されるようにします。

f:id:ksby:20180929004531p:plain

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

再度 clean タスク → Rebuild Project → build タスクを実行してみると spotbugsMain タスクで何も出力されなくなりました。

f:id:ksby:20180929013923p:plain

次回は。。。

いろいろ対応したので一旦 Tomcat を起動して画面が表示できるか確認してみたのですが、画面が正常に表示されませんでした。CSS が適用されていないようです。原因を調査することにします。

f:id:ksby:20180929015332p:plain

履歴

2018/09/29
初版発行。