使用SpotBugs/FindBugs進行代碼檢查


原po:https://blog.csdn.net/zhangb00/article/details/8407070

 

SpotBugs 介紹
  SpotBugs是Findbugs的繼任者(Findbugs已經於2016年后不再維護,see https://mailman.cs.umd.edu/pipermail/findbugs-discuss/2016-November/004321.html),用於對代碼進行靜態分析,查找相關的漏洞。

  目前SpotBugs 3.1.3 自帶檢測器,其中有90余種Bad practice,155余種Correctness,9種Experimental, 2種 Internationalization,17種Malicious code vulnerability,46種Multithreaded correctness,4種 Bogus random noise,37種Performance,11種 Security,87種Dodgy。

  Bad practice 不佳實踐:常見代碼錯誤,用於靜態代碼檢查時進行缺陷模式匹配(如重寫equals但沒重寫hashCode,或相反情況等)
  Correctness 可能導致錯誤的代碼(如空指針引用、無限循環等)
  Experimental 實驗性
  Internationalization 國際化相關問題(如錯誤的字符串轉換等)
  Malicious code vulnerability 可能受到的惡意攻擊(如訪問權限修飾符的定義等)
  Multithreaded correctness 多線程的正確性(如多線程編程時常見的同步,線程調度問題等)
  BogusMultithreaded correctness 多線程的正確性(如多線程編程時常見的同步,線程調度問題等)
  Performance 運行時性能問題(如由變量定義,方法調用導致的代碼低效問題等)
  Security 安全問題(如HTTP,SQL,DB等)
  Dodgy code 導致自身錯誤的代碼(如未確認的強制轉換、冗余的空值檢查等)
注: SpotBugs 需要當前的JDK環境為 1.8以上,但可以對1.0~1.9的代碼來進行檢查。

 

SpotBugs 插件
  SpotBugs 還有對應的額外插件,用於擴展對應的規則,探測出更多的代碼問題。

  fb-contrib
  目前最新為7.4.3,增加了大致130+條規則;其中因 FindBugs分叉,SpotBugs需要使用-sb版本。
  官網 : https://github.com/mebigfatguy/fb-contrib

find-sec-bugs
目前最新為 1.8.0,針對安全(Security)增加了大致70+條規則
官網: https://github.com/find-sec-bugs/find-sec-bugs

SpotBugs 使用
Maven
maven 插件方式使用 spotbugs及相關插件

<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<plugins>
<plugin>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findsecbugs-plugin</artifactId>
<version>LATEST</version>
</plugin>
<plugin>
<groupId>com.mebigfatguy.fb-contrib</groupId>
<artifactId>fb-contrib</artifactId>
<version>7.4.3.sb</version>
</plugin>
</plugins>
</configuration>
</plugin>

IDE環境
Eclipse
請在對應的marketplace 搜索或install new software來進行spotbugs的安裝,正式版本的安裝路徑 https://spotbugs.github.io/eclipse/

安裝插件后,可以在對應的Preferences 進行更多SpotBugs plugin安裝,參考如下圖:


可以通過以下網址獲取最新的插件jar
https://mvnrepository.com/artifact/com.mebigfatguy.fb-contrib/fb-contrib
https://mvnrepository.com/artifact/com.h3xstream.findsecbugs/findsecbugs-plugin

完成安裝后,可以對java工程或者對應的類,右鍵進行 SpotBugs ->Find Bugs


JetBrain IDEA
目前沒有SpotBugs插件,原FindBugs插件作者、IDEA還在討論商標問題,請先使用 原有FindBugs代替。
https://github.com/andrepdo/findbugs-idea/issues/265
https://youtrack.jetbrains.com/issue/IDEA-201846

其他
其他更多內容可以參考SpotBugs 官方文檔
https://spotbugs.readthedocs.io/en/latest/installing.html
---------------------
作者:zhangb00
來源:CSDN
原文:https://blog.csdn.net/zhangb00/article/details/84070706
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM