Eclipse FindBugs插件的安裝與使用

1 什麼是FindBugs
FindBugs 是一個靜態分析工具,它檢查類或者 JAR 文件,將字節碼與一組缺陷模式進行對比以發現可能的問題。有了靜態分析工具,就可以在不實際運行程序的情況對軟件進行分析。不是通過分析類文件的形式或結構來確定程序的意圖,而是通常使用 Visitor 模式來鑑別代碼是否符合一些固定的規範。
2 如何安裝FindBugs?
作爲Eclipse的一個插件,可以將Findbugs集成到Eclipse中使用。
第一種是在線安裝:在Eclipse的插件安裝地址中輸入http://findbugs.cs.umd.edu/eclipse並一路“next”就可安裝成功。
第二種方式是下載Findbugs插件,將它放入Eclipse下的plusin文件夾,然後重啓Eclipse即可。
3如何使用FindBugs
安裝了Findbugs插件後。右擊點擊你要檢查的項目選擇【Find Bugs】->【Find Bugs】進行檢查。要查看Findbugs檢查出了哪些Bug,可以選擇Windows菜單->Show View->Bug Explorer,打開Bug Explorer面板。如果想要查看某個Bug詳細的信息,則可以選擇Windows菜單->Open Perspective,然後選擇FindBugs就可以打開FindBugs的Properties面板,在這個面板裏面可以看到最詳盡的Bugs信息。
4 FindBugs能發現的所有Bug類型
FindBugs 網站http://findbugs.sourceforge.net/bugDescriptions.html提供了完整的類型清單。
To install the FindBugs plugin:
  1. In Eclipse, click on Help -> Software Update -> Find and Install...
  2. Choose the Search for new features to install option, and click Next.
  3. Click New Remote Site.
  4. Enter the following:
    • Name: FindBugs update site
    • URL: one of the following (note: no final slash on the url)
      • http://findbugs.cs.umd.edu/eclipse for official releases
      • http://findbugs.cs.umd.edu/eclipse-candidate for candidate releases and official releases
      • http://findbugs.cs.umd.edu/eclipse-daily for all releases, including developmental ones
    and click OK.
  5. "FindBugs update site" should appear under Sites to include in search
    Click the checkbox next to it to select it, and click Finish.
  6. You should see FindBugs Feature under Select features to install
    (You may have to click on one or two triangles to make it visible in the tree.)
    Select the checkbox next to it and click next.
  7. Select the I accept option to accept the license and click Next.
  8. Make sure the location is correct where you're installing it. The default (your workspace) should be fine. Click Finish.
  9. The plugin is not digitally signed. Go ahead and install it anyway.
  10. Click Yes to make Eclipse restart itself.

它是幹嘛的?

findbugs是一個開源的eclipse 代碼檢查工具;它可以簡單高效全面地幫助我們發現程序代碼中存在的bug,bad smell,以及潛在隱患。針對各種問題,它並且提供了簡單的修改意見供我們重構時進行參考; 通過使用它,可以一定程度上降低我們code review的工作量,並且會提高review效率。 通過findbugs找到bug,再由我們自己重構代碼,可以培養我們的編碼意識及水平,形成好的習慣提高開發編碼能力。


基本用法:

findbugs 簡單易用,按照下圖操作即可;
1, 在eclipse package Explorer 右鍵選擇目標工程-> build project


2, 選擇指定的包或者類進行findbug


此時findbugs會遍歷指定的包或者類,進行分析,找出代碼bug,然後集中顯示在 find bugs的bugs explorer 中,下面我們添加bugs explorer。
3,添加findbugs explorer 
(eclipse 左下角)








3, bugs explorer 添加完畢後,我們就可以查看剛剛找到的bugs了,如圖:



找出的bug有3中顏色, 黑色的臭蟲標誌是分類, 紅色的臭蟲表示嚴重bug發現後必須修改代碼,橘黃色的臭蟲表示潛在警告性bug 儘量修改。(附錄是各種bug的解釋及修改方案,請大家按附表參考修改)
雙擊bug項目就可以在右邊編輯窗口自動打開相關代碼文件並連接到代碼片段。 點擊行號旁邊的小臭蟲圖標後再eclipse下方輸出區將提供詳細的bug描述,以及修改建議等信息。我們可以根據此信息進行修改。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章