findbugs过滤器规则

Add Package (or Class) Filtering To Your Findbugs Ant Task


Following on from my last post about using hbm2java to generate a @Entity bean DAO layer for our application, Findbugs, the great automatic bug finding tool, will find a lot of issues with Hibernate’s auto-generated code (primarily around exposing internal Date objects to callers). So long as this is something we are aware of and don’t have a problem with, we don’t want find bugs to tell us about this stuff. It’s possible to filter out an entire package or class tree with the use of Findbugs’ filters.

For example, say we wanted to filter out the classes in the package com.mycom.dbautodao, we could create a findbugs_filter.xml which contains:

view plaincopy to clipboardprint?
<FindBugsFilter>
<Match>
<Package name="~com\.mycom\.dbautodao\..*"/>
</Match>
</FindBugsFilter>
<FindBugsFilter>
<Match>
<Package name="~com\.mycom\.dbautodao\..*"/>
</Match>
</FindBugsFilter>
This uses a regular expression to filter out checks against all contents and sub-packages of the com.mycom.dbautodao package.

Similarly it’s possible to do the same with a <class name=”myregex”/> element rather than a <package/> element for finer control.

To enable the filtering we just need modify the tag in our Ant build file to include the filter, e.g.

view plaincopy to clipboardprint?
<findbugs home="${findbugs.home}" output="xml" outputFile="${basedir}/dist/findbugs/findbugs.xml" excludefilter="findbugs_filter.xml">
<findbugs home="${findbugs.home}" output="xml" outputFile="${basedir}/dist/findbugs/findbugs.xml" excludefilter="findbugs_filter.xml">
While on the topic of regular expressions, I often find myself using this online regex checker tool which uses Java’s regex syntax – http://www.fileformat.info/tool/regex.htm. It’s a really quick and handy way of writing and testing a regular expression, since you can enter


在主脚本文件中增加:
<target name="findbugs">
<findbugs home="${findbugs.dir}/findbugs-1.2.1" output="xml" jvmargs="-Xmx256M" projectName="${ant.project.name}"
outputFile="${testreport.dir}/findbugs.xml" excludeFilter="${testreport.dir}/FindBugsFilter.xml">
<class location="${outclasses.dir}" />
</findbugs>
<echo>Findbugs检查代码错误完成!</echo>
</target>

然后指定一个单独的配置文件: FindBugsFilter.xml
内容如下例子:

<FindBugsFilter>
<Match>
<Method name="main" />
</Match>
<Match>
<Class name="~.*\.*Test" />
</Match>
<Match>
<Class name="~.*\.*Value" />
</Match>
<Match>
<Class name="~.*\.*Value\$.*" />
</Match>
<Match>
<Class name="~.*\.*Table" />
</Match>
</FindBugsFilter>
这样会过滤Value和Table,Cache等类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值