findbugs自定義規則並配置實現檢測


findbugs不過多介紹了,對於這個主題找了一些資料,沒有找到一個完整的介紹,要么是介紹怎么寫detector,要么就是沒有完整的介紹怎么配置生效,下面主要介紹一下怎么配置其生效,至於怎么寫這個detector還是有很多資料說明的,不過在些也重復一下。

一、自定義detector

1 ForbidSystemOutClass檢測類

package com.test.findbugs;  
  
import org.apache.bcel.classfile.Code;

import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
  
public class ForbidSystemOutClass extends OpcodeStackDetector {  
    
    private BugReporter bugReporter;  
  
    public ForbidSystemOutClass(BugReporter bugReporter) {  
        this.bugReporter = bugReporter;  
    }  
  
    @Override  
    public void visit(Code obj) {  
        super.visit(obj);  
    }  
  
    @Override  
    public void sawOpcode(int seen) {  

        if (seen == GETSTATIC) {
            
            if ("java/lang/System".equals(getClassConstantOperand())
                    && ("out".equals(getNameConstantOperand()) || "error".equals(getNameConstantOperand()))) {
                
                bugReporter.reportBug( new BugInstance("SYSTEM_OUT_ERROR", HIGH_PRIORITY) 
                                        .addClassAndMethod(this).addSourceLine(this)); 
            }  
        }  
  
  
    }  
}  
View Code

參考: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的插件中配置自定義的規則還是個問題,試過,可以把規則加進來,但檢測的時候卻沒有生效,目前還沒有找到原因。

 附上demo:

右鍵保存這張圖片到本地,然后把擴展名改成zip,解壓就OK了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM