findbugs的校驗點翻譯-Bad+practice(一)

如有轉載,請註明出處:http://blog.csdn.net/yihui823/article/details/6866801

不是直譯,加了自己的理解。有不合適的地方歡迎大家指正。

基於版本:FindBugs version 1.3.9.


AM : Creates an empty jar file entry(AM_CREATES_EMPTY_JAR_FILE_ENTRY)
在putNextEntry()和closeEntry()之間,沒有對jar文件做其他操作。這樣會給jar文件生成一個空的條目。
The code calls putNextEntry(), immediately followed by a call to closeEntry(). This results in an empty JarFile entry. The contents of the entry should be written to the JarFile between the calls to putNextEntry() and closeEntry().


AM : Creates an empty zip file entry(AM_CREATES_EMPTY_ZIP_FILE_ENTRY)
在putNextEntry()和closeEntry()之間,沒有對zip文件做其他操作。這樣會給zip文件生成一個空的條目。
The code calls putNextEntry(), immediately followed by a call to closeEntry(). This results in an empty ZipFile entry. The contents of the entry should be written to the ZipFile between the calls to putNextEntry() and closeEntry().


BC : Equals method should not assume anything about the type of its argument (BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS)
在equals方法中,沒有對參數進行類型匹配判斷。改法很簡單,加上:
if (!(o instanceof [當前的class]) {
return false;
}
The equals(Object o) method shouldn't make any assumptions about the type of o. It should simply return false if o is not the same type as this.


BC : Random object created and used only once(DMI_RANDOM_USED_ONLY_ONCE)
一個java.util.Random對象只使用了一次,生成了一個隨機數就廢棄了。正常的做法,應該把這個對象保存起來,所有需要用到隨機數的地方都調用這一個對象就行了。
This code creates a java.util.Random object, uses it to generate one random number, and then discards the Random object. This produces mediocre quality random numbers and is inefficient. If possible, rewrite the code so that the Random object is created once and saved, and each time a new random number is required invoke a method on the existing Random object to obtain it.


BIT : Check for sign of bitwise operation(BIT_SIGNED_CHECK)
一個判斷語句中,使用了位操作,並且進行了>0的比較。例如:
((event.detail & SWT.SELECTED) > 0)
這個判斷,本意應該是兩個數字的做與操作後還有非0的位數。但是,一個不小心,與操作的結果是個負數,這就是一個bug了。最好用"!="替換">0"
This method compares an expression such as

((event.detail & SWT.SELECTED) > 0)
. Using bit arithmetic and then comparing with the greater than operator can lead to unexpected results (of course depending on the value of SWT.SELECTED). If SWT.SELECTED is a negative number, this is a candidate for a bug. Even when SWT.SELECTED is not negative, it seems good practice to use '!= 0' instead of '> 0'.
Boris Bokowski



CN : Class implements Cloneable but does not define or use clone method(CN_IDIOM)
一個類實現了Cloneable接口,但是沒有聲明或使用到clone方法。因爲clone方法是Object類的方法,所以當前類不去聲明這個方法,不會編譯不通過。但是,clone是需要逐個字段去複製的,所以沒有聲明clone方法是不對的。
Class implements Cloneable but does not define or use the clone method.


CN : clone method does not call super.clone()(CN_IDIOM_NO_SUPER_CALL)
非final的類,定義了clone()方法,卻在方法中沒有調用super.clone()。
看上去,不應該調用super.clone()。如果A是B的父類,那麼B調用super.clone(),則B的clone方法返回的是A的實例,看上去是錯的。
但是,如果所有的clone()方法都調用了super.clone(),則最終調用的是Object.clone(),那就能返回正確的類型。
This non-final class defines a clone() method that does not call super.clone(). If this class ("A") is extended by a subclass ("B"), and the subclass B calls super.clone(), then it is likely that B's clone() method will return an object of type A, which violates the standard contract for clone().

If all clone() methods call super.clone(), then they are guaranteed to use Object.clone(), which always returns an object of the correct type.



CN : Class defines clone() but doesn't implement Cloneable (CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE)
類定義了clone()方法,但是沒有聲明實現Cloneable接口。這個不是個什麼大問題,只是確認一下是不是漏了聲明。
This class defines a clone() method but the class doesn't implement Cloneable. There are some situations in which this is OK (e.g., you want to control how subclasses can clone themselves), but just make sure that this is what you intended.


Co : Abstract class defines covariant compareTo() method(CO_ABSTRACT_SELF)
類定義了一個compareTo()方法,其參數不是Object類型。要正確的實現Comparable接口的compareTo()方法,最好的做法就是,compareTo()方法的參數就是Object類。
This class defines a covariant version of compareTo(). To correctly override the compareTo() method in the Comparable interface, the parameter of compareTo() must have type java.lang.Object.


Co : Covariant compareTo() method defined(CO_SELF_NO_OBJECT)
同上
This class defines a covariant version of compareTo(). To correctly override the compareTo() method in the Comparable interface, the parameter of compareTo() must have type java.lang.Object.


DE : Method might drop exception(DE_MIGHT_DROP)
這個方法可能放棄了異常。正常的做法,異常應該被處理或者通過某種方式被報告,或者再扔給外層。
This method might drop an exception. In general, exceptions should be handled or reported in some way, or they should be thrown out of the method.


DE : Method might ignore exception(DE_MIGHT_IGNORE)
這個方法可能忽略了異常。正常的做法,異常應該被處理或者通過某種方式被報告,或者再扔給外層。
This method might ignore an exception. In general, exceptions should be handled or reported in some way, or they should be thrown out of the method.


DMI : Don't use removeAll to clear a collection(DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION)
如果你想把集合內的所有的元素都刪除掉,請用集合的clear方法,而不是c.removeAll( c )方法。調用c.removeAll( c )去清空集合,會清除的不乾淨,容易產生錯誤,可能會拋出ConcurrentModificationException異常。
If you want to remove all elements from a collection c, use c.clear, not c.removeAll(c). Calling c.removeAll(c) to clear a collection is less clear, susceptible to errors from typos, less efficient and for some collections, might throw a ConcurrentModificationException.


DP : Classloaders should only be created inside doPrivileged block (DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED)
這段代碼寫了一個需要安全管理器的classloader。如果代碼需要被授權爲安全權限,但是可能被不安全的代碼去調用,那麼classloader就需要放在doPrivileged塊內。
This code creates a classloader, which requires a security manager. If this code will be granted security permissions, but might be invoked by code that does not have security permissions, then the classloader creation needs to occur inside a doPrivileged block.


DP : Method invoked that should be only be invoked inside a doPrivileged block (DP_DO_INSIDE_DO_PRIVILEGED)
代碼調用了一個需要安全權限檢查的方法。如果代碼需要被授權爲安全權限,但是可能被不安全的代碼去調用,那麼classloader就需要放在doPrivileged塊內。
This code invokes a method that requires a security permission check. If this code will be granted security permissions, but might be invoked by code that does not have security permissions, then the invocation needs to occur inside a doPrivileged block.


Dm : Method invokes System.exit(...)(DM_EXIT)
調用了System.exit去關閉虛擬機進程。只能在適當的時候這麼用。這種調用會讓你的代碼很難甚至不可能被其他代碼調用。用扔出RuntimeException異常來代替會比較好。
Invoking System.exit shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code. Consider throwing a RuntimeException instead.


Dm : Method invokes dangerous method runFinalizersOnExit(DM_RUN_FINALIZERS_ON_EXIT)
永遠不要以任何理由調用System.runFinalizersOnExit 或者Runtime.runFinalizersOnExit,在java包裏面,他們是非常危險的方法。
-- Java教父Joshua Bloch
Never call System.runFinalizersOnExit or Runtime.runFinalizersOnExit for any reason: they are among the most dangerous methods in the Java libraries. -- Joshua Bloch


ES : Comparison of String parameter using == or !=(ES_COMPARING_PARAMETER_STRING_WITH_EQ)
這段代碼用 == 或者 != 來比較字符串。這種方式去比較字符串,並不是比較字符串的內容相同,而是比較是不是同一個對象。用equals方法來代替這種比較。
This code compares a java.lang.String parameter for reference equality using the == or != operators. Requiring callers to pass only String constants or interned strings to a method is unnecessarily fragile, and rarely leads to measurable performance gains. Consider using the equals(Object) method instead.


ES : Comparison of String objects using == or !=(ES_COMPARING_STRINGS_WITH_EQ)
同上
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.


Eq : Abstract class defines covariant equals() method(EQ_ABSTRACT_SELF)
這個類定義了equals()方法,但是參數卻是Object的子類。正確覆蓋equals()方法,參數必須是Object
This class defines a covariant version of equals(). To correctly override the equals() method in java.lang.Object, the parameter of equals() must have type java.lang.Object.


Eq : Equals checks for noncompatible operand (EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS)
equals方法內,對參數的類型檢查的時候,檢查了除了本身之外的其他類型。如:
public boolean equals(Object o) {
if (o instanceof Foo)
return name.equals(((Foo)o).name);
else if (o instanceof String)
return name.equals(o);
else return false;
這種寫法是不好的習慣,它會讓代碼難以理解和遷移。
This equals method is checking to see if the argument is some incompatible type (i.e., a class that is neither a supertype nor subtype of the class that defines the equals method). For example, the Foo class might have an equals method that looks like:


public boolean equals(Object o) {
if (o instanceof Foo)
return name.equals(((Foo)o).name);
else if (o instanceof String)
return name.equals(o);
else return false;
This is considered bad practice, as it makes it very hard to implement an equals method that is symmetric and transitive. Without those properties, very unexpected behavoirs are possible.



Eq : Class defines compareTo(...) and uses Object.equals()(EQ_COMPARETO_USE_OBJECT_EQUALS)
這個類定義了compareTo(…)方法,但是卻直接繼承Object的equals()方法。通常,compareTo方法在並且只有在equals方法返回ture的時候返回0。如果沒有遵守這個原則,就會出現一些奇怪和不可預測的問題。在java5中,PriorityQueue.remove方法是用了compareTo方法,但是java6中它用的卻是equals方法。
不用多說了。
This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object. Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue. In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.
From the JavaDoc for the compareTo method in the Comparable interface:

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."



Eq : equals method fails for subtypes(EQ_GETCLASS_AND_CLASS_CONSTANT)
這個類寫了自己的equals方法,但是這個方法在比較參數的對象類型的時候被定義的不可繼承,如果有子類繼承了這個類,那麼就會出錯。例如,Foo類的檢查是:
if (Foo.class == o.getClass())。最好改成:
if (this.getClass() == o.getClass())
This class has an equals method that will be broken if it is inherited by subclasses. It compares a class literal with the class of the argument (e.g., in class Foo it might check if Foo.class == o.getClass()). It is better to check if this.getClass() == o.getClass().


Eq : Covariant equals() method defined(EQ_SELF_NO_OBJECT)
equals()方法的參數,最好就是Object。如果不是,會容易出問題。
This class defines a covariant version of equals(). To correctly override the equals() method in java.lang.Object, the parameter of equals() must have type java.lang.Object.



發佈了54 篇原創文章 · 獲贊 19 · 訪問量 97萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章