Checkstyle Javaルール

CheckstyleのJavaルールについて1つずつまとめます

AnnotationUseStyle

CheckStyle公式ドキュメント

検証環境

Checkstyleバージョン:10.3.3
Javaバージョン:17


チェック概要

チェック追加バージョン
Checkstyle 5.0

アノテーションの属性のスタイルをチェックする。
スタイルの設定に関わるオプションには以下の3つが存在する。

  • ElementStyleOption
  • ClosingParensOption
  • TrailingArrayCommaOption

ElementStyleOption

アノテーションの属性の記述方法を定義するためのスタイルのポリシー。

説明
expanded アノテーションの属性を名前付きパラメータで記述する
compact 属性がvalueのみ、もしくはvalue以外の全ての属性がデフォルト値を持つ場合にのみ使用可能で、属性名を省略して記述する
compact_no_array compactと似ているが、属性に要素が1つのみの配列を記述する場合に{}を記述しない
ignore アノテーションの属性の記述方法はチェックしない
// expanded
// OK
@SuppressWarnings(value = {"unchecked","unused",})

// NG:属性名を省略しない
@SuppressWarnings({"unchecked","unused",})

// compact
// OK
@SuppressWarnings({"unchecked","unused",})
@SuppressWarnings("unchecked")

// NG:属性名がvalueのもののみ指定する場合は、属性名を省略する
@SuppressWarnings(value = {"unchecked","unused",})
@SuppressWarnings(value = "unchecked")

// compact_no_array
// OK
@SuppressWarnings("unchecked")
@MyAnnotation(someArray = "some value")

// NG:属性に指定する配列の要素数が1の場合に{}を記述しない
@SuppressWarnings({"unchecked"})
@MyAnnotation(someArray = {"some value"})

ClosingParensOption

アノテーションの終了括弧のスタイルに関するポリシー。

説明
always 属性値の有無に関わらず常に括弧を記述する
例:@Deprecated()
never 属性値を設定しない場合は括弧を記述しない
例:@Deprecated
ignore アノテーションの括弧についてチェックを行わない

TrailingArrayCommaOption

配列の末尾のコンマのスタイルに関するポリシー。

説明
always 配列の要素の末尾に常にコンマを記述する
例:@SuppressWarnings(value={"unchecked","unused",})
never 配列の要素の末尾にコンマを記述しない
例:@SuppressWarnings(value={"unchecked","unused"})
ignore 配列の末尾のコンマの有無をチェックしない

プロパティ

プロパティ デフォルト値 説明 追加バージョン
elementStyle ElementStyleOption compact_no_array アノテーションの属性の記述方法のスタイル 5.0
closingParens ClosingParensOption never 終了括弧に関するポリシー 5.0
trailingArrayComma TrailingArrayCommaOption never 配列の末尾のコンマに関するポリシー 5.0

設定+チェック実行結果

プロパティ設定なし

設定ファイル記述方法

<module name="Checker">
    <module name="TreeWalker">
        <module name="AnnotationUseStyle"/>
    </module>
</module>

チェック実行例

public class MyClass {
    // OK
    @Deprecated    
    // NG 配列の要素が1つだけなので{}は不要
    @SomeArrays(pooches = { DOGS.LEO })
    // NG 配列の要素が1つだけなので{}は不要
    @SuppressWarnings({ "" })
    public class TestOne {
    }

    // NG  配列の要素が1つだけなので{}は不要
    @SomeArrays(pooches = { DOGS.LEO }, um = {}, test = { "bleh" })
    // OK
    @SuppressWarnings("")
    // NG 括弧不要
    @Deprecated()
    class TestTwo {
    }
}

プロパティ設定あり

elementStyle

アノテーションの属性の記述方法のスタイルを設定する。

設定ファイル記述方法

<module name="Checker">
    <module name="TreeWalker">
        <module name = "AnnotationUseStyle">
            <property name="elementStyle" value="expanded"/>
        </module>
    </module>
</module>

チェック実行例

elementStyleに「expanded」を設定しているので、属性名は省略しない。

public class MyClass {
    // OK
    @Deprecated    
    // OK
    @SomeArrays(pooches = { DOGS.LEO })
    // NG 属性名は省略しない
    @SuppressWarnings({ "" })
    public class TestOne {
    }

    // OK
    @SomeArrays(pooches = { DOGS.LEO }, um = {}, test = { "bleh" })
    // NG 属性名は省略しない
    @SuppressWarnings("")
    // NG 括弧不要
    @Deprecated()
    class TestTwo {
    }
}

trailingArrayComma

終了括弧に関するポリシーを設定する。

設定ファイル記述方法

<module name="Checker">
    <module name="TreeWalker">
        <module name = "AnnotationUseStyle">
            <property name="trailingArrayComma" value="always"/>
        </module>
    </module>
</module>

チェック実行例

trailingArrayCommaに「always」を設定しているので、終了括弧を常に記述する。

public class MyClass {
    // NG 括弧を記述する
    @Deprecated    
    // NG 配列の要素が1つだけなので{}は不要
    @SomeArrays(pooches = { DOGS.LEO })
    // NG 配列の要素が1つだけなので{}は不要
    @SuppressWarnings({ "" })
    public class TestOne {
    }

    // NG  配列の要素が1つだけなので{}は不要
    @SomeArrays(pooches = { DOGS.LEO }, um = {}, test = { "bleh" })
    // OK
    @SuppressWarnings("")
    // OK
    @Deprecated()
    class TestTwo {
    }
}

closingParens

配列の末尾のコンマに関するポリシーを設定する。

設定ファイル記述方法

<module name="Checker">
    <module name="TreeWalker">
        <module name = "AnnotationUseStyle">
            <property name="closingParens" value="always"/>
        </module>
    </module>
</module>

チェック実行例

closingParensに「always」を設定しているので要素の末尾にコンマを記載する。

public class MyClass {
    // OK
    @Deprecated    
    // NG 配列の要素の末尾にコンマを記載する
    @SomeArrays(pooches = { DOGS.LEO })
    // NG 配列の要素の末尾にコンマを記載する
    @SuppressWarnings({ "" })
    public class TestOne {
    }

    // NG  配列の要素の末尾にコンマを記載する
    @SomeArrays(pooches = { DOGS.LEO }, um = {}, test = { "bleh" })
    // OK
    @SuppressWarnings("")
    // NG 括弧不要
    @Deprecated()
    class TestTwo {
    }
}