java - Findbugs NP_ARGUMENT_MIGHT_BE_NULL 未按预期触发

标签 java findbugs

我将 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 foobar@Nonnull。这告诉 findbugs 查看 methodA 的调用,以了解参数是否为 null 的可能性,如果发现它们,则拒绝这些参数。

在该方法中,正是因为它们用 @Nonnull 进行注释,findbugs 假定它们不能为 null,因此允许您直接使用它们。

您可能需要使用 @CheckForNull 注释参数以实现您期望的行为。

关于java - Findbugs NP_ARGUMENT_MIGHT_BE_NULL 未按预期触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37357653/

相关文章:

java - 为什么我的 Bean 在 Junit 测试中不符合 Autowire Candidate 资格?

java - 如何修复 FindBugs 的 Naked notify 警告?

java - Findbugs "Method ignores exceptional return value"java.io.File.mkdirs()

java - 查找错误 SIO_SUPERFLUOUS_INSTANCEOF

sonarqube - 如何在 Sonar 扫描期间获取已修复问题的列表

ant - java.lang.ClassNotFoundException : edu. umd.cs.findbugs.FindBugs2

java - 创建 Java 类的新对象返回空对象

java - 如何在java中使用Xmlreader读取XML?

java - 我如何在 gvim 中交换 C/C++ 赋值语句的左侧和右侧?

java - 如何在 Eclipse 中手动安装 EGit 插件