目前已轉(zhuǎn)至個人博客痹筛,本系列地址:Lam's Blog - Knowledge as Action
BX_BOXING_IMMEDIATELY_UNBOXED
Primitive value is boxed and then immediately unboxed
對原始值進(jìn)行裝箱横朋,然后立即取消裝箱适肠。這可能是在一個未要求裝箱的地方進(jìn)行了手動裝箱,從而迫使編譯器進(jìn)行立即撤消裝箱的操作
BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION
Primitive value is boxed then unboxed to perform primitive coercion
對原始值進(jìn)行裝箱然后立即把它強(qiáng)制轉(zhuǎn)換為另外一種原始類型屎慢。例如:
new Double(d).intValue()
應(yīng)該直接進(jìn)行強(qiáng)制轉(zhuǎn)換例如:(int) d
DM_BOXED_PRIMITIVE_TOSTRING
Method allocates a boxed primitive just to call toString
僅僅為了調(diào)用封裝類的toString()而對原始類型進(jìn)行封裝操作蒿往。比這種方法更有效的是調(diào)用封裝類的toString(…)方法例如:
new Integer(1).toString()
替換為 Integer.toString(1)
new Long(1).toString()
替換為 Long.toString(1)
new Float(1.0).toString()
替換為 Float.toString(1.0)
new Double(1.0).toString()
替換為 Double.toString(1.0)
new Byte(1).toString()
替換為 Byte.toString(1)
new Short(1).toString()
替換為 Short.toString(1)
new Boolean(true).toString()
替換為 Boolean.toString(true)
DM_FP_NUMBER_CTOR
Method invokes inefficient floating-point Number constructor; use static valueOf instead
使用new Double(double)方法總是會創(chuàng)建一個新的對象杉畜,然而使用Double.valueOf(double)方法可以把值保存在編輯器或者class library、JVM中蹭秋。使用存儲值的方式來避免對象的分配可以或得更好的代碼性能
除非類必須符合Java 1.5以前的JVM扰付,否則請使用自動裝箱或valueOf()方法創(chuàng)建Double和Float實例。
DM_NUMBER_CTOR
Method invokes inefficient Number constructor; use static valueOf instead
使用new Integer(int)方法總是會創(chuàng)建一個新的對象感凤,然而使用Integer.valueOf(int)方法可以把值保存在編輯器或者class library悯周、JVM中。使用存儲值的方式來避免對象的分配可以或得更好的代碼性能
除非類必須符合Java 1.5以前的JVM陪竿,否則請使用自動裝箱或valueOf()方法創(chuàng)建Long, Integer, Short, Character, Byte實例禽翼。
DMI_BLOCKING_METHODS_ON_URL
The equals and hashCode methods of URL are blocking
使用equals和hashCode方法來對url進(jìn)行資源標(biāo)識符解析時會引起堵塞屠橄。考慮使用java.net.URI來代替闰挡。
DMI_COLLECTION_OF_URLS
Maps and sets of URLs can be performance hogs
方法或者字段使用url的map/set集合锐墙。因為equals方法或者h(yuǎn)ashCode方法來進(jìn)行資源標(biāo)識符解析時都會引起堵塞〕ば铮考慮使用java.net.URI來代替溪北。
DM_BOOLEAN_CTOR
Method invokes inefficient Boolean constructor; use Boolean.valueOf(...) instead
使用new方法創(chuàng)建一個java.lang.Boolean類型能夠的實例對象是浪費空間的,因為Boolean對象是不可變的而且只有兩個有用的值夺脾。使用Boolean.valueOf()或者Java1.5中的自動裝箱功能來創(chuàng)建一個Boolean實例之拨。
DM_GC
Explicit garbage collection; extremely dubious except in benchmarking code
在代碼中顯式的調(diào)用垃圾回收命名,這樣做并不能起作用咧叭。在過去蚀乔,有人在關(guān)閉操作或者finalize方法中調(diào)用垃圾回收方法導(dǎo)致了很多的性能浪費。這樣大規(guī)姆撇纾回收對象時會造成處理器運行緩慢吉挣。
DM_NEXTINT_VIA_NEXTDOUBLE
Use the nextInt method of Random rather than nextDouble to generate a random integer
如果r是一個java.util.Random對象,你可以使r.nextInt(n)生成一個0到n-1之前的隨機(jī)數(shù)婉弹,而不是使用(int)(r.nextDouble() * n)
DM_STRING_CTOR
Method invokes inefficient new String(String) constructor
使用java.lang.String(String)構(gòu)造函數(shù)會浪費內(nèi)存因為這種構(gòu)造方式和String作為參數(shù)在功能上容易混亂睬魂。只是使用String直接作為參數(shù)的形式
DM_STRING_TOSTRING
Method invokes toString() method on a String
調(diào)用String.toString()是多余的操作,只要使用String就可以了镀赌。
DM_STRING_VOID_CTOR
Method invokes inefficient new String() constructor
使用沒有參數(shù)的構(gòu)造方法去創(chuàng)建新的String對象是浪費內(nèi)存空間的氯哮,因為這樣創(chuàng)建會和空字符串“”混淆。Java中保證完成相同的構(gòu)造方法會產(chǎn)生描繪相同的String對象佩脊。所以你只要使用空字符串來創(chuàng)建就可以了蛙粘。
ITA_INEFFICIENT_TO_ARRAY
Method uses toArray() with zero-length array argument
當(dāng)使用集合的toArray()方法時使用數(shù)組長度為0的數(shù)組作為參數(shù)。比這更有效的一種方法是
myCollection.toArray(new Foo[myCollection.size()])威彰,如果數(shù)組的長度足夠大就可以直接把集合中的內(nèi)容包裝到數(shù)組中直接返回從而避免了第二次創(chuàng)建一個新的數(shù)組來存放集合中值出牧。
SBSC_USE_STRINGBUFFER_CONCATENATION
Method concatenates strings using + in a loop
在循環(huán)中構(gòu)建一個String對象時從性能上講使用StringBuffer來代替String對象
例如:
// This is bad String s = ""; for (int i = 0; i < field.length; ++i) { s = s + field[i]; }
// This is better StringBuffer buf = new StringBuffer(); for (int i = 0; i < field.length; ++i) { buf.append(field[i]); } String s = buf.toString();
SS_SHOULD_BE_STATIC
Unread field: should this field be static?
類中所包含的final屬性字段在編譯器中初始化為靜態(tài)的值⌒危考慮在定義時就把它定義為static類型的舔痕。
UM_UNNECESSARY_MATH
Method calls static Math class method on a constant value
在方法中使用了java.lang.Math的靜態(tài)方法代替常量來使用,使用常量速度和準(zhǔn)確度會更好豹缀。 以下為Math中的方法產(chǎn)生的值伯复。
Method Parameter abs -any- acos 0.0 or 1.0 asin 0.0 or 1.0 atan 0.0 or 1.0 atan2 0.0 cbrt 0.0 or 1.0 ceil -any- cos 0.0 cosh 0.0 exp 0.0 or 1.0 expm1 0.0 floor -any- log 0.0 or 1.0 log10 0.0 or 1.0 rint -any- round -any- sin 0.0 sinh 0.0 sqrt 0.0 or 1.0 tan 0.0 tanh 0.0 toDegrees 0.0 or 1.0 toRadians 0.0
UPM_UNCALLED_PRIVATE_METHOD
Private method is never called
定義為Private類型方法從未被調(diào)用,應(yīng)該被刪除邢笙。
URF_UNREAD_FIELD
Unread field
類中定義的屬性從未被調(diào)用啸如,建議刪除。
UUF_UNUSED_FIELD
Unused field
類中定義的屬性從未被使用氮惯,建議刪除叮雳。
WMI_WRONG_MAP_ITERATOR
Inefficient use of keySet iterator instead of entrySet iterator
當(dāng)方法中接受一個Map類型的參數(shù)時想暗,使用keySet的迭代器比使用entrySet的迭代器效率要高。
DM_BOXED_PRIMITIVE_FOR_PARSING
A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method.
不需要使用封裝/反封裝來解析一個基本類型帘不,使用parseXXX效率更高
其他文章(持續(xù)更新)
FindBugs:簡介與使用
FindBugs 規(guī)則整理:CORRECTNESS
FindBugs 規(guī)則整理:Bad Practice
FindBugs 規(guī)則整理:Style & Dodgy
FindBugs 規(guī)則整理:Malicious Code Vulnerability
FindBugs 規(guī)則整理:Multithreaded Correctness
FindBugs 規(guī)則整理:Internationalization
引用
整合以下文章過程中發(fā)現(xiàn)部分存在翻譯錯誤说莫,已做修正,同時感謝以下文章作者
FindBugs規(guī)則整理