【发布时间】:2022-11-21 13:16:27
【问题描述】:
我从下面的代码中得到 SonarQube 错误:getProducts() 调用中的参数 1 和 2 标记为“javax.annotation.Nonnull”,但可以传递 null。
Summary summaryForOrg = null;
PoolSummary poolSummary = null;
summaryForOrg = getSummary(); //GET Api call
poolSummary = getPoolSummary(); //GET Api call
// I get an error from SonarCube for parameter 1 and 2 below
products = SummaryUtils.getProducts(summaryForOrg, poolSummary);
下面是 getProducts 方法的签名:
公共产品摘要 getProducts( @Nonnull 总结总结, @Nonnull PoolSummary poolSummary, ) { .... }
【问题讨论】:
-
我不确定 Sonar 如何准确分析空值传播,但 1) 看起来它只是可以看到对变量的空值赋值(如果合并变量声明和赋值并在两种情况下丢弃
= null会怎样?); 2) get-methods 是用@Nullable注释的吗? 3)一旦你用@Nonnull注释变量,它会做什么? 4)尝试将变量包装在Objects.requireNonNull中(如果可能,或者更好地分析空检查警告更深入); 5)抑制警告(不得已的办法,不推荐)。