白盒靜態自動化測試工具:FindBugs使用指南


目 錄     
1     FINDBUGS介紹     
2     在ECLIPSE中安裝FINDBUGS插件     
3     在ECLIPSE中使用FINDBUGS操作步驟     
3.1     打開FindBugs視圖     
3.2     執行FindBugs任務     
4     配置FINDBUGS     
4.1     Run Automatically開關     
4.2     Detector Configuration選擇項     
4.3     Minimum priority to report選擇項     
4.4     Report bug categories選擇項     
5     Ant 使用簡介     
6     總結     

1     FindBugs介紹
     FindBugs是一款Java靜態代碼分析工具,與其他靜態分析工具(如Checkstyle和PMD)不同,FindBugs不注重樣式或者格式,它專注於尋找真正的缺陷或者潛在的性能問題,它可以幫助java工程師提高代碼質量以及排除隱含的缺陷。有了靜態分析工具,就可以在不實際運行程序的情況對軟件進行分析。
      最新版本是1.3.9.20090821,下載地址  http://findbugs.sourceforge.net/downloads.html
     FindBugs運用Apache BCEL 庫分析類文件(class文件)而不是源代碼,將字節碼與一組缺陷模式進行對比以發現可能的問題。FindBugs的檢測器已增至300多條,被分為不同的類型,常見的類型如下:
  • 正確性(Correctness):這種歸類下的問題在某種情況下會導致bug,比如錯誤的強制類型轉換等。
  • 最佳實踐反例(Bad practice):這種類別下的代碼違反了公認的最佳實踐標准,比如某個類實現了equals方法但未實現hashCode方法等。
  • 多線程正確性(Multithreaded correctness):關注於同步和多線程問題。
  • 性能(Performance):潛在的性能問題。
  • 安全(Security):安全相關。
  • 高危(Dodgy):FindBugs團隊認為該類型下的問題代碼導致bug的可能性很高。
2     在Eclipse中安裝FindBugs插件
以下為我的安裝步驟,可能大家用的是MyEclipse步驟不僅相同。

Step 1:

 

Step 2:

 

Step 3:

 

3     在Eclipse中使用FindBugs操作步驟
3.1     打開FindBugs視圖
step1:

step2:

 

step3:

 


3.2     執行FindBugs任務
     右鍵單擊你要檢測的工程、包或文件-->Find Bugs-->Find Bugs。

     check完成后將在Bug Explorer視圖中看到問題列表,該列表以問題類型組織。

     展開列表,雙擊列表中具體的問題就可以定位的具體的代碼行。

4     配置FindBugs
     在這里可以對FindBugs規則等進行詳細設置。選擇你的項目,右鍵 --> Properties --> FindBugs -->

4.1     Run Automatically開關
     當此項選中后,FindBugs將會在你修改Java類時自動運行,如你設置了Eclipse自動編譯開關后,當你修改完Java文件保存,FindBugs就會運行,並將相應的信息實時顯示。
     當此項沒有選中,你只能每次在需要的時候自己去運行FindBugs來檢查你的代碼。
4.2     Detector Configuration選擇項
     在這里你可以選擇所要進行檢查的相關的Bug Pattern條目,你可以根據需要選擇或去掉相應的 檢查條件。

4.3     Minimum priority to report選擇項
     這個選擇項是讓你選擇哪個級別的信息進行顯示,有Low、Medium、High三個選擇項可以選擇,很類似於Log4J的級別設置啦。 比如:
  • High選擇項:只有是High級別的提示信息才會被顯示;
  • Medium選擇項:只有是Medium和High級別的提示信息才會被顯示;
  • Low選擇項:所有級別的提示信息都會被顯示。
4.4     Report bug categories選擇項
     在這里是一些顯示Bug分類的選擇:
  • Malicious code vulnerability關於惡意破壞代碼相關方面的
  • Correctness關於代碼正確性相關方面的
  • Internationalization關於代碼國際化相關方面的
  • Performance關於代碼性能相關方面的
  • Multithreaded correctness關於代碼多線程正確性相關方面的
5     Ant 使用簡介
     有了 Ant 這樣的自動化機制,就可以以多種方法監視軟件質量。有許多種掃描源代碼和二進制文件的工具,但是其中最流行的兩種是 PMD 和 FindBugs。PMD 掃描源代碼文件,並根據一系列規則檢查其中的代碼。如果代碼有問題,它就會報告一個違規。FindBugs 的作用與 PMD 相似,但是它掃描二進制文件(即類文件)並報告 bug。
     以下為配置腳本:
  < project  name ="analyze_asm_util"  default ="findbugs" >

   <!-- findbugs task definition -->
   <property name="findbugs.home" value="/Users/ben/Documents/workspace/findbugs/findbugs" />
   <property name="jvmargs" value="-server -Xss1m -Xmx800m -Duser.language=en -Duser.region=EN -Dfindbugs.home=${findbugs.home}" />

    <path id="findbugs.lib">
      <fileset dir="${findbugs.home}/lib">
         <include name="findbugs-ant.jar"/>
      </fileset>
   </path>

   <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
      <classpath refid="findbugs.lib" />
   </taskdef>

   <taskdef name="computeBugHistory" classname="edu.umd.cs.findbugs.anttask.ComputeBugHistoryTask">
      <classpath refid="findbugs.lib" />
   </taskdef>

   <taskdef name="setBugDatabaseInfo" classname="edu.umd.cs.findbugs.anttask.SetBugDatabaseInfoTask">
      <classpath refid="findbugs.lib" />
   </taskdef>

   <taskdef name="mineBugHistory" classname="edu.umd.cs.findbugs.anttask.MineBugHistoryTask">
      <classpath refid="findbugs.lib" />
   </taskdef>

   <!-- findbugs task definition -->
   <target name="findbugs">
      <antcall target="analyze" />
      <antcall target="mine" />
   </target>

   <!-- analyze task -->
   <target name="analyze">
      <!-- run findbugs against asm-util -->
      <findbugs home="${findbugs.home}"
                output="xml:withMessages"
                timeout="90000000"
                reportLevel="experimental"
                workHard="true"
                effort="max"
                adjustExperimental="true"
                jvmargs="${jvmargs}"
                failOnError="true"
                outputFile="out.xml"
                projectName="Findbugs"
                debug="false">
         <class location="asm-util-3.0.jar" />
      </findbugs>
   </target>

   <target name="mine">

      <!-- Set info to the latest analysis -->
      <setBugDatabaseInfo home="${findbugs.home}"
                            withMessages="true"
                            name="asm-util-3.0.jar"
                            input="out.xml"
                            output="out-rel.xml"/>

      <!-- Checking if history file already exists (out-hist.xml) -->
      <condition property="mining.historyfile.available">
         <available file="out-hist.xml"/>
      </condition>
      <condition property="mining.historyfile.notavailable">
         <not>
            <available file="out-hist.xml"/>
         </not>
      </condition>

      <!-- this target is executed if the history file do not exist (first run) -->
      <antcall target="history-init">
        <param name="data.file" value="out-rel.xml" />
        <param name="hist.file" value="out-hist.xml" />
      </antcall>
      <!-- else this one is executed -->
      <antcall target="history">
        <param name="data.file"         value="out-rel.xml" />
        <param name="hist.file"         value="out-hist.xml" />
        <param name="hist.summary.file" value="out-hist.txt" />
      </antcall>
   </target>

   <!-- Initializing history file -->
   <target name="history-init" if="mining.historyfile.notavailable">
      <copy file="${data.file}" tofile="${hist.file}" />
   </target>

   <!-- Computing bug history -->
   <target name="history" if="mining.historyfile.available">
      <!-- Merging ${data.file} into ${hist.file} -->
      <computeBugHistory home="${findbugs.home}"
                           withMessages="true"
                           output="${hist.file}">
            <dataFile name="${hist.file}"/>
            <dataFile name="${data.file}"/>
      </computeBugHistory>

      <!-- Compute history into ${hist.summary.file} -->
      <mineBugHistory home="${findbugs.home}"
                        formatDates="true"
                      noTabs="true"
                        input="${hist.file}"
                        output="${hist.summary.file}"/>
   </target>

</project>

 

6     總結
     更多詳情,請查閱官方手冊:
     http://findbugs.sourceforge.net/manual/index.html

 

 參考文獻:

http://findbugs.sourceforge.net/manual/index.html
http://blog.csdn.net/strawbingo/article/details/5924005
http://www.ibm.com/developerworks/cn/education/java/j-cq11207/section6.html

 


免責聲明!

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



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