Sonar扫描的NPE问题

直到我修改为这样的代码才通过——


public class DetectorImport {
    public String check1(Nonentity nonentity) {
        String s;
        if(nonentity == null) {
            s = null;
        }else {
            s = nonentity.getName();
        }
        if(s !=null) {
            s = s.replaceAll("(", "(");
        }
        return s;
    }
}

查到的几个相关问题——
https://community.sonarsource.com/tags/c/bug/fp/7/java

Sonarqube is raising false-positive NPE

2019-07-19

I expect that nonNull implementation is in the another file than the main code. Unfortunately that’s the current limitation we have for this rule. If you move nonNull in the file, FP should disappear.
This problem can’t be fixed easily, so for now I suggest you to mark issue as FP in SonarQube UI.
Anyway thanks for reporting it.

squid:S2259 : A “NullPointerException” could be thrown; “dc” is nullable here. While “dc” is checked as not null

[JAVA] squid:S2259 False Positive with Utility methods
这里提到是支持apache commen的StringUtils包的。

We currently support methods from commons-lang StringUtils (v2, and v3), guava preconditions, and java 8 methods from java.util.Objects (nonNull, isNull, requireNonNull). As we know how these methods behave, we are able to correctly handle such call and discard similar FPs. Of course, I don't want to force you using such libraries to make the analyzer happy. :)

report this on Sonar Community: Commons-lang StringUtils isNotBlank method still raise NPE

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容