FindBugs 規(guī)則整理:Bad Practice

目前已轉(zhuǎn)至個人博客,本系列地址:Lam's Blog - Knowledge as Action

NM_SAME_SIMPLE_NAME_AS_SUPERCLASS

  • 翻譯
    The class name ... shadows the simple name of the superclass ...
    This class has a simple name that is identical to that of its superclass, except that its superclass is in a different package (e.g.,alpha.Fooextendsbeta.Foo). This can be exceptionally confusing, create lots of situations in which you have to look at import statements to resolve references and creates many opportunities to accidently define methods that do not override methods in their superclasses.

    某個類的名字和父類名稱一樣,這將會導致混亂的情況,一旦出現(xiàn)問題也將難以發(fā)現(xiàn)。
  • 原因
    同翻譯
  • 解決方案
    修改父類或者子類的名稱

ES_COMPARING_STRINGS_WITH_EQ

  • 翻譯
    Comparison of String objects using == or != in ...
    This code ... 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.intern()方法,否則兩個同樣值的字符串可能會被兩個不一樣的字符串對象持有,考慮使用equals(Object)來比較。
  • 原因
    使用==或者!=來比較字符串會比較兩個字符串對象的內(nèi)存地址,通常這是不一樣的。
  • 解決方案
    如果是想比較值的話通過equals(Object)來比較

SE_BAD_FIELD

  • 翻譯
    Class ... defines non-transient non-serializable instance field ...
    This Serializable class defines a non-primitive instance field which is neither transient, Serializable, orjava.lang.Object, and does not appear to implement the Externalizableinterface 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.

    該可序列化類定義了一個非暫存也不可序列化的非原始實例字段,也沒有實現(xiàn)Externalizable接口或者readObject() 和 writeObject()方法,所以如果非序列化對象被存儲在該字段里,那么這個類的對象將無法正確將其反序列化
  • 原因
    序列化時所有的成員變量都必須遞歸的實現(xiàn)序列化,否則將導致序列化失敗。如果某個成員變量不想被序列化要么標注為瞬態(tài)要么重寫readObj方法
  • 解決方案
    給字段加上序列化或者瞬態(tài)關(guān)鍵字transient

AM_CREATES_EMPTY_JAR_FILE_ENTRY

Creates an empty jar file entry
調(diào)用putNextEntry()方法寫入新的 jar 文件條目時立即調(diào)用closeEntry()方法。這樣會造成JarFile條目為空。

AM_CREATES_EMPTY_ZIP_FILE_ENTRY

Creates an empty zip file entry
調(diào)用putNextEntry()方法寫入新的 zip 文件條目時立即調(diào)用closeEntry()方法。這樣會造成ZipFile條目為空。

BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS

Equals method should not assume anything about the type of its argument
equals(Object o)方法不能對參數(shù)o的類型做任何的假設(shè)。比較此對象與指定的對象。當且僅當該參數(shù)不為 null,并且是表示與此對象相同的類型的對象時,結(jié)果才為 true。

DMI_RANDOM_USED_ONLY_ONCE

Random object created and used only once
隨機創(chuàng)建對象只使用過一次就拋棄

BIT_SIGNED_CHECK

Check for sign of bitwise operation
檢查位操作符運行是否合理

CN_IDIOM

Class implements Cloneable but does not define or use clone method
類實現(xiàn)了Cloneable接口,但是沒有定義或使用clone方法。按照慣例,實現(xiàn)此接口的類應該使用公共方法重寫 Object.clone(它是受保護的),以獲得有關(guān)重寫此方法的詳細信息。

CN_IDIOM_NO_SUPER_CALL

clone method does not call super.clone()
一個非final類型的類定義了clone()方法而沒有調(diào)用super.clone()方法。例如:B擴展自A,如果B中clone方法調(diào)用了spuer.clone(),而A中的clone沒有調(diào)用spuer.clone(),就會造成結(jié)果類型不準確。要求A的clone方法中調(diào)用spuer.clone()方法。

CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE

Class defines clone() but doesn't implement Cloneable
類中定義了clone方法但是它沒有實現(xiàn)Cloneable接口

CO_ABSTRACT_SELF

Abstract class defines covariant compareTo() method
抽象類中定義了多個compareTo()方法,正確的是覆寫Comparable中的compareTo方法,方法的參數(shù)為Object類型

CO_SELF_NO_OBJECT

Covariant compareTo() method defined
類中定義了多個compareTo()方法,正確的是覆寫Comparable中的compareTo方法,方法的參數(shù)為Object類型

DE_MIGHT_DROP

Method might drop exception
方法可能拋出異常

DE_MIGHT_IGNORE

Method might ignore exception
方法可能忽略異常

DMI_USING_REMOVEALL_TO_CLEAR_COLLECTION

Don't use removeAll to clear a collection
不要用removeAll方法去clear一個集合

DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED

Classloaders should only be created inside doPrivileged block
類加載器只能建立在特殊的方法體內(nèi)

DM_EXIT

Method invokes System.exit(...)
在方法中調(diào)用System.exit(...)語句,考慮用RuntimeException來代替

DM_RUN_FINALIZERS_ON_EXIT

Method invokes dangerous method runFinalizersOnExit
在方法中調(diào)用了System.runFinalizersOnExit 或者Runtime.runFinalizersOnExit方法,因為這樣做是很危險的。

ES_COMPARING_PARAMETER_STRING_WITH_EQ

Comparison of String parameter using == or !=
用==或者!=方法去比較String類型的參數(shù)

EQ_ABSTRACT_SELF

Abstract class defines covariant equals() method
抽象類定義了共變的compareTo()方法

EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS

Equals checks for noncompatible operand
equals方法檢查不一致的操作。兩個類根本就是父子關(guān)系而去調(diào)用equals方法去判讀對象是否相等。

EQ_COMPARETO_USE_OBJECT_EQUALS

Class defines compareTo(...) and uses Object.equals()
類定義了compareTo(...)方法,卻使用Object.equals()方法

EQ_GETCLASS_AND_CLASS_CONSTANT

equals method fails for subtypes
類中的equals方法可能被子類中的方法所破壞,當使用類似于Foo.class == o.getClass()的判斷時考慮用this.getClass() == o.getClass()來替換

EQ_SELF_NO_OBJECT

Covariant equals() method defined
類中定義了多個equals方法。正確的做法是覆寫Object中的equals方法,它的參數(shù)為Object類型的對象。

FI_EMPTY

Empty finalizer should be deleted
應當刪除空的finalize()方法

GC_UNCHECKED_TYPE_IN_GENERIC_CALL

Unchecked type in generic call
泛型調(diào)用中使用了未檢查的類型

HE_EQUALS_NO_HASHCODE

Class defines equals() but not hashCode()
方法定義了equals方法卻沒有定義hashCode方法

HE_HASHCODE_NO_EQUALS

Class defines hashCode() but not equals()
類定義了hashCode方法去沒有定義equal方法

HE_EQUALS_USE_HASHCODE

Class defines equals() and uses Object.hashCode()
一個類覆寫了equals方法,沒有覆寫hashCode方法,使用了Object對象的hashCode方法

HE_INHERITS_EQUALS_USE_HASHCODE

Class inherits equals() and uses Object.hashCode()
子類繼承了父類的equals方法卻使用了Object的hashCode方法

IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION

Superclass uses subclass during initialization
子類在父類未初始化之前使用父類對象實例

IMSE_DONT_CATCH_IMSE

Dubious catching of IllegalMonitorStateException
可疑的IllegalMonitorStateException異常捕捉

ISC_INSTANTIATE_STATIC_CLASS

Needless instantiation of class that only supplies static methods
為使用靜態(tài)方法而創(chuàng)建一個實例對象。調(diào)用靜態(tài)方法時只需要使用類名+靜態(tài)方法名就可以了。

IT_NO_SUCH_ELEMENT

Iterator next() method can't throw NoSuchElementException
迭代器的next方法不能夠拋出NoSuchElementException

J2EE_STORE_OF_NON_SERIALIZABLE_OBJECT_INTO_SESSION

Store of non serializable object into HttpSession
在HttpSession對象中保存非連續(xù)的對象

JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS

Fields of immutable classes should be final
不可變類的字段應當是final的

NP_BOOLEAN_RETURN_NULL

Method with Boolean return type returns explicit null
返回值為boolean類型的方法直接返回null,這樣會導致空指針異常

NP_EQUALS_SHOULD_HANDLE_NULL_ARGUMENT

equals() method does not check for null argument
變量調(diào)用equals方法時沒有進行是否為null的判斷

NP_TOSTRING_COULD_RETURN_NULL

toString method may return null
toString方法可能返回null

NM_CLASS_NAMING_CONVENTION

Class names should start with an upper case letter
類的名稱以大寫字母名稱開頭

NM_CLASS_NOT_EXCEPTION

Class is not derived from an Exception, even though it is named as such
類的名稱中含有Exception但是卻不是一個異常類的子類,這種名稱會造成混淆

NM_CONFUSING

Confusing method names
令人迷惑的方法命名

NM_FIELD_NAMING_CONVENTION

Field names should start with a lower case letter
非final類型的字段需要遵循駝峰命名原則

NM_FUTURE_KEYWORD_USED_AS_IDENTIFIER

Use of identifier that is a keyword in later versions of Java
驗證是否是java預留關(guān)鍵字

NM_FUTURE_KEYWORD_USED_AS_MEMBER_IDENTIFIER

Use of identifier that is a keyword in later versions of Java
驗證是否時java中的關(guān)鍵字

NM_METHOD_NAMING_CONVENTION

Method names should start with a lower case letter
方法名稱以小寫字母開頭

NM_SAME_SIMPLE_NAME_AS_INTERFACE

Class names shouldn't shadow simple name of implemented interface
實現(xiàn)同一接口實現(xiàn)類不能使用相同的名稱,即使它們位于不同的包中

NM_SAME_SIMPLE_NAME_AS_SUPERCLASS

Class names shouldn't shadow simple name of superclass
繼承同一父類的子類不能使用相同的名稱,即使它們位于不同的包中

NM_VERY_CONFUSING_INTENTIONAL

Very confusing method names (but perhaps intentional)
很容易混淆的方法命名,例如方法的名稱使用大小寫來區(qū)別兩個不同的方法。

NM_WRONG_PACKAGE_INTENTIONAL

Method doesn't override method in superclass due to wrong package for parameter
由于錯誤引用了不同包中相同類名的對象而不能夠正確的覆寫父類中的方法

ODR_OPEN_DATABASE_RESOURCE

Method may fail to close database resource
方法中可能存在關(guān)閉數(shù)據(jù)連接失敗的情況

OS_OPEN_STREAM

Method may fail to close stream
方法中可能存在關(guān)閉流失敗的情況

OS_OPEN_STREAM_EXCEPTION_PATH

Method may fail to close stream on exception
方法中可能存在關(guān)閉流時出現(xiàn)異常情況

RC_REF_COMPARISON_BAD_PRACTICE

Suspicious reference comparison to constant
當兩者為不同類型的對象時使用equals方法來比較它們的值是否相等,而不是使用==方法。例如比較的兩者為java.lang.Integer, java.lang.Float

RC_REF_COMPARISON_BAD_PRACTICE_BOOLEAN

  • Suspicious reference comparison of Boolean values*
    使用== 或者 !=操作符來比較兩個 Boolean類型的對象,建議使用equals方法。

RR_NOT_CHECKED

Method ignores results of InputStream.read()
InputStream.read方法忽略返回的多個字符,如果對結(jié)果沒有檢查就沒法正確處理用戶讀取少量字符請求的情況。

SR_NOT_CHECKED

Method ignores results of InputStream.skip()
InputStream.skip()方法忽略返回的多個字符,如果對結(jié)果沒有檢查就沒法正確處理用戶跳過少量字符請求的情況

RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

Method ignores exceptional return value
方法忽略返回值的異常信息

SI_INSTANCE_BEFORE_FINALS_ASSIGNED

Static initializer creates instance before all static final fields assigned
在所有的static final字段賦值之前去使用靜態(tài)初始化的方法創(chuàng)建一個類的實例。

SE_BAD_FIELD_STORE

Non-serializable value stored into instance field of a serializable class
非序列化的值保存在聲明為序列化的的非序列化字段中

SE_COMPARATOR_SHOULD_BE_SERIALIZABLE

Comparator doesn't implement Serializable
Comparator接口沒有實現(xiàn)Serializable接口

SE_INNER_CLASS

Serializable inner class
序列化內(nèi)部類

SE_NONFINAL_SERIALVERSIONID

serialVersionUID isn't final
serialVersionUID不是final的

SE_NO_SUITABLE_CONSTRUCTOR

Class is Serializable but its superclass doesn't define a void constructor
子類序列化時父類沒有提供一個void的構(gòu)造函數(shù)

SE_NO_SUITABLE_CONSTRUCTOR_FOR_EXTERNALIZATION

Class is Externalizable but doesn't define a void constructor
Externalizable 實例類沒有定義一個void類型的構(gòu)造函數(shù)

SE_READ_RESOLVE_MUST_RETURN_OBJECT

The readResolve method must be declared with a return type of Object.
readResolve從流中讀取類的一個實例,此方法必須聲明返回一個Object類型的對象

SE_TRANSIENT_FIELD_NOT_RESTORED

Transient field that isn't set by deserialization.
不需要被反序列化的字段沒有聲明transient

SE_NO_SERIALVERSIONID

Class is Serializable, but doesn't define serialVersionUID
一個類實現(xiàn)了Serializable接口但是沒有定義serialVersionUID類型的變量。序列化運行時使用一個稱為 serialVersionUID 的版本號與每個可序列化類相關(guān)聯(lián),該序列號在反序列化過程中用于驗證序列化對象的發(fā)送者和接收者是否為該對象加載了與序列化兼容的類。如果接收者加載的該對象的類的 serialVersionUID 與對應的發(fā)送者的類的版本號不同,則反序列化將會導致 InvalidClassException??尚蛄谢惪梢酝ㄟ^聲明名為 "serialVersionUID" 的字段(該字段必須是靜態(tài) (static)、最終 (final) 的 long 型字段)顯式聲明其自己的 serialVersionUID

UI_INHERITANCE_UNSAFE_GETRESOURCE

Usage of GetResource may be unsafe if class is extended
當一個類被子類繼承后不要使用this.getClass().getResource(...)來獲取資源

其他文章(持續(xù)更新)

FindBugs:簡介與使用
FindBugs 規(guī)則整理:CORRECTNESS
FindBugs 規(guī)則整理:Style & Dodgy
FindBugs 規(guī)則整理:Malicious Code Vulnerability
FindBugs 規(guī)則整理:Multithreaded Correctness
FindBugs 規(guī)則整理:Security & Experimental
FindBugs 規(guī)則整理:Performance
FindBugs 規(guī)則整理:Internationalization

引用

整合以下文章過程中發(fā)現(xiàn)部分存在翻譯錯誤,已做修正,同時感謝以下文章作者
FindBugs使用備忘錄
FindBugs規(guī)則整理
詳解FindBugs的各項檢測器
Findbugs 缺陷詳解與英文代號的對照表

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 對象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對象:僅僅是創(chuàng)建對象的方法,并非Fa...
    孫小磊閱讀 2,182評論 0 3
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,638評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,535評論 19 139
  • 文章作者:Tyan博客:noahsnail.com | CSDN | 簡書 CHAPTER3 Method...
    SnailTyan閱讀 774評論 1 4
  • 去年在大理的時候,去了心念的洱海,骨子里太著迷于洱海便特意去租了一間臨著它布局氛圍又文藝溫馨的民宿。 比較幸運的是...
    端木婉清閱讀 1,224評論 13 18

友情鏈接更多精彩內(nèi)容