かんがるーさんの日記

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

Spring Boot 2.0.x の Web アプリを 2.1.x へバージョンアップする ( その17 )( JDK 11 環境下で error-prone を復活させる )

概要

記事一覧はこちらです。

Spring Boot 2.0.x の Web アプリを 2.1.x へバージョンアップする ( その16 )( JDK を 8u202 → 11.0.2+9 に変更する2 ) の続きです。

参照したサイト・書籍

  1. net.ltgt.errorprone
    https://plugins.gradle.org/plugin/net.ltgt.errorprone

  2. google/error-prone
    https://github.com/google/error-prone

  3. Error Prone
    https://errorprone.info/index

  4. tbroyer/gradle-errorprone-plugin
    https://github.com/tbroyer/gradle-errorprone-plugin

目次

  1. build.gradle を変更する
  2. clean タスク → Rebuild Project → build タスクを実行してみる
  3. 出力された警告を解消する

手順

build.gradle を変更する

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

..........

plugins {
    id "java"
    id "eclipse"
    id "idea"
    id "org.springframework.boot" version "2.1.3.RELEASE"
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
    id "groovy"
    id "checkstyle"
    id "com.github.spotbugs" version "1.6.9"
    id "pmd"
    id "net.ltgt.errorprone" version "0.7.1"
    id "de.undercouch.download" version "3.4.3"
    id "com.gorylenko.gradle-git-properties" version "2.0.0"
}

sourceCompatibility = 11
targetCompatibility = 11

wrapper {
    gradleVersion = "5.2.1"
    distributionType = Wrapper.DistributionType.ALL
}

[compileJava, compileTestGroovy, compileTestJava]*.options*.encoding = "UTF-8"
[compileJava, compileTestGroovy, compileTestJava]*.options*.compilerArgs = ["-Xlint:all,-options,-processing,-path"]

..........

dependencies {
    def jdbcDriver = "org.postgresql:postgresql:42.2.5"
    def spockVersion = "1.2-groovy-2.5"
    def domaVersion = "2.23.0"
    def lombokVersion = "1.18.6"
    def errorproneVersion = "2.3.3"
    def powermockVersion = "2.0.0"
    def spotbugsVersion = "3.1.11"

    ..........

    // for Error Prone ( http://errorprone.info/ )
    errorprone("com.google.errorprone:error_prone_core:${errorproneVersion}")
    compileOnly("com.google.errorprone:error_prone_annotations:${errorproneVersion}")

    ..........
}
  • plugins block の id "net.ltgt.errorprone" ...コメントアウトを解除し、id "net.ltgt.errorprone" version "0.0.16"id "net.ltgt.errorprone" version "0.7.1" に変更します。
  • JDK 11 に上げたので、compile 時のオプションは一旦見直します。以下の行は削除します。
    • , "-Xep:RemoveUnusedImports:WARN"
    • , "-Xep:InsecureCryptoUsage:OFF"
    • , "-Xep:ParameterName:OFF"
  • dependencies block の以下の点を変更します。
    • def errorproneVersion = "2.3.1"def errorproneVersion = "2.3.3" に変更します。
    • 以下の行のコメントアウトを解除します。
      • errorprone("com.google.errorprone:error_prone_core:${errorproneVersion}")
      • compileOnly("com.google.errorprone:error_prone_annotations:${errorproneVersion}")

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

clean タスク → Rebuild Project → build タスクを実行してみる

clean タスク → Rebuild Project → build タスクを実行してみます。

f:id:ksby:20190320231618p:plain f:id:ksby:20190320231725p:plain (.....途中省略.....) f:id:ksby:20190320231825p:plain

compileJava タスクで警告5個、compileTestJava タスクで警告31個が出ました。

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

compileJava タスクで出た警告5個は以下のものでした。対応方法も記載します。

  • 警告:[UnusedMethod] Private method 'pointcutServiceMethod' is never used.
  • 警告:[UnusedMethod] Private method 'pointcutControllerMethod' is never used.
    • 上の2個は Spring AOP の Pointcut の定義をするための private method でした。どちらも PMD でチェック対象外になるよう @SuppressWarnings({"PMD.UnusedPrivateMethod"}) を付与していますが error-prone のチェックも @SuppressWarnings でチェックの対象外に指定できるので@SuppressWarnings({"PMD.UnusedPrivateMethod"})@SuppressWarnings({"PMD.UnusedPrivateMethod", "UnusedMethod"}) に変更してチェックされないようにします。
  • 警告:[JavaTimeDefaultTimeZone] LocalDateTime.now() is not allowed because it silently uses the system default time-zone. You must pass an explicit time-zone (e.g., ZoneId.of("America/Los_Angeles")) to this method.
  • 警告:[JavaTimeDefaultTimeZone] LocalDateTime.now() is not allowed because it silently uses the system default time-zone. You must pass an explicit time-zone (e.g., ZoneId.of("America/Los_Angeles")) to this method.
    • これまで LocalDateTime.now() のサンプルしか見たことがなかったのですが、マシンや JVM の設定に依存してしまうので LocalDateTime.now(ZoneId.of("Asia/Tokyo")) のように明示的に time-zone を指定するようにとのこと(JavaTimeDefaultTimeZone)。これはこの指摘通りに対応します。
  • 警告:[UnusedVariable] The local variable 'lendingBookList' is never read.
    • レコードをロックするために List<LendingBook> lendingBookList = lendingBookDao.selectByLendingAppId(lendingAppId, SelectOptions.get().forUpdate()) と呼び出していたのですが、その後の処理で lendingBookList を使用していないので警告が出ていました。警告が出たメソッドに @SuppressWarnings("UnusedVariable") を付けてチェックされないようにします。

compileTestJava タスクで出た警告31個は以下のものでした。

  • 警告:[Finally] If you return or throw from a finally, then values returned or thrown from the try-catch block will be ignored. Consider using try-with-resources instead. が1個。
  • 警告:[UnusedVariable] The local variable '.....' is never read. が5個。
  • 警告:[ClassCanBeStatic] Inner class is non-static but does not reference enclosing class が25個。

compileTestJava タスクまで error-prone でチェックする必要はないので、build.gradle に以下の tasks.named("compileTestJava").configure { ... } の設定を追加してチェックを実行しないようにします。

[compileJava, compileTestGroovy, compileTestJava]*.options*.encoding = "UTF-8"
[compileJava, compileTestGroovy, compileTestJava]*.options*.compilerArgs = ["-Xlint:all,-options,-processing,-path"]
tasks.named("compileTestJava").configure {
    options.errorprone.enabled = false
}

再度 clean タスク → Rebuild Project → build タスクを実行すると、何の警告も出なくなりました。

f:id:ksby:20190321080527p:plain

JDK 11 になると error-prone の設定が楽になりました。また LocalDateTime.now(ZoneId.of("Asia/Tokyo")) の警告は error-prone を入れないと絶対分かりませんね。そんなサンプルを見たことがありません(今でも本当に ZoneId.of を書く必要があるのかと疑問に思っています)。

履歴

2019/03/21
初版発行。