sonarQube常见问题及分析

阻断
1、Close this"FileInputStream" in a "finally" clause.
在finally中关闭FileInputStream,这个最为常见,主要是关闭方式不对,finally代码块中,应该要对每个stream进行单独关闭,而不能统一写在一个try-catch代码中,jdk 7 可以考虑try-resources方式关闭,代码相对优雅。

另外数据库操作的statement和resultRs没有关闭的情况也非常多。这个也是要重点留意的地方。

2、A"NullPointerException" could be thrown; "tom" is nullablehere
空指针,极为讨厌的问题,主要是编码经验缺少的体现。一般的高手在编码过程中,就会第一时间考虑到这类情况,并做相应的处理。解决方式无它,先判断或者先实例化,再访问里面的属性或者成员。


严重
1、Define and throw a dedicated exception instead of using a generic one
定义并抛出一个专用的异常来代替一个通用的异常。

2、Removethis hard-coded password
移除代码里硬编码的密码信息。会有少许的误判的情况,一般是变量包含:PWD或者password,所以如果真的不是硬编码,可以考虑更换变量名称,比如PWD改PW等等。

3、Eitherlog or rethrow this exception
catch异常之后,使用log方式或者throw异常的方式解决。如果业务上真的没有throw或者记录日志的话,可以使用log.debug的方式填充来解决问题。

4、Makethis IP "127.0.0.1" address configurable
将IP弄到配置文件中,不要硬编码到代码里。个人觉得改动稍大!

5、Make this"public static JSAPI" field final
如果你将这个变量设置为public访问方式,同时又是静态Static方式,就要考虑将它设置为final了,因为这个是共享变量,其它类可以随时随地将它设置为别的值。所以如果是只是当前类使用,可以考虑将公开访问方式改为私有。

6、Makethe enclosing method "static" or remove this set
见代码:
public class MyClass {
private static int count = 0;
public void doSomething() {
//...
count++; // Noncompliant
}
}
不要使用非静态方法去更新静态字段,这样很难获得正确的结果,如果有多个类实例和/或多个线程,则很容易导致错误。理想情况下,静态字段仅从同步静态方法中更新。

7、Override"equals(Object obj)" to comply with the contract of the"compareTo(T o)" method
如果重写了compareTo方法,同时也应重写equals方法。

8、Make"body" transient or serializable.
public class Address {
//...
}

public class Person implements Serializable {
private static final long serialVersionUID = 1905122041950251207L;

private String name;
private Address address; // Noncompliant; Address isn't serializable
}


如果person已经序列化,其成员变量Address也进行序列化。不然转化时会有问题。

9 Floating point numbers should not be tested for equality
浮点类型的数字,不要通过==或者!=方式其它类型比较,因为浮点是不精确的,所以在比较时,会进行类型升级升级原则如下:
· 如果运算符任意一方的类型为double,则另一方会转换为double
· 否则,如果运算符任意一方的类型为float,则另一方会转换为float
· 否则,如果运算符任意一方的类型为long,则另一方会转换为long
· 否则,两边都会转换为int

以下的方式得到的结果都是false。
float myNumber = 3.146;
if ( myNumber == 3.146f ) { //Noncompliant. Because of floating point imprecision, this will be false

if ( myNumber != 3.146f ) { //Noncompliant. Because of floating point imprecision, this will be true


if (myNumber < 4 || myNumber > 4) { // Noncompliant; indirect

float zeroFloat = 0.0f;
if (zeroFloat == 0) { // Noncompliant. Computations may end up with a value close but not equal to zero.
}

所以,要比较浮点数是否相等,需要做的事情是:

排除NaN和无穷
在精度范围内进行比较
正确的例子:
public boolean isEqual(double a, double b) {
if (Double.isNaN(a) || Double.isNaN(b) || Double.isInfinite(a) || Double.isInfinite(b)) {
return false;
}
return (a - b) < 0.001d;
}

10、Thiscall to "contains()" may be a performance hot spot if the collectionis large.
如果collection的记录数非常大的话,它的contains方法的时间复杂度是很高的。所以开发过程中要注意这一点。下面的是列表:
· ArrayList
contains
remove
· LinkedList
get
contains
· ConcurrentLinkedQueue
size
contains
· ConcurrentLinkedDeque
size
contains
· CopyOnWriteArrayList
add
contains
remove
· CopyOnWriteArraySet
add
contains
remove

猜你喜欢

转载自www.cnblogs.com/sunyllove/p/9549584.html