一、簡介
?????? Findbugs 是一個靜態分析工具,它檢查類或者 JAR 文件,將字節碼與一組缺陷模式進行對比以發現可能的問題。利用這個工具,就可以在不實際運行程序的情況對軟件進行分析。它可以幫助改進代碼的質量。
?????? Findbugs 提供了方便操作的可視化界面,同時也可以作為 Eclipse 的一個插件來使用,而我們使用得最多的還是作為 Eclipse 的插件來使用。
二、使用方法
?????? Findbugs 可以通過三種方法使用,可以通過 Ant 工具,通過 Ant 提供的 Swing 操作界面和作為 Eclipse 的一個插件來使用。
1. Ant工具
Ant是一個很好的Java自動執行工具。
???????? Findbugs 官方提供了 Ant 的 findbugs 操作方法,我們可以通過這樣一個 build.xml 文件來使用 findbugs 。
<project name=" 項目名 " default="all">
<property name= "findbugs.home" value= "findbugs 解壓路徑 " />
???????? ???? <path id= "findbugs.path" >
???????? ???????? <fileset dir= "findbugs 解壓路徑 " >
?????????????????? ???? <include name= "**/*.jar" />
????????????? ???? </fileset>
???????? ???? </path>
???????? ???? <taskdef name= "findbugs"
????????????? ???? classname= "edu.umd.cs.findbugs.anttask.FindBugsTask"
????????????? ???? classpathref= "findbugs.path" />
???????? ???? <!-- ? 定義 findbugs 的 home , findbugs 的 task 要使用 ? -->
???????? ???? <target name= "findbugs" >
????????????? ???? <findbugs home= "${findbugs.home}"
?????????????????? ???? output= "xml:withMessages" outputFile= " 生成的文件 " >
?
?????????????????? ???? <!-- ? 以上定義 findbugs 查找的類路徑 ? -->
?????????????????? ???? <auxClasspath path= "${findbugs.home}/lib/findbugs-ant.jar" />
?????????????????? ???? <auxClasspath>
?????????????????????? ???? <fileset dir= "lib"
??????????????????????????? includes= "*.jar" />
?????????????????? ???? </auxClasspath>
?????????????????? ???? <sourcePath path= " 源文件路徑 " />
???? ????? ??????? ???? <class location= " 生成類路徑 " />
????????????? ???? </findbugs>
???? ???? </target>
???? </project>
比如:我這里有一個我放在博客上的項目的findbugs的ant操作的build文件。
<project name="Calendar" default="all">
<property name= "findbugs.home" value= "../../findbugs-1.3.8" />
???????? ???? <path id= "findbugs.path" >
???????? ???????? <fileset dir= "../../findbugs-1.3.8" >
?????????????????? ???? <include name= "**/*.jar" />
????????????? ???? </fileset>
???????? ???? </path>
???????? ???? <taskdef name= "findbugs"
????????????? ???? classname= "edu.umd.cs.findbugs.anttask.FindBugsTask"
????????????? ???? classpathref= "findbugs.path" />
???????? ???? <!-- ? 定義 findbugs 的 home , findbugs 的 task 要使用 ? -->
???????? ???? <target name= "findbugs" >
????????????? ???? <mkdir dir= "target/findbugs" />
????????????? ???? <findbugs home= "${findbugs.home}"
?????????????????? ???? output= "xml:withMessages" outputFile= "target/findbugs/calendar-fb.xml" >
?????????????????? ???? <!-- ? 以上定義 findbugs 查找的類路徑 ? -->
?????????????????? ???? <auxClasspath path= "${findbugs.home}/lib/findbugs-ant.jar" />
?????????????????? ???? <auxClasspath>
?????????????????????? ???? <fileset dir= "lib"
??????????????????????????? includes= "*.jar" />
?????????????????? ???? </auxClasspath>
?????????????????? ???? <sourcePath path= "src" />
???? ????? ??????? ???? <class location= "target/classes" />
????????????? ???? </findbugs>
???? ???? </target>
???? </project>
設置好 Ant 的環境后,在命令中使用 ant -f build.xml ,或者在 Eclipse 直接運行 build.xml 文件,運行后生成了一個 xml 文件,如果你想用 Html 的格式查看 findbugs 的結果,可以把 output 屬性設為: html 。這樣就可以通過 Html 來查看 findbugs 的結果了。
2. 提供的 Swing 工具
?????? Ant操作是專家級的操作,一般對于Java不是很熟悉的人,寫build.xml文件。比起 Ant 來,使用 Findbugs 提供的 Swing 工具會使 Findbugs 的操作更加簡單。運行 Findbugs 解壓包中的 bin 文件夾下的 findbugs.bat 文件。
Findbugs 的 Swing 工具初始主界面如下:
?
在分析項目之前,我們必須要新建一個項目來分析,選擇文件 -> 新建
顯示新建項目的界面如下圖:
?
?
然后添加要分析的類包和目錄(可以選擇編譯好的類所在的文件夾,也可以選擇生成的
jar
包),再添加輔助類所在的文件夾和源文件所在的文件夾(
java
文件所在的文件夾)。再點擊完成就可以建立一個要分析的項目。
?
建立項目后,會自動先自動開始解析項目。
解析后界面:
其中左邊是缺陷的樹結構列表,點擊其中一個 Bug ,可以在右邊的界面中,顯示 Bugs 的源文件以及所在的位置。
2.3 ???????? Findbugs Eclipse 插件
Eclipse的Findbugs 插件,可以將 Findbugs 集成到 Eclipse 中使用。
2.3.1 ??????? Findbugs 的 Eclipse 插件安裝方法
??????? 1. ????? 在線安裝
安裝地址: http://findbugs.cs.umd.edu/eclipse
??????? 2. ????? 離線安裝
下載 Findbugs 插件,將它放入 Eclipse 下的 plusin 文件夾,然后重啟 Eclipse
?
??????? 2.3.2 ??????? Findbugs 的 Eclipse 插件使用
???????? 安裝了 Findbugs 插件后。右擊點擊你要檢查的項目選擇【 Find Bugs 】 -> 【 Find Bugs 】進行檢查。
??????? 要查看 Findbugs 檢查出了哪些 Bug ,可以選擇 Windows 菜單 ->Show View->Bug Explorer ,打開 Bug Explorer 面板。
???????? 如果想要查看某個 Bug 詳細的信息,則可以選擇 Windows 菜單 ->Open Perspective ,然后選擇 FindBugs 就可以打開 FindBugs 的 Properties 面板,在這個面板里面可以看到最詳盡的 Bugs 信息。
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
