Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can apply <code>@CheckForNull</code> to all method return values (and/or parameters) within a package by adding the annotation to the package's <code>package-info.java</code> file, but you won't have control over individual methods.</p> <p>First, create <code>@ReturnValuesAreCheckForNullByDefault</code> in a utility package of your project.</p> <pre><code>@Documented @CheckForNull @TypeQualifierDefault(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface ReturnValuesAreCheckForNullByDefault { /* noop */ } </code></pre> <p>Next, create <code>src/java/util/package-info.java</code>.</p> <pre><code>@ReturnValuesAreCheckForNullByDefault package java.util; import my.project.util.ReturnValuesAreCheckForNullByDefault; </code></pre> <p>Finally, enjoy your FindBugs warnings.</p> <pre><code>@Nonnull public String getValue() { Map&lt;String, String&gt; values = new HashMap&lt;&gt;(); return values.get("foo"); // &lt;-- Possible null pointer dereference ... } </code></pre> <p>The problem with doing this is that there are many methods in the <code>java.*</code> packages that contractually <em>do not</em> return <code>null</code>. Using these without checking for <code>null</code> will raise warnings. For example, this NPE-safe code also raises a warning:</p> <pre><code>@Nonnull public Set&lt;String&gt; getNotNull() { Map&lt;String, String&gt; values = new HashMap&lt;&gt;(); return values.keySet(); } </code></pre> <p>You can suppress the warning with <code>@SuppressFBWarnings</code>, but this may clutter up the code too much for your liking.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload