使用SpotBugs/FindBugs進行代碼檢查

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

 

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

  目前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。git

  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的代碼來進行檢查。github

 

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及相關插件dom

<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/eclipse

安裝插件後,能夠在對應的Preferences 進行更多SpotBugs plugin安裝,參考以下圖:maven


能夠經過如下網址獲取最新的插件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 版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索