FindBug 未从 javax.annotation.Nullable 方法中检测到 null

FindBug does not detect null from javax.annotation.Nullable method

我不确定是否应该,但 FindBug 没有检测到错误:

public @Nullable String getNull() {
    return null;
}
public @Nonnull String getNotNull() {
    return getNull();
}
public void doSomething() {
    getNull().length();
}

它只检测:

public @Nonnull String getNotNull() {
    return null;
}

但是这种情况不如检查 @Nonnull return 方法契约那么有用...

我使用:

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

和Gradle:

apply plugin: 'findbugs'

Section 3.7.2 of the Checker Reference Manual 解释了这种(不良)行为:

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.