Sonar代码扫描常见的bug

1、Save and re-use this "Random"

"Random" objects should be reused   创建的随机数不安全。
解决方案
1、sonar建议:
private Random rand = SecureRandom.getInstanceStrong(); // SecureRandom is preferred to Random    public void doSomethingCommon() {
  int rValue = this.rand.nextInt();    //...
}
2、SecureRandom random = new SecureRandom(); 

2、Remove this conditional structure or edit its code blocks so that they’re not all the same

例子:

if(i > 0){return i;

}else{

#### return i;

}
删除此条件结构或编辑其代码块,使它们不完全相同。
因为返回的结果都一样,需要去掉筛选分支。

3、The return value of “replace” must be used.

String s = "2,2";
s.replace("2", "3");

需要代替,s字符串并未改变;
或者 直接拼接s.replace("2", "3");

4、“NullPointerException” will be thrown when invoking method “xxxx”

该方法传入的参数为null
需要在该方法里面 ,判断非空

5、Change this condition so that it does not always evaluate to “false”

条件判断后一直返回的结果为false。需要改条件判断。

6、Unlock this lock along all executions paths of this method

需要解锁 lock.unlock();

7、Cast one of the operands of this multiplication operation to a "long"

需要强转类型
例如转成long
(long)Integer.MaxValue;

8、Do something with the “boolean” value retyrbed by "xxx"

解决:

public boolean doSome(){

}

if(!doSome){......

}

如果返回boolean ,需要判断为false的情况;用!()判断 失败情况。

9、Either re-interrupt this method or rethrow the “InterruptedException” thar4t can be caught here.

解决: 

try(){

}catch (InterruptedException e){//抛出InterruptedException   异常需要重新清除线程的中断状态,添加如下

Thread.currentThread().interrupt();

}

10、Use the original value instead

例子:

public int compare(eq22 e) {
		return -this.getCode1().compareTo(this.getCode2());
	}
用了compareTo方法,取反的话,就算是正数取反,结果将是不可靠的。
解决:
public int compare(eq22 e) {
	return   -1 * value;
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕長亭

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值