FindBugs工具常见问题
1,AM: Creates an empty jar file entry (AM_CREATES_EMPTY_JAR_FILE_ENTRY)/AM: Creates an empty zip file entry (AM_CREATES_EMPTY_ZIP_FILE_ENTRY)
示例代碼:
ZipEntry entry = new ZipEntry(PATH);
zos.putNextEntry(entry);
zos.closeEntry();
原因:
代碼中在調(diào)用putNextEntry()之后緊接著調(diào)用了closeEntry()函數(shù),致使該jar文件內(nèi)容為空,因?yàn)榇騤ar包的寫(xiě)內(nèi)容是在putNextEntry()和closeEntry()兩個(gè)函數(shù)調(diào)用之間來(lái)進(jìn)行的。(有時(shí)候也許會(huì)有意的構(gòu)建一個(gè)空目錄,因此不一定就是bug)
?
2,BC: Equals method should not assume anything about the type of its argument (BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS)
?
示例代碼:
public class Foo {
???// some code
???public void equals(Object o) {
?????Foo other = (Foo) o;
????// the real equals code
??}
}
原因:
當(dāng)你在實(shí)現(xiàn)類(lèi)的equals方法時(shí),不應(yīng)該對(duì)參數(shù)有任何的預(yù)先設(shè)定。如上代碼所寫(xiě),則設(shè)定了參數(shù)o肯定是Foo類(lèi)的一個(gè)對(duì)象.但是如果在函數(shù)調(diào)用時(shí),參數(shù)o不是一個(gè)Foo類(lèi)或其子類(lèi),就會(huì)導(dǎo)致代碼會(huì)拋出一個(gè)ClassCastException。因此在實(shí)現(xiàn)equals方法,應(yīng)該加一個(gè)判斷,如果參數(shù)o不是一個(gè)Foo類(lèi)對(duì)象,則返回false。
?
3,BC: Random object created and used only once (DMI_RANDOM_USED_ONLY_ONCE)
示例代碼:
public int?getRandom(int?seed)?{
????return new?Random(seed).nextInt();
}
原因:
由于java.util.Random是一個(gè)偽隨機(jī)函數(shù),如果傳入的seed值相同的話,返回的隨機(jī)數(shù)者是相同的?。因此沒(méi)必要每次都new一個(gè)新的random出來(lái)計(jì)算隨機(jī)數(shù)。如果你想真正地獲得一個(gè)不可預(yù)知的隨機(jī)數(shù),建議使用java.security.SecureRandom,該類(lèi)繼承自Random,是一個(gè)強(qiáng)隨機(jī)數(shù)生成器?。因此上述代碼可以修改為:
public?class?Test??extends?Thread{
????private?SecureRandom?ran;
?
????Test(int?seed){
????????ran?=?new?SecureRandom();
????}
????
????public?int?getRandom(int?seed)?{
????????return?ran.nextInt();
????}
}
?
4,CN: Class implements Cloneable but does not define or use clone method (CN_IDIOM)
示例代碼:
public class Foo implements Cloneable {
?????????public Object clone() throws CloneNotSupportedException {
???????????????????return super.clone();
?????????}
}
原因:
類(lèi)定義要實(shí)現(xiàn)了?Cloneable接口,卻沒(méi)有定義或使用?clone方法,即缺少紅色字體部分。
?
5,CN: clone method does not call super.clone() (CN_IDIOM_NO_SUPER_CALL)
示例代碼:
public class Foo implements Cloneable {
?????????public Object clone() throws CloneNotSupportedException {
???????????????????return super.clone();
?????????}
}
原因:
clone方法沒(méi)有調(diào)用super.clone()方法,如果沒(méi)有調(diào)用,則會(huì)導(dǎo)致對(duì)象父子層級(jí)關(guān)系不能正確建立,最終導(dǎo)致無(wú)法正確組裝對(duì)象。
?
6,CN: Class defines clone() but doesn't implement Cloneable (CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE)
示例代碼:
public class Foo{
?????????public Object clone() throws CloneNotSupportedException {
???????????????????return super.clone();
?????????}
}
原因:
這個(gè)用法的意義在于你可以規(guī)范該類(lèi)的子類(lèi)的clone的實(shí)現(xiàn),如果你的確想這樣做的話,這不是一個(gè)bug,否則的話是一個(gè)bug
?
7,DE: Method might drop exception (DE_MIGHT_DROP)/DE: Method might ignore exception (DE_MIGHT_IGNORE)
?
示例代碼:
try{}catch(Exception ex){}
原因:
方法有可能拋異常或者忽略異常,需要對(duì)異常進(jìn)行處理,即需要在catch體中對(duì)異常進(jìn)行處理。
?
8,DMI: Don't use removeAll to clear a collection (DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION)
原因:
建議不要使用?collection.removeAll(collection)方法來(lái)刪除?collection中的所有元素,而使用collection.clear()。比較二者的代碼實(shí)現(xiàn)就可以看出:
removeAll()源碼:
????public boolean removeAll(Collection<?> c) {
??????????????boolean modified = false;
??????????????Iterator<?> e = iterator();
??????????????while (e.hasNext()) {
????????????????????if (c.contains(e.next())) {
?????????????????????????????????e.remove();
?????????????????????????????????modified = true;
??????????????????}
??????????????}
??????????????return modified;
????}
clear()源碼:
????public void clear() {
??????????????Iterator<E> e = iterator();
??????????????while (e.hasNext()) {
??????????????????e.next();
??????????????????e.remove();
??????????????}
????}
前者是比較參數(shù)中的collection和要移除元素的collection中是否有交集,然后將交集元素刪除;后者是直接將collenction中的元素刪除。顯然后者要比前者高效,而且對(duì)于某些特殊的collenction還容易拋出一些異常,如ConcurrentModificationException
9,ES: Comparison of String parameter using == or != (ES_COMPARING_PARAMETER_STRING_WITH_EQ)
原因:當(dāng)比較兩個(gè)字符串內(nèi)容是否相同時(shí),僅當(dāng)兩個(gè)字符串在源文件中都是常量時(shí)或者是使用intern()來(lái)比較才可以用==來(lái)比較,否則最好使用對(duì)象比較方法equal。附string比較:
String str1 = "java";
String str2 = "java";
System.out.print(str1==str2);
結(jié)果:true(二者都為常量)
String str1 = new String("java");
String str2 = new String("java");
System.out.print(str1==str2);
結(jié)果:false(二者為對(duì)象)
String str1 = "java";
String str2 = "blog";
String s = str1+str2;
System.out.print(s=="javablog");
結(jié)果:false(s不為常量,為對(duì)象)
String s1 = "java";
String s2 = new String("java");
System.out.print(s1.intern()==s2.intern());
結(jié)果:true(但是intern()方法在效率和實(shí)現(xiàn)方式上不統(tǒng)一)
轉(zhuǎn)載于:https://www.cnblogs.com/happyPawpaw/p/4227697.html
總結(jié)
以上是生活随笔為你收集整理的FindBugs工具常见问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用zlib库进行数据压缩
- 下一篇: Tomcat数据源总结