1. 程式人生 > >IDEAL葵花寶典:java代碼開發規範插件 FindBugs-IDEA

IDEAL葵花寶典:java代碼開發規範插件 FindBugs-IDEA

finall rem width find 是否為空 col cnblogs input -s

前言:

  檢測代碼中可能的bug及不規範的位置,檢測的模式相比p3c更多,寫完代碼後檢測下 避免低級bug,強烈建議用一下,一不小心就發現很多老代碼的bug。

使用步驟:

1):打開 Settings---->Plugins---> 進行搜索--->選擇---->"FindBugs-IDEA" ----> install

技術分享圖片

安裝插件:點擊插件詳情中的"install"按鈕,按照其提示即可完成安裝,安裝完成後需重啟IDEA。

會發現左下角會出現findbugs的圖標 ;

技術分享圖片

可以分析單個文件,包下面的所有文件,整個module下的文件,整個project下的文件,右鍵想要分析的文件名/包名/module名/project:

技術分享圖片

分析完之後就會出現結果面板:(Badpractice 分類詳情請往下看...)

技術分享圖片

點擊對應的item在右邊會定位到具體的代碼:

技術分享圖片

根據需要可以進行更改,其中Correctness這個錯誤使我們重點關註的對象,這裏大多是空指針的錯誤,根據提示進行處理。

附:一些常見的錯誤信息 :

1):Bad practice 代碼中的一些壞習慣:

  Class names should start with an upper case letter :主要包括類名的命名,以大寫字母開頭

  Method names should start with a lower case letter:

方法名以小寫字母開頭

  Field names should start with a lower case letter :字段名以小寫字母開頭

  equals()method does not check for null argument equals():方法應該檢查非空

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

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

  Equals method should not assume anything about the type of its argument equals(Object o):方法不能對參數o的類型做任何的假設。比較此對象與指定的對象。

  當且僅當該參數不為 null,並且是表示與此對象相同的類型的對象時,結果才為 true。

  Comparison of String objects using :== or != 用==或者!=去比較String類型的對象

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

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

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

2):Dodgy code 糟糕的代碼:

  Switch statement found where default case is missing Switch:沒有默認情況下執行的case語句

  Switch statement found where one case falls through to the next case:Switch語句中一個分支執行後又執行了下一個分支。通常case後面要跟break 或者return語句來跳出。

  Dead store to local variable : 該指令為局部變量賦值,但在其後的沒有對她做任何使用。通常,這表明一個錯誤,因為值從未使用過。

  Write to static field from instance method :在實例方法寫入靜態字段

  Redundant nullcheck of value known to be non-null :方法中對不為空的值進行為空的判斷。

  Method uses the same code for two branches :此方法使用相同的代碼,以實現兩個有條件的分支。檢查以確保這是不是一個編碼錯誤

  Exception is caught when Exception is not thrown :在try/catch塊中捕獲異常,但是異常沒有在try語句中拋出而RuntimeException又沒有明確的被捕獲

  Integral division result cast to double or float :整形數除法強制轉換為double或者float類型。

  Possible null pointer dereference due to return value of called method: 方法的返回值沒有進行是否為空的檢查就重新賦值,這樣可能會出現空指針異常。

  Useless object created: 對象創建了並沒有用

  Unread public/protected field :沒有用到的字段

  Internationalization :關於代碼國際化相關方面的

  Consider using Locale parameterized version of invoked method :使用平臺默認的編碼格式對字符串進行大小寫轉換,這可能導致國際字符的轉換不當。使用以下方式對字符進行轉換

3):Performance 關於代碼性能相關方面的:

  Boxing/unboxing to parse a primitive :類型轉換 比如字符串轉換成int 應該使用Integer.parseInt(“”) 代替Integer.valueOf(“”)

  Method concatenates string using + in aloop:每次循環裏的字符串+連接,都會新產生一個string對象,在java中,新建一個對象的代價是很昂貴的,特別是在循環語句中,效率較低;

  解決辦法:使用StringBuffer或者StringBuilder重用對象。

  Private method is never called: 私有方法沒有被調用

  Explicit garbage collection;extremely dubious except in benchmarking code:

    在代碼中顯式的調用垃圾回收命名,這樣做並不能起作用。在過去,有人在關閉操作或者finalize方法中調用垃圾回收方法導致了很多的性能浪費。這樣大規模回收對象時會造成處理器運行緩慢。

  Unread field:should this field be static? :沒有用到的static 字段

  should be a static inner class 此內部類應該使用static修飾

4):Experimental:

  Method may fail to clean up stream or resource on checked exception:這種方法可能無法清除(關閉,處置)一個流,數據庫對象,或其他資源需要一個明確的清理行動

    解決方法:流的關閉都寫在finally裏面

  Malicious code vulnerability :關於惡意破壞代碼相關方面的

  May expose internal representation by incorporating reference to mutable object:

    此代碼把外部可變對象引用存儲到對象的內部表示。如果實例受到不信任的代碼的訪問和沒有檢查的變化危及對象和重要屬性的安全。存儲一個對象的副本,在很多情況下是更好的辦法。

  Field isn’t final but should be :此字段前應該加final

  Field isn’t final and can’t be protected from malicious code :此字段前應該加final

  Field should be package protected:一個靜態字段是可以被惡意代碼或其他的包訪問修改。可以把這種類型的字段聲明為final類型的以防止這種錯誤。

5):Multithreaded correctness :關於代碼正確性相關方面的:

  Static DateFormat DateFormat: 在多線程中本身就是不安全的,如果在線程範圍中共享一個DateFormat的實例而不使用一個同步的方法在應用中就會出現一些奇怪的行為。

  Call to static DateFormat DateFormats:多線程使用本事就是不安全的,改進方法:需要創建多實例或線程同步

6):Correctness :關於代碼正確性相關方面的:

  Nullcheck of value previously dereferenced :此代碼之前廢棄null值檢查。解決辦法 進行null檢查

  Possible null pointer dereference :可能為null

  Null pointer dereference :對象賦為null值後 沒有被重新賦值

  Possible null pointer dereference in method on exception path :在異常null值處理分支調用的方法上,可能存在對象去除引用操作

  value is null and guaranteed to be dereferenced on exception path exception:分支上,存在引用一個null對象的方法,引發空指針異常。

  Self comparison of value with itself: 方法中對一個局部變量自身進行比較運算,並可說明錯誤或邏輯錯誤。請確保您是比較正確的事情。

  An apparent infinite recursive loop :明顯的無限叠代循環,將導致堆棧溢出.

--------------------------------------------------------------------------------

以上內容若有不足之處:請多多請教

如要轉載請註明小編本站地址:(https://www.cnblogs.com/mlq2017/)

IDEAL葵花寶典:java代碼開發規範插件 FindBugs-IDEA