If you add @Nonnull
to the parameter declaration, FindBugs will highlight anywhere you're passing a value that isn't checked for null
. If you mark it @CheckForNull
, FindBugs will highlight anywhere in the method that you access it without checking for null
.
Which you do depends on the method's contract: does it tolerate null
or not? Looking at its implementation it does not allow for null
without throwing an unexpected exception. Therefore, test
should be marked @Nonnull
so you can spot incorrect calls.
Update
FindBugs will only check fields, parameters, method return values that are annotated with either @Nonnull
or @CheckForNull
. Anything without an annotation is assumed @Nullable
which tells FindBugs to ignore it.
public boolean saveIrr(@Nonnull TestObject test) { ... }
public void dontCareAboutNull(TestObject value) {
saveIrr(value); // no bug
}
public void mightBeNull(@CheckForNull TestObject value) {
saveIrr(value); // bug
}
For this reason, we apply @Nonnull
to all three types of values at the package level. Any value that needs to allow null
must be annotated with @CheckForNull
. We do not allow the use of @Nullable
except in very few corner cases (e.g. @Autowired
fields which Spring enforces).