我将 Findbugs 与注释 javax.annotation.Nonnull 一起使用。在下面的方法中
@Nonnull
public String methodA(@Nonnull String foo, @Nonnull Integer bar) {
// missing checkNotNull(foo)
// missing checkNotNull(bar)
int fooLen = foo.length(); // did not check that foo is non-null
return (bar < fooLen)? foo : null; // did not check that bar is non-null
}
参数 foo 和 bar 被声明为非空,并且随后在不首先检查它们是否为空的情况下取消引用它们。 当我针对代码运行 Findbugs(使用 Gradle Findbugs 插件)时,Findbugs 报告不包含预期的警告 NP_ARGUMENT_MIGHT_BE_NULL。从 Findbugs 网站,NP_ARGUMENT_MIGHT_BE_NULL 的描述:
NP: Method does not check for null argument (NP_ARGUMENT_MIGHT_BE_NULL)
A parameter to this method has been identified as a value that should always be checked to see whether or not it is null, but it is being dereferenced without a preceding null check.
我做错了什么?
最佳答案
您已告诉 findbugs foo
和 bar
是 @Nonnull
。这告诉 findbugs 查看 methodA
的调用,以了解参数是否为 null
的可能性,如果发现它们,则拒绝这些参数。
在该方法中,正是因为它们用 @Nonnull
进行注释,findbugs 假定它们不能为 null
,因此允许您直接使用它们。
您可能需要使用 @CheckForNull
注释参数以实现您期望的行为。
关于java - Findbugs NP_ARGUMENT_MIGHT_BE_NULL 未按预期触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37357653/