findbug
findbugs是一个开源的eclipse代码检查工具;它可以简单高效全面地帮助我们发现程序代码中存在的bug,badsmell,以及潜在隐患。针对各种问题,它并且提供了简单的修改意见供我们重构时进行参考; 通过使用它,可以一定程度上降低我们codereview的工作量,并且会提高review效率。通过findbugs找到bug,再由我们自己重构代码,可以培养我们的编码意识及水平,形成好的习惯提高开发编码能力。
在eclipse中安装findbug插件:
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项目就可以在右边编辑窗口自动打开相关代码文件并连接到代码片段。 点击行号旁边的小臭虫图标后再eclipse下方输出区将提供详细的bug描述,以及修改建议等信息。我们可以根据此信息进行修改。
从http://findbugs.sourceforge.net/downloads.html下载最新版本的Findbugs,目前的版本是1.3.0, 于2007年11
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"
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"
<!-- 以下定义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)
使用过滤器我们就可以定义使用哪些bug检测器和针对哪些类进行检查,因为一旦项目比较庞大,那查看冗长的bug
<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
浙公网安备 33010602011771号