(转)定制findbugs规则
转载自http://www.51testing.com/html/97/13997-211893.html
这类文章极少,字节码操作需要对becl库及jvm字节码操作有一定常识。参考:
http://blog.csdn.net/lywybo/archive/2010/03/01/5335748.aspx
http://javadevelopmentforthemasses.blogspot.com/2008/09/custom-findbugs-detectors-and-maven.html
https://www.ibm.com/developerworks/cn/java/j-findbug2/
ibm介绍的原理实用,但配置过时;支付宝朋友写的message.xml/findbugs.xml不够详细且有笔误。
1.1 准备
下载findbugs:http://sourceforge.net/projects/findbugs/files/findbugs/1.3.9/findbugs-1.3.9.zip/download
修改build.xml ,去除所有的validate依赖。执行ant编译。
eclipse引入findbugs工程。
1.2 实现类
直接在findbugs目录中增加类
packageedu.umd.cs.findbugs.detect;
importorg.apache.bcel.classfile.Code;
importedu.umd.cs.findbugs.BugInstance;
importedu.umd.cs.findbugs.BugReporter;
importedu.umd.cs.findbugs.bcel.OpcodeStackDetector;
/**
*@authorbo
*这个规则类用于判断System.out和System.error这种情况
*/
publicclassForbiddenSystemClassextendsOpcodeStackDetector{
BugReporterbugReporter;
publicForbiddenSystemClass(BugReporter bugReporter) {
this.bugReporter= bugReporter;
}
/**
* visit方法,在每次进入字节码方法的时候调用
*在每次进入新方法的时候清空标志位
*/
@Override
publicvoidvisit(Code obj) {
super.visit(obj);
}
/**
*每扫描一条字节码就会进入sawOpcode方法
*
*@paramseen 字节码的枚举值
*/
@Override
publicvoidsawOpcode(intseen) {
if(seen ==GETSTATIC) {
if(getClassConstantOperand().equals("java/lang/System")
&& (getNameConstantOperand().equals("out") || getNameConstantOperand().equals("error"))) {
BugInstance bug =newBugInstance(this,"ALP_SYSTEMCLASS",NORMAL_PRIORITY).addClassAndMethod(this)
.addSourceLine(this, getPC());
bug.addInt(getPC());
bugReporter.reportBug(bug);
}
}
}
}
1.3 修改etc目录配置文件findbugs.xml和message.xml
不支持中文注释。
在findbugs.xml增加内容。
<Detectorclass="edu.umd.cs.findbugs.detect.ForbiddenSystemClass"
speed="fast"
reports="ALP_SYSTEMCLASS"
hidden="false"/>
<BugPatternabbrev="LIANGJZFORBIDDENSYSTEMCALSS"type="ALP_SYSTEMCLASS"category="EXPERIMENTAL" />
Message.xml增加:
<Detectorclass="edu.umd.cs.findbugs.detect.ForbiddenSystemClass">
<Details>
<![CDATA[
<p>category:detector find System.out/System.error
<p>please use log4j
]]>
</Details>
</Detector>
<BugPatterntype="ALP_SYSTEMCLASS">
<ShortDescription>short desc:System.out/error</ShortDescription>
<LongDescription>class={0},method {1}long desc:System.out,please use log4j</LongDescription>
<Details>
<![CDATA[
<p>detail info see log4j document</p>
]]>
</Details>
</BugPattern>
1.4 用findbugs图形化界面测试
点击bin/finbugs.bat,打开扫描的.class目录。看到扫描带System.out或者System.error的.class放到experimental类错误时,验证成功。
1.5 替换eclipse findbugs-plugin.jar文件
用winrar打开
D:\devtools\eclipse_3.5.1\plugins\edu.umd.cs.findbugs.plugin.eclipse_1.3.9.20090821\findbugs-plugin.jar中message.xml,findbugs.xml,z加入二进制的edu.umd.cs.findbugs.detect.ForbiddenSystemClass。
重启elipse,还需要确保experimental类的错误能在findbugs窗口展现:windows->preferences->java->findbugs->reporter configuration上的experimental选项勾上。
执行findbugs扫描.class,看到结果出现..
(转)定制findbugs规则的更多相关文章
- FindBugs规则整理
http://blog.csdn.net/jdsjlzx/article/details/21472253/ 配置FindBugs和常见FindBugs错误 http://blog.csdn.net/ ...
- findbugs规则
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- EF里如何定制实体的验证规则和实现IObjectWithState接口进行验证以及多个实体的同时验证
之前的Code First系列文章已经演示了如何使用Fluent API和Data Annotation的方式配置实体的属性,比如配置Destination类的Name属性长度不大于50等.本文介绍E ...
- 利用Sonar定制自定义JS扫描规则(三)——SSLR JavaScript Toolkit 使用说明
在上一篇blog中讲了在sonar中如何新增自定义的JS规则,这里面比较难的地方是XPath语句的编写,而要编写正确的XPath语句,首先要拿到语法的AST,下面我们就来介绍如何使用SSLR Java ...
- 配置FindBugs和常见FindBugs错误
配置FindBugs: 在这里可以对FindBugs规则等进行详细设置. 选择你的项目,右键 => Properties => FindBugs => 1 Run Automatic ...
- ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL
ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL 引言--- 在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. ...
- Linux makefile教程之隐含规则九[转]
隐含规则 ———— 在 我们使用Makefile时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix下是[.o] 文件,Windows下是[.o ...
- Makefile详解--隐含规则
Makefile详解--隐含规则(转) Makefile系列文章,这里有个前辈连续洗了一个系列来介绍,共有26篇博客文章. http://www.cppblog.com/ivenher/archive ...
- 很详细、很移动的Linux makefile教程:介绍,总述,书写规则,书写命令,使用变量,使用条件推断,使用函数,Make 的运行,隐含规则 使用make更新函数库文件 后序
很详细.很移动的Linux makefile 教程 内容如下: Makefile 介绍 Makefile 总述 书写规则 书写命令 使用变量 使用条件推断 使用函数 make 的运行 隐含规则 使用m ...
随机推荐
- android-samples-mvp
Model–view–presenter (MVP)介绍 mvp在wiki上的介绍为 Model 定义用户界面所需要被显示的数据模型,一个模型包含着相关的业务逻辑 View View不应该处理业务 ...
- 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- CODECHEF Oct. Challenge 2014 Children Trips
@(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...
- CUDA 实现JPEG图像解码为RGB数据
了解JPEG数据格式的人应该easy想到.其对图像以8*8像素块大小进行切割压缩的方法非常好用并行处理的思想来实现.而其实英伟达的CUDA自v5.5開始也提供了JPEG编解码的演示样例.该演示样例存储 ...
- angular - 安装 -1
在阅读以下教程以前,请安装node,请先确保您的使用平台:Win.Mac.Linux 首次安装node以后,我们先检测版本 node -v npm -v 这样就代表安装成功,那么我们可以进入下一步了 ...
- HDU 3435A new Graph Game(网络流之最小费用流)
题目地址:HDU 3435 这题刚上来一看,感觉毫无头绪. .再细致想想.. 发现跟我做的前两道费用流的题是差点儿相同的. 能够往那上面转换. 建图基本差点儿相同.仅仅只是这里是无向图.建图依旧是拆点 ...
- OA权限树搭建 代码
<ul id="tree"> <s:iterator value="#application.topPrivilegeList"> &l ...
- PC常用电源IC、MOS、三极管、二极管厂家
笔记本常用MOS.三极管.二极管厂家: 1.EMC 杰力电子(台湾)官方网站:http://www.excelliancemos.com/tw/solution.php 2.UBIQ(台湾电源厂家UP ...
- 编译3.10内核 出现错误 “undefined reference to...." 解决方法
向内核中加入C文件后.假设想编译进内核须要改动当前文件夹下的Kconfig文件和Makefile文件. 如:加入一个test.c文件到driver文件夹下,则须要改动Kconfig文件: config ...
- freescale-sdk linux移植一搭建编译环境脚本host-prepare.sh分析
接下来使用自己的课外歇息时间,对基于PowerPC架构freescale-sdk,进行linux移植和分析.主要參考官方文档freescale linux sdk START_HERE.html,首先 ...