(转)定制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 ...
随机推荐
- Nginx图片防盗链的方式
原文:http://www.open-open.com/code/view/1430750263460 location ~* \.(gif|jpg|jpeg|png|ico)$ { valid_re ...
- GitHub+Octopress搭建免费blog
生成github公钥 检查ssh公钥设置: 如果id_rsa*文件不存在,跳到第三步: $ cd .ssh $ ls 备份原来的ssh key: 备份旧数据,备份后删除旧数据: $ mkdir key ...
- PS 基础知识 CMYK全称是什么
已解决 请问谁知道CMYK四色的英文全称? 悬赏分:20 - 解决时间:2006-9-6 16:23 C代表什么颜色?英文全称是什么? M代表什么颜色?英文全称是什么? Y代表什么颜色?英文全称是什么 ...
- cocos2d-x调用android内嵌浏览器打开网页
cocos2d-x调用android内嵌浏览器打开网页,能够从入口传入网址,C++调用android 的api就可以实现. 方法也非常easy 1. 改动"cocos2dx\platform ...
- hql 时间
1.hql中时间格式转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String d ...
- javascript变量初始化位置
变量在之前<script type="text/javascript"></script>(或引用的js文件)中初始化,可以正常访问. 运行程序:弹出123 ...
- 【bzoi2006】【狼抓兔子】【最小割】
Description Source: Beijing2006 [BJOI2006] 八中OJ上本题链接:http://www.lydsy.com/JudgeOnline/problem.php?id ...
- [Maven实战](9)传递性依赖
了解Spring的朋友都知道.创建一个Spring Framework项目都须要依赖什么样的Jar包.假设不使用Maven,那么在项目中就须要手动下载相关的依赖.因为Spring Framework又 ...
- [LeetCode]Insert Interval 考虑多种情况
写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)| (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...
- kubernetes之多容器pod以及通信
系列目录 容器经常是为了解决单一的,窄范围的问题,比如说微服务.然而现实中,一些复杂问题的完成往往需要多个容器.这里我们讨论一下如何把多个容器放在同一个pod里以及容器间的通信 什么是pod pod是 ...