1. 程式人生 > >(轉載)findBugs的一些功能說明

(轉載)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)));             }         } 10. Exception is caught when Exception is not thrown 這個意思比較好理解:就是catch了異常但是try裡並沒有丟擲異常 11. Method ignores exceptional return value 沒有對方法的異常返回值進行檢查 12. Comparison of String objects using == or != This code compares java.lang.String objects for reference equality using the == or != operators. Unless both strings are either constants in a source file, or have been interned using the String.intern() method,  the same string value may be represented by two different String objects. Consider using the equals(Object) method   instead.   從字面意思可以理解String物件進行比較的時候:只有兩種情況可以使用== or !=的,這兩種情況是;在原始檔中是個常數或者是呼叫   String.intern()方法,使用String的規範化表示形式來進行比較,如果不是這兩中情況的話推薦使用.equals(object)方式 13. Method names should start with a lower case letter 這個好理解方法名的第一個字母不能是大寫 14. Non-transient non-serializable instance field in serializable class This Serializable class defines a non-primitive instance field which is neither transient, Serializable,  or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject()   and writeObject() methods.? Objects of this class will not be deserialized correctly if a non-Serializable object    is stored in this field. 這個錯誤的意思是:在可序列化的類中存在不能序列化或者不能暫存的資料