Chapter 10. Annotations
Prev |  | Next

# Chapter 10. Annotations

FindBugs supports several annotations to express the developer's intent so that FindBugs can issue warnings more appropriately. You need to use Java 5 to use annotations, and must place the annotations.jar and jsr305.jar files in the classpath while compiling your program.

edu.umd.cs.findbugs.annotations.CheckForNull

  [Target] Field, Method, Parameter

  The annotated element might be null, and uses of the element should check for null. When this annotation is applied to a method it applies to the method return value.

edu.umd.cs.findbugs.annotations.CheckReturnValue

  [Target] Method, Constructor

  [Parameter]

    priority:The priority of the warning (HIGH, MEDIUM, LOW, IGNORE). Default value:MEDIUM.

    explanation:A textual explanation of why the return value should be checked. Default value:"".

  This annotation is used to denote a method whose return value should always be checked after invoking the method.

edu.umd.cs.findbugs.annotations.DefaultAnnotation

  [Target] Type, Package

  [Parameter]

    value:Annotation class objects. More than one class can be specified.

    priority:Default priority(HIGH, MEDIUM, LOW, IGNORE). Default value: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:Annotation class objects. More than one class can be specified.

    priority:Default priority(HIGH, MEDIUM, LOW, IGNORE). Default value:MEDIUM.

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

edu.umd.cs.findbugs.annotations.DefaultAnnotationForMethods

  [Target] Type, Package

  [Parameter]

    value:Annotation class objects. More than one class can be specified.

    priority:Default priority(HIGH, MEDIUM, LOW, IGNORE). Default value:MEDIUM.

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

edu.umd.cs.findbugs.annotations.DefaultAnnotationForParameters

  [Target] Type, Package

  [Parameter]

    value:Annotation class objects. More than one class can be specified.

    priority:Default priority(HIGH, MEDIUM, LOW, IGNORE). Default value:MEDIUM.

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

edu.umd.cs.findbugs.annotations.NonNull

  [Target] Field, Method, Parameter

  The annotated element must not be null. Annotated fields must not be null after construction has completed. Annotated methods must have non-null return values.

edu.umd.cs.findbugs.annotations.Nullable

  [Target] Field, Method, Parameter

  The annotated element could be null under some circumstances. In general, this means developers will have to read the documentation to determine when a null value is acceptable and whether it is necessary to check for a null value. FindBugs will treat the annotated items as though they had no annotation.

  In practice 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. Default value:"".

  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 also supports the following annotations:

- net.jcip.annotations.GuardedBy

- net.jcip.annotations.Immutable

- net.jcip.annotations.NotThreadSafe

- net.jcip.annotations.ThreadSafe

You can refer the JCIP annotation API documentation at Java Concurrency in Practice.

Prev |  | Next
Chapter 9. Analysis Properties | Home | Chapter 11. Using rejarForAnalysis