gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:58:34 28 4
gpt4 key购买 nike

我不确定是否应该,但 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 返回方法契约那么有用...

我使用:

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.

关于java - FindBug 未从 javax.annotation.Nullable 方法中检测到 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526523/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com