Reputation: 48873
I am not sure if it should but FindBug does not detect error for:
public @Nullable String getNull() {
return null;
}
public @Nonnull String getNotNull() {
return getNull();
}
public void doSomething() {
getNull().length();
}
It only detects:
public @Nonnull String getNotNull() {
return null;
}
but this case is not so helpful as checking for @Nonnull
return method contract...
I use:
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
and Gradle:
apply plugin: 'findbugs'
Upvotes: 1
Views: 1039
Reputation: 17363
Section 3.7.2 of the Checker Reference Manual explains this (undesirable) behavior:
3.7.2 Incompatibility note about FindBugs @Nullable
FindBugs has a non-standard definition of @Nullable. FindBugs’s treatment is not documented in its own Javadoc; it is different from the definition of @Nullable in every other tool for nullness analysis; it means the same thing as @NonNull when applied to a formal parameter; and it invariably surprises programmers. Thus, FindBugs’s @Nullable is detrimental rather than useful as documentation. In practice, your best bet is to not rely on FindBugs for nullness analysis, even if you find FindBugs useful for other purposes...
FindBugs suppresses all warnings at uses of a @Nullable variable. (You have to use @CheckForNull to indicate a nullable variable that FindBugs should check.)...
// declare getObject() to possibly return null
@Nullable Object getObject() { ... }
void myMethod() {
@Nullable Object o = getObject();
// FindBugs issues no warning about calling toString on a possibly-null reference!
o.toString();
}
...With FindBugs, you annotate a declaration, which suppresses checking at all client uses, even the places that you want to check.
Upvotes: 2