SonarBug修复
数据类型
- Sonar提示: Use "BigDecimal.valueOf" instead.
解决方法:使用BigDecimal.valueOf()代替。因为这个方法内部会将参数转换为String,保证精度不丢失。
public static BigDecimal valueOf(double val) {
return new BigDecimal(Double.toString(val));
}
- Sonar提示: Equality tests should not be made with floating point values.
解决方法: 浮点数不应该用==去比较,可能会精度丢失导致不准确。
使用BigDecimal.valueOf().compareTo()比较。如果为零可以直接使用BigDecimal.ZERO。
if (BigDecimal.valueOf(value1).compareTo(BigDecimal.valueOf(value2)) == 0) {
//....
}
如果是double类型,也可以直接用Double.doubleToLongBits()的结果值用==,>,<进行比较,如下:
if(Double.doubleToLongBits(d1) == Double.doubleToLongBits(d2))){
//
}
- Sonar提示: Cast one of the operands of this subtraction operation to a "double".
解决方法:使用(double)类型转换,再进行运算。
double d = (double)a + (double)b;
- Sonar提示: Cast one of the operands of this multiplication operation to a "long".
解决方法:使用(long)类型转换,或者在数字后面加上L转换类型。
60*1000*60L;
修饰符
- Sonar提示: Make this "public static " field final .
解决方法:添加final,或者改成 protected。
改成protected时,一直要注意在其他类中有没有用到,不然会报错。
如果使用了"public static " 最好加上final,避免多个对象都修改了变量造成错误。
Sonar解释如下:There is no good reason to declare a field "public" and "static" without also declaring it "final". Most of the time this is a kludge to share a state among several objects. But with this approach, any object can do whatever it wants with the shared state, such as setting it to null。
- Sonar提示: Cannot assign a value to final variable ""
解决方法:final修饰的变量不可赋值,去掉final修饰符或赋值语句。
- Sonar提示: SonarLint: Format specifiers should be used instead of string concatenation.
解决方法: 使用格式说明符。
String.format("字符串其他内容 %s 字符串其他内容", data)
方法
- Sonar提示: Return an empty collection instead of null.
解决方法: 返回空集合而不是返回null,空集合不会导致空指针异常。
return Collections.emptyList();
- Sonar提示: Merge this if statement with the enclosing one.
解决方法: 将靠在一起的多个if合并成一个。
实体类(model)
- Sonar提示: This class overrides "equals()" and should therefore also override "hashCode()".
解决方法: 重写equals()必须重写hashCode()。IDEA可以通过Alt+Insert自动生成。
- Sonar提示: 硬编码http地址不应该为sit地址。
Sonar提示: Make sure using this hardcoded IP address is safe here.
解决方法: 把http地址、IP地址写到property之类的配置文件中。
异常
- Sonar提示: Use a logger to log this exception.
解决方法:logger.error("错误提示字符串:",e);
- Sonar提示: Define and throw a dedicated exception instead of using a generic one.
解决方法:不要直接抛Error,RuntimeException/Throwable/Exception这样的通用的异常,使用更具体的异常代替
比较常用的 IllegalArgumentException(不合法参数的异常) 。
其他的还有 IOException等
示例如下:
throw new IllegalArgumentException("invalid argument, please check argument.");
- Sonar提示: Either log or rethrow this exception.
解决方法: logger.error("错误提示字符串:",e);
- Sonar提示: Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here
解决方法: catch InterruptedException 异常后,需要中断该线程。
try {
//...
} catch (InterruptedException e) {
log.error("doSomething InterruptedException error.", e);
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("doSomething error.", e);
}
集合
- Sonar提示: Iterate over the "entrySet" instead of the "keySet".
解决方法: 使用entrySet,然后再通过entrySet获取key。
for (Map.Entry<String, String> entry : map.entrySet()) {
String key=entry.getKey();
String value=entry.getValue();
}
- Sonar提示: SonarLint: Provide the parametrized type for this generic.
Sonar提示: Raw types should not be used.
解决方法: 使用泛型,比如 List后面指定泛型为 String.
List<String> goodsInfoList = new ArrayList<>();
日志
- Sonar提示: "Preconditions" and logging arguments should not require evaluation
解决方法: 直接使用{}格式符,如下所示:
logger.info("日志内容:{}",变量名称);
- Sonar提示:Invoke method(s) only conditionally.
解决方法: 声明变量后再传递变量,而不是传递一个需要计算的表达式。
logger.info("log start dto:{}", JSON.toJSONString(dto));
可以修改为:
String dtoStr = JSON.toJSONString(dto);
logger.info("log start dto:{}", dtoStr);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了