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;
}