时间:2021-07-01 10:21:17 帮助过:8人阅读
参考:http://blog.csdn.net/franklies/article/details/6830534
https://www.ibm.com/developerworks/cn/java/j-findbug2/
至于里面的一些相关逻辑不在此探讨之内;
2 findbugs.xml
<FindbugsPlugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="findbugsplugin.xsd" pluginid="com.test.findbugs" defaultenabled="true" provider="test"> <Detector class="com.test.findbugs.ForbidSystemOutClass" speed="fast" /> <BugPattern type="SYSTEM_OUT_ERROR" abbrev="SFC" category="PERFORMANCE" /> </FindbugsPlugin>View Code
3 messages.xml
<?xml version="1.0" encoding="UTF-8"?> <MessageCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="messagecollection.xsd"> <Detector class="com.test.findbugs.ForbidSystemOutClass" > <Details> <![CDATA[ <p>detector decription ]]> </Details> </Detector> <BugPattern type="SYSTEM_OUT_ERROR"> <ShortDescription>short decription</ShortDescription> <LongDescription>long decription</LongDescription> <Details> <![CDATA[ <p> System.out can‘t be released, you must delete it. ]]> </Details> </BugPattern> <BugCode abbrev="SFC">System.out can‘t allow</BugCode> </MessageCollection>View Code
参考:http://www.warski.org/staticaccess.html,这两个xml文件直接是从这个开源工程里面拿出来修改的
自定义检测规则主要有这三个步骤,写完这三个,剩下的工作就是编译配置;
二、编译配置
主要是编写ant脚本
<?xml version="1.0" encoding="UTF-8"?> <project name="typestatechecker" default="dist" basedir="."> <description>Builds the Systemout Checker.</description> <property file="build.properties" /> <property name="src" value="src"/> <property name="etc" value="etc"/> <property name="bin" value="bin"/> <property name="dist.file" value="testFindBugs.jar"/> <target name="clean" description="Remove generated files"> <delete dir="${bin}"/> </target> <target name="prep" depends="clean" description="Create required directories"> <mkdir dir="${bin}"/> </target> <path id="build.src.main.path"> <pathelement location="${findbugs.home}/lib/findbugs.jar" /> </path> <target name="build" depends="prep" description="Compile files"> <javac srcdir="${src}" classpathref="build.src.main.path" destdir="${bin}" /> </target> <target name="dist" depends="build" description="Create jar file"> <jar destfile="${findbugs.home}/plugin/${dist.file}"> <fileset dir="${bin}" /> <fileset dir="${src}" /> <fileset dir="${etc}" /> </jar> </target> </project>View Code
使用ant命令:
ant dist
编译过程如下:
在ant配置脚本中已经默认把生成的testFindBugs.jar包放到findbugs安装目录里面的plugin中,对于自定义的规则,打成jar包后,放在这个plugin目录就会自动生效,而不是像网上介绍的那样放在findbugs.jar包中。
三、检测
<?xml version="1.0" encoding="UTF-8"?> <project name="typestatechecker" default="dist" basedir="."> <description>Builds the Systemout Checker.</description> <property file="build.properties" /> <property name="src" value="src"/> <property name="etc" value="etc"/> <property name="bin" value="bin"/> <property name="dist.file" value="testFindBugs.jar"/> <target name="clean" description="Remove generated files"> <delete dir="${bin}"/> </target> <target name="prep" depends="clean" description="Create required directories"> <mkdir dir="${bin}"/> </target> <path id="build.src.main.path"> <pathelement location="${findbugs.home}/lib/findbugs.jar" /> </path> <target name="build" depends="prep" description="Compile files"> <javac srcdir="${src}" classpathref="build.src.main.path" destdir="${bin}" /> </target> <target name="dist" depends="build" description="Create jar file"> <jar destfile="${findbugs.home}/plugin/${dist.file}"> <fileset dir="${bin}" /> <fileset dir="${src}" /> <fileset dir="${etc}" /> </jar> </target> <!-- exec findbugs --> <target name="findbugs" depends="build"> <path id="findbugs_lib" > <fileset dir="${findbugs.home}/lib/" > <include name="*.jar" /> </fileset> </path> <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpathref="findbugs_lib" /> <findbugs home="${findbugs.home}" output="xml" outputFile="${bin}/findbugs_result.xml" > <class location="${bin}" /> <sourcePath path="${src}" /> </findbugs> </target> </project>View Code
使用ant命令:
ant findbugs
过程如下:
ok了,可以在工程的bin目录下,看到findbugs检测生成的结果findbugs_result.xml文件,至此整个过程结束,以上只上讲解了一个配置及过程,至于findbugs规则的具体编写以及findbugs.xml、message.xml里面的每个符号代表的意思没有涉及,向findbugs迈出了一步,接下来还是需要更多的时间摸索。
另外怎么在eclipse的插件中配置自定义的规则还是个问题,试过,可以把规则加进来,但检测的时候却没有生效,目前还没有找到原因。
附上测试工程:
右键保存这张图片到本地,然后把扩展名改成zip,解压就OK了。
findbugs自定义规则并配置实现检测
标签: