Package apps

Class FindBugsCheck


  • public class FindBugsCheck
    extends java.lang.Object
    Check how SpotBugs (formally FindBugs) and annotations interact.

    Note: This deliberately causes SpotBugs warnings! Do not remove or annotate them! Instead, when past its useful point, just comment out the body of the class so as to leave the code examples present.

    Tests Nonnull, Nullable and CheckForNull from javax.annotation annotations.

    This has no main() because it's not expected to run: Many methods will certainly throw a NullPointerException right away. The idea is for SpotBugs to find those in static analysis. This is in java/src, instead of java/test, so that our usual CI infrastructure builds it.

    Annotations are explicitly qualified (instead of using 'import') to make it completely clear which is being used at each point. That makes this the code less readable, so it's not recommended for general use.

    The "ja" prefix means that javax annotations are used. "no" means un-annotated.

    A previous version (Git SHA 4049c5d690) also had "fb" as a prefix for using the edu.umd.cs.findbugs.annotations form of annotations. There were found to work exactly the same as the (preferred) javax.annotation forms, including when intermixed with each other.

    The comments are the warnings thrown (and not thrown) by SpotBugs 4.52

    Summary:

    • Parameter declaration handling:
      • @Nonnull means that references are assumed OK
      • Both @CheckForNull and @Nullable are checked for dereferences
      • Parameters with no annotation are not checked (i.e. acts like @Nonnull, no null checks required before dereferencing)
    • Passing explicit null parameters
      • @Nonnull will flag a passed null
      • @CheckForNull and @Nullable accept a passed null (but previously flagged any dereferences in the method declaration, i.e. that the annotation wasn't OK)
      • No annotation results in a NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS warning from analyzing the body of the method, effectively working like @Nonnull with a different error
    • Parameter passing of return values isn't always checked by SpotBugs. For example, a @CheckForNull return value is accepted for an @Nonnull parameter. Perhaps this will improve with time.
    • Return values are properly checked for @CheckForNull, but not for @Nullable.
      • A @CheckForNull return value is flagged if it's dereferenced.
      • A @Nullable return value is not flagged if it's dereferenced.
      • Return values without annotation are also not flagged when dereferenced.
    Bottom line: When flagging return values, use @CheckForNull.
    See Also:
    CheckerFrameworkCheck
    • Constructor Summary

      Constructors 
      Constructor Description
      FindBugsCheck()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void test()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait