Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その6 )( FindBugs 3.0.1 → SpotBugs 3.1.7 に切り替える )
概要
記事一覧はこちらです。
- 今回の手順で確認できるのは以下の内容です。
- FindBugs 3.0.1 → SpotBugs 3.1.7 に切り替えます。
- Spring Boot + npm + Geb で入力フォームを作ってテストする ( その74 )( FindBugs 3.0.1 → SpotBugs 3.1.3 に切り替える ) を参考に進めます。
参照したサイト・書籍
How do I configure Jackson Serialization on LocalDateTime and LocalDate for Java?
https://stackoverflow.com/questions/51527794/how-do-i-configure-jackson-serialization-on-localdatetime-and-localdate-for-java- 今回の記事とは関係ありません。作業中に見つけたのでメモとして残しています。
目次
- build.gradle を変更する
- config/findbugs/findbugs-exclude.xml を削除する
- clean タスク → Rebuild Project → build タスクを実行する
- 出力された警告を解消する
- org.springframework.validation.Errors.rejectValue(String, String) の 非 null パラメータに null を渡しています。(Bug type NP_NONNULL_PARAM_VIOLATION)
- null になっている可能性があるメソッドの戻り値を利用しています。(Bug type NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE)
- target は,非 null でなければならないが null 可能としてマークされています。(Bug type NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE)
- 再度 clean タスク → Rebuild Project → build タスクを実行する
- 次回は。。。
手順
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"
に変更します。
- タスク名を
findbugs
→spotbugs
に変更し、タスク内の以下の点を変更します。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}")
の下に移動します。
- 以下の4行を追加します。
変更後、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.
のメッセージが出力されました。
build/reports/spotbugs/main.html を Firefox で開くと、High Priority Warnings が 4件、Medium Priority Warnings が 12件の合計 16件の警告が出ています。
出力された警告を解消する
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 の以下の画像の赤線で囲んだところと、
src/main/java/ksbysample/webapp/lending/web/lendingapproval/LendingapprovalFormValidator.java の以下の画像の赤線で囲んだところでした。
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 を削除します。
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=却下理由が入力されていません。
を追加します。
null になっている可能性があるメソッドの戻り値を利用しています。(Bug type NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE)
3つのソースで警告が出ており、1つ目は src/main/java/ksbysample/webapp/lending/aspect/logging/RequestAndResponseLogger.java の以下の画像の赤線で囲んだところでした。
ここは Optional を使って以下のように変更します。
2つ目は src/main/java/ksbysample/webapp/lending/service/calilapi/CalilApiService.java の以下の画像の赤線で囲んだところでした。
org.springframework.http.HttpEntity@Nullable
アノテーションが付いていて、null の場合がありうるため警告が出ていました。
response.getBody()
を変数に取得して null チェックをするように修正します。
src/main/resources/messages_ja_JP.properties に CalilApiService.checkapi.response.emptybody=蔵書検索APIのレスポンスボディが空です。
のメッセージを追加しています。
3つ目は src/main/java/ksbysample/webapp/lending/service/openweathermapapi/OpenWeatherMapApiService.java の以下の画像の赤線で囲んだところでした。
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
以下の画像の赤線で囲んだところでした。
override 元の org.springframework.validation.Validator#validate では引数 target に @Nullable
アノテーションが付いていますが、override した後では NonNull の想定で実装していて矛盾があるという警告でした。
Assert.notNull(target, "target must not be null");
を追加して null の場合には例外が throw されるようにします。
再度 clean タスク → Rebuild Project → build タスクを実行する
再度 clean タスク → Rebuild Project → build タスクを実行してみると spotbugsMain タスクで何も出力されなくなりました。
次回は。。。
いろいろ対応したので一旦 Tomcat を起動して画面が表示できるか確認してみたのですが、画面が正常に表示されませんでした。CSS が適用されていないようです。原因を調査することにします。
履歴
2018/09/29
初版発行。