sonar代码扫描常见问题以及处理方案


)

一. 没有关闭io流

sonar

Use try-with-resources or close this "FileInputStream" in a "finally" clause.

错误示例


FileInputStream fis = null;
byte[] buffer = new byte[1024];
try {
    fis = new FileInputStream(new File("E:\Java文件.txt")); 
    while (fis.read(buffer) > 0) {
        System.out.println(new String(buffer)); 
    }
} catch(Exception e) {
    e.printStackTrace();
}

解决办法

在catch后加上关闭代码
finally{
                if(null != printwriter) {
                    printwriter.close();
                }
            }
使用try-with-resources ,放在try里面
try ( Fil eInputStream fis = new FileInputStream(new File("E:\Java文件.txt"))) {
	out2.println("the text");
} catch (IOException e) {
	e.printStackTrace();
}

注: 要是使用的是idea ,可以使用ctrl + alt + T ,找到try-with-resources 快捷解决

二. 在finally语句块中有return,continue,throw 语句

sonar

Remove this return statement from this finally block.
Remove this continue statement from this finally block.
Remove this throw statement from this finally block.

错误示例

 } finally {
            try {
                fis.close();
            } catch (IOException e) {
                log.error("关闭流出现异常:{}", e);
                throw new Exception(); 
            }
 }

解决办法

 } finally {
          fis.close();
 }

三. 可能存在空指针异常,需要增加空值检测。

sonar

A "NullPointerException" could be thrown; "name" is nullable here

错误示例

String name = user.getName();

解决办法

String name ;
if(user != null ){
name = user.getName();
}

四. 当包含操作状态代码时,不应该忽略文件删除操作的结果

sonar

Do something with the "boolean" value returned by "delete".

错误示例

file.dekete();

解决办法

 if (!file.delete()) {
    // file delete failed; take appropriate action
  }

五. 移除没有用到的包、变量、方法

sonar


//移除没有用到的包、变量、方法
Remove this useless assignment to local variable xxx

错误示例

在这里插入图片描述

解决办法

需要看看这个变量有什么作用,没有作用的可以删除掉

六. 方法的认知复杂性不应太高

sonar

//方法的认知复杂性不应太高

Cognitive Complexity of methods should not be too high

//认知复杂性是衡量一种方法的控制流程理解难度的指标。认知复杂性高的方法难以别的开发人员去维护。
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.

错误示例

在这里插入图片描述

解决办法

Compliant Solution(统一解决方案):

对应这种if-else 过多方法,我们主要目的是要消除 if-else ,每多一个 else 那么就会多一个逻辑分叉,代码的易读性会急速降低,这里收集总结了一些地址,需要的同学可以去看一下:
l如何无痛降低 if else 面条代码复杂度 .

七. BigDecimal的取值方法

sonar


//BigDecimal的取值方法
Use "BigDecimal.valueOf" instead.

错误示例


double d = 1.1;

BigDecimal bd1 = new BigDecimal(d); // Noncompliant; see comment above

BigDecimal bd2 = new BigDecimal(1.1); // Noncompliant; same result

解决办法

double d = 1.1;

BigDecimal bd1 = BigDecimal.valueOf(d);

BigDecimal bd2 = BigDecimal.valueOf(1.1);

八. 更改此条件,以便它不总是评估为“false"

sonar


//更改此条件,以便它不总是评估为“false"
Change this condition so that it does not always evaluate to “false

错误示例

在这里插入图片描述

解决办法

可以把这行删除掉,obj不可能为null if (obj == null) return false; 这样这个bug也没有了。

  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值