日期:2014-05-16  浏览次数:20345 次

findbugs的一些错误说明

1. equals比较不同的对象类型
Call to equals() comparing different types
This method calls equals(Object) on two references of different class types with no common subclasses. Therefore, the objects being compared are unlikely to be members of the same class at runtime (unless some application classes were not analyzed, or dynamic class loading can occur at runtime). According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.
说的是equals要比较相同的对象类型

2,可能产生空指针异常
Possible null pointer dereference
A reference value dereferenced here might be null at runtime.? This may lead to a NullPointerException when the code is executed.

3.从未使用的本地变量
Dead store to local variable
This instruction assigns a value to a local variable, but the value is not read by any subsequent instruction. Often, this indicates an error, because the value computed is never used.
Note that Sun's javac compiler often generates dead stores for final local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives.

4.应该是一个静态内部类
Should be a static inner class

This class is an inner class, but does not use its embedded reference to the object which created it.? This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.? If possible, the class should be made static.

5.方法名称第一个字母小写
Method names should start with an lower case letter

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.


6.用包装类的valueOf代替NEW
解释:因为用new Integer(int) 这样的方式会产生一个新的对象
而当编译时用valueOf则会被缓存,并且速度更快。
Method invokes inefficient Number constructor; use static valueOf instead

Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.

?

?

7.Dead store to local variable 本地变量存储了闲置不用的对象
举例:
List accountCoList = new ArrayList();
我们为accountCoList新建了一个对象,但是程序的后面并没有使用这个这个新建对象。
建议改为:
List accountCoList = null;

?

8.Write to static field from instance method 向static字段中写入值
举例:
?private static DBRBO dbrBO;
?public final void refresh() {
??????? danskeBankBO = null;
??????? dbrBO = null;
??????? fileAndPathBO = null;
??? }
建议改为:
去掉static。


9. Load of known null value 大体意思是加载了null的对象。
举例
??????? if (null == boList) {

??????????? for (int i = 0; i < boList.size(); i++) {
??????????????? entityList.add(productBOToEntity(boList.get(i)));
??????????? }
??????? }

?

?15.Inefficient use of keySet iterator instead of entrySet iterator
这个意思是说请用遍历Entry的方式替换keySet,因为前者比后者效率高。
很多人都这样遍历Map,没错,但是效率很低,先一个一个的把key遍历,然后在根据key去查找value,这不是多此一举么,为什么不遍历entry(桶)然后直接从entry得到value呢?它们的执行效率大概为1.5:1(有人实际测试过)。

?

16.Method call passes null for nonnull parameter

??? 对参数为null的情况没做处理

??? 例

?

public void method1() {
???String ip = null;
??try {
???ip = InetAddress.getLocalHost().