第10章 アノテーション

FindBugs はいくつかのアノテーションをサポートしています。開発者の意図を明確にすることで、 FindBugs はより的確に警告を発行することができます。アノテーションを使用するためには Java 5 が必要であり、 annotations.jar および jsr305.jar ファイルをコンパイル時のクラスパスに含める必要があります。

edu.umd.cs.findbugs.annotations.CheckForNull
[Target] Field, Method, Parameter

アノテーションをつけた要素は、 null である可能性があります。したがって、当該要素を使用する際は null チェックをするべきです。このアノテーションをメソッドに適用すると、メソッドの戻り値に適用されます。

edu.umd.cs.findbugs.annotations.CheckReturnValue
[Target] Method, Constructor
[Parameter]

priority: 警告の優先度を指定します (HIGH, MEDIUM, LOW, IGNORE) 。デフォルト値 :MEDIUM。

explanation:戻り値をチェックしなけばならない理由をテキストで説明します。デフォルト値 :""。

このアノテーションを使用して、呼出し後に戻り値をチェックすべきメソッドを表すことができます。

edu.umd.cs.findbugs.annotations.DefaultAnnotation
[Target] Type, Package
[Parameter]

value:アノテーションクラスのclassオブジェクト。複数のクラスを指定することができます。

priority:省略時の優先度を指定します (HIGH, MEDIUM, LOW, IGNORE) 。デフォルト値 :MEDIUM。

Indicates that all members of the class or package should be annotated with the default value of the supplied annotation classes. This would be used for behavior annotations such as @NonNull, @CheckForNull, or @CheckReturnValue. In particular, you can use @DefaultAnnotation(NonNull.class) on a class or package, and then use @Nullable only on those parameters, methods or fields that you want to allow to be null.

edu.umd.cs.findbugs.annotations.DefaultAnnotationForFields
[Target] Type, Package
[Parameter]

value:アノテーションクラスのclassオブジェクト。複数のクラスを指定することができます。

priority:省略時の優先度を指定します (HIGH, MEDIUM, LOW, IGNORE) 。デフォルト値 :MEDIUM。

This is same as the DefaultAnnotation except it only applys to fields.

edu.umd.cs.findbugs.annotations.DefaultAnnotationForMethods
[Target] Type, Package
[Parameter]

value:アノテーションクラスのclassオブジェクト。複数のクラスを指定することができます。

priority:省略時の優先度を指定します (HIGH, MEDIUM, LOW, IGNORE) 。デフォルト値 :MEDIUM。

This is same as the DefaultAnnotation except it only applys to methods.

edu.umd.cs.findbugs.annotations.DefaultAnnotationForParameters
[Target] Type, Package
[Parameter]

value:アノテーションクラスのclassオブジェクト。複数のクラスを指定することができます。

priority:省略時の優先度を指定します (HIGH, MEDIUM, LOW, IGNORE) 。デフォルト値 :MEDIUM。

This is same as the DefaultAnnotation except it only applys to method parameters.

edu.umd.cs.findbugs.annotations.NonNull
[Target] Field, Method, Parameter

アノテーションをつけた要素は、 null であってはいけません。アノテーションをつけたフィールドは、構築完了後 null であってはいけません。アノテーションをつけたメソッドは、 null ではない値を戻り値としなければなりません。

edu.umd.cs.findbugs.annotations.Nullable
[Target] Field, Method, Parameter

アノテーションをつけた要素は、 null であってはいけません。In general, this means developers will have to read the documentation to determine when a null value is acceptable and whether it is neccessary to check for a null value. FindBugs will treat the annotated items as though they had no annotation.

In pratice this annotation is useful only for overriding an overarching NonNull annotation.

edu.umd.cs.findbugs.annotations.OverrideMustInvoke
[Target] Method
[Parameter]

value:Specify when the super invocation should be performed (FIRST, ANYTIME, LAST). Default value:ANYTIME.

Used to annotate a method that, if overridden, must (or should) be invoke super in the overriding method. Examples of such methods include finalize() and clone(). The argument to the method indicates when the super invocation should occur: at any time, at the beginning of the overriding method, or at the end of the overriding method. (This anotation is not implmemented in FindBugs as of September 8, 2006).

edu.umd.cs.findbugs.annotations.PossiblyNull

This annotation is deprecated. Use CheckForNull instead.

edu.umd.cs.findbugs.annotations.SuppressWarnings
[Target] Type, Field, Method, Parameter, Constructor, Package
[Parameter]

value:The name of the warning. More than one name can be specified.

justification:Reason why the warning should be ignored. デフォルト値 :""。

The set of warnings that are to be suppressed by the compiler in the annotated element. Duplicate names are permitted. The second and successive occurrences of a name are ignored. The presence of unrecognized warning names is not an error: Compilers must ignore any warning names they do not recognize. They are, however, free to emit a warning if an annotation contains an unrecognized warning name. Compiler vendors should document the warning names they support in conjunction with this annotation type. They are encouraged to cooperate to ensure that the same names work across multiple compilers.

edu.umd.cs.findbugs.annotations.UnknownNullness
[Target] Field, Method, Parameter

Used to indicate that the nullness of the target is unknown, or my vary in unknown ways in subclasses.

edu.umd.cs.findbugs.annotations.UnknownNullness
[Target] Field, Method, Parameter

Used to indicate that the nullness of the target is unknown, or my vary in unknown ways in subclasses.

また、 FindBugs 次に示すアノテーションもサポートしています。 :

Java Concurrency in Practice API ドキュメント を参照してください。