• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Jelly#布丁
我希望世界上不再有战争,我希望和我爱的人永远在一起
博客园    首页    新随笔    联系   管理     

findbug

(1)
findbugs是一个开源的eclipse代码检查工具;它可以简单高效全面地帮助我们发现程序代码中存在的bug,badsmell,以及潜在隐患。针对各种问题,它并且提供了简单的修改意见供我们重构时进行参考; 通过使用它,可以一定程度上降低我们codereview的工作量,并且会提高review效率。通过findbugs找到bug,再由我们自己重构代码,可以培养我们的编码意识及水平,形成好的习惯提高开发编码能力。
 
Findbugs与PMD,checkStyle的区别:
Findbugs主要着眼于寻找代码中的缺陷,这就与其他类似工具有些区别了,比如说Checkstyle和PMD就是根据样式(style)来改进代码质量的。FindBugs分析引擎与Checkstyle和PMD也有所不同,因为它直接操作类文件(class文件)而不是源代码。
 
(2)
在eclipse中安装findbug插件:
下载地址:
http://downloads.sourceforge.net/project/findbugs/findbugs%20eclipse%20plugin/1.3.9/edu.umd.cs.findbugs.plugin.eclipse_1.3.9.20090821.zip?use_mirror=ncu

1,把下载的压缩包解压后,把plugin包copy到eclipse的plugin目录中去;
2,重新启动eclipse
3,打开eclipse->window->Preferences,搜索关键字findbugs,如果能找到配置项,那么表示安装成功,如下图.

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)
               o http://findbugs.cs.umd.edu/eclipse for official releases
               o http://findbugs.cs.umd.edu/eclipse-candidate for candidate releases and official releases
               o http://findbugs.cs.umd.edu/eclipse-daily for all releases, inculding 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.


(3)
findbugs 简单易用,按照下图操作即可: 选择指定的包或者类进行findbug.
此时findbugs会遍历指定的包或者类,进行分析,找出代码bug,然后集中显示在 find bugs 的bugs explorer 中,下面我们添加bugs explorer。
bugs explorer 添加完毕后,我们就可以查看刚刚找到的bugs了,如下图.
找出的bug有3中颜色, 黑色的臭虫标志是分类, 红色的臭虫表示严重bug发现后必须修改代码,橘黄色的臭虫表示潜在警告性bug 尽量修改。(附录是各种bug的解释及修改方案,请大家按附表参考修改)
双击bug项目就可以在右边编辑窗口自动打开相关代码文件并连接到代码片段。 点击行号旁边的小臭虫图标后再eclipse下方输出区将提供详细的bug描述,以及修改建议等信息。我们可以根据此信息进行修改。
(4)
ant中使用findBugs:
从http://findbugs.sourceforge.net/downloads.html下载最新版本的Findbugs,目前的版本是1.3.0, 于2007年11
月8日发布。把解压后目录复制到项目的lib目录下,然后就可以和Ant配合使用了。FindBugs工作在j2se1.4.0或以
后的版本中,需要至少256MB内存。
8.1
在Ant脚本中,首先定义Findbugs的解压目录位置:
<path id="findbugs.path" >
     <fileset  dir ="${lib.home}/findbugs-1.3.0">
        <include  name ="**/*.jar"/>
     </fileset>
</path>
8.2
接着声明Findbugs任务:
<taskdef name="findbugs" classpathref="findbug.path"
classname="edu.umd.cs.findbugs.anttask.FindBugsTask" />
8.3
建立任务:
 <target name="findbug" depends="zip"> 
  <delete dir="${findbugs.result}" />
  <mkdir dir="${findbugs.result}" />
  <findbugs home="${findbugs.path}" output="xml:withMessages" outputFile="${findbugs.result}\bug.xml"
jvmargs="-Xmx1000m" timeout="6000000"> 
<!--  以下定义findbugs查找的类路径  -->
<class location="F:\CruiseControl\projects\DM_Management_Console\assemble\dm.war.zip"></class>
<!--  以下定义源代码的路径  -->
<sourcePath path="F:\CruiseControl\projects\DM_Management_Console"></sourcePath>
<!--  以下定义上述类所依赖的类路径  -->
   <auxClasspath> 
    <fileset dir="${webapp}\lib"> 
     <include name="*.jar" /> 
    </fileset>
   </auxClasspath>
  </findbugs> 
 </target>

(5)

在生成的html report中,点击每一条bug,可下拉查看当前bug的详细信息.
(6)
使用过滤器
使用过滤器我们就可以定义使用哪些bug检测器和针对哪些类进行检查,因为一旦项目比较庞大,那查看冗长的bug
报告也是十分痛苦的事情。使用过滤器,过滤器用来包含或排除特殊的bug报告。这样做有助于在特定的时间段内,
聚焦我们的关注点。过滤器实际是在一个xml文件定义的,xml配置文件的内容如下:
<FindBugsFilter>
     <!--  该类使用所有的bug检测器  -->
     <Match>
       <Class name="com.foobar.MyClass" />
     </Match>  
     <!--  该类使用bugcode为HE的检测器  -->
     <Match class ="com.foobar.BClass">
         <BugCode  name ="HE"/>
     </Match>
     <!--  该类通过指定缩写名使用一些bug检测器  -->
     <Match>
       <Class name="com.foobar.MyClass"/ >
       <Bug code="DE,UrF,SIC" />
     </Match>
</FindBugsFilter>
在findBugs定义属性时,加上相应的过滤器,如下:
<findbugs home ="${findbugs.home}" includeFilter="${findbugs_include_filter}"
excludeFilter="${findbugs_exclude_filter}" ...>
具体可参考:
http://hulongzhou.blog.hexun.com/33024717_d.html
posted on 2010-05-26 09:59  Jelly#布丁  阅读(1579)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3