gpt4 book ai didi

java - 尽管处理了这种可能性,sonarLint 还是提示 "Null pointers should not be dereferenced (squid:S2259)"

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:06:04 27 4
gpt4 key购买 nike

所以我对 SonarLint 有一个问题,我不确定如何处理。

假设我有一个带有方法的类

public class Class(RemoteContext context)
RemoteContext context = context;

public void String method(String data) {
if(data == null)
context.raiseException("data can't be null");

//do stuff with data like data.get();
}

当我用 sonarLint (3.2.) 分析这个类时,我得到一个 Null pointer should not be dereferenced 问题。

所以我的问题是。如何解决这个问题?context.RaiseException 将停止方法执行,所以我认为这是误报。

应用程序有很多案例(类/方法)存在这个问题。所以我认为注释是一种矫枉过正(到处都是丑陋的代码)我也可以在每次 raiseException() 调用后键入 return,但我的印象是这不是“程序员的方式”。

我猜最好是编写自己的规则。

我正在查看主题并四处搜索,但没有发现任何对这种情况有用的东西,当我不得不做与 Sonar 实际做的“相反”的事情时。不是提出问题,而是对方法“开绿灯”?

希望我对这个问题足够清楚。

最佳答案

如果 RemoteContext 是您控制的类,并且您真的不想使用通常的 new ExceptionType(...) 模式,我会更改 RemoteContext 构建 异常但不抛出,然后

if (data == null) {
throw context.buildException("data can't be null");
}

...因此对于 SonarLint、Java 编译器和稍后处理代码的程序员来说,方法的执行在该点停止(因为“引发异常”可能意味着很多事情)。

(是的,这意味着要更改很多地方,但通过相对简单的搜索和替换即可实现。)

关于java - 尽管处理了这种可能性,sonarLint 还是提示 "Null pointers should not be dereferenced (squid:S2259)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534436/

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