亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

從定義最簡單Findbugs Detector做起

系統 1872 0

創建Eclipse項目

在eclipse中新建一個Java項目,使用jdk 1.7,從版本3.0開始Findbugs要求Java 7。項目名稱符合Java項目名即可,這里以FB Plugin為例。

clip_image001

設置CLASSPATH

為了開發一個detector,我們需要對FindBugs的detector進行擴展,這里要使用到FindBugs的一些jar包。下載最新版本的FindBugs,在lib目錄下找到findbugs.jar, bcel.jar,并將其添加到項目FB Plugin 的Build Path中。

clip_image003

編寫BUG Detector

這里介紹一個FindBugs的例子(DMI_BIGDECIMAL_CONSTRUCTED_FROM_DOUBLE),用來檢測BigDecimal實例使用Double進行構造。

      BigDecimal a = 
      
        new
      
       BigDecimal(0.1);
    

實際a值為:0.1000000000000000055511151231257827021181583404541015625。

具體代碼如下:

      
        package
      
      
         com.cqu.edu.test;




      
      
        import
      
      
         java.math.BigDecimal;




      
      
        import
      
      
         edu.umd.cs.findbugs.BugInstance;


      
      
        import
      
      
         edu.umd.cs.findbugs.BugReporter;


      
      
        import
      
      
         edu.umd.cs.findbugs.OpcodeStack;


      
      
        import
      
      
         edu.umd.cs.findbugs.bcel.OpcodeStackDetector;




      
      
        public
      
      
        class
      
       DetectorTutorial 
      
        extends
      
      
         OpcodeStackDetector {

    

    
      
      
        private
      
      
         BugReporter bugReporter;



    
      
      
        public
      
      
         DetectorTutorial(BugReporter bugReporter) {

        
      
      
        this
      
      .bugReporter =
      
         bugReporter;

    }



    @Override

    
      
      
        public
      
      
        void
      
       sawOpcode(
      
        int
      
      
         seen) {

        
      
      
        //
      
      
         TODO Auto-generated method stub
      
      
        if
      
       (seen == INVOKESPECIAL && getClassConstantOperand().equals("java/math/BigDecimal"
      
        )

                
      
      && getNameConstantOperand().equals("<init>") && getSigConstantOperand().equals("(D)V"
      
        )) {

            OpcodeStack.Item top 
      
      = stack.getStackItem(0
      
        );

            Object value 
      
      =
      
         top.getConstant();

            
      
      
        if
      
       (value 
      
        instanceof
      
      
         Double) {

                
      
      
        double
      
       arg =
      
         ((Double) value).doubleValue();

                String dblString 
      
      =
      
         Double.toString(arg);

                String bigDecimalString 
      
      = 
      
        new
      
      
         BigDecimal(arg).toString();

                
      
      
        boolean
      
       ok = dblString.equals(bigDecimalString) || dblString.equals(bigDecimalString + ".0"
      
        );



                
      
      
        if
      
       (!
      
        ok) {

                    
      
      
        boolean
      
       scary = dblString.length() <= 8 && dblString.toUpperCase().indexOf("E") == -1
      
        ;

                    bugReporter.reportBug(
      
      
        new
      
       BugInstance(
      
        this
      
      , "TUTORIAL_BUG", scary ?
      
         NORMAL_PRIORITY : LOW_PRIORITY)

                            .addClassAndMethod(
      
      
        this
      
      ).addString(dblString).addSourceLine(
      
        this
      
      
        ));

                }

            }

        }

    }



}
      
    

部署和測試

FindBugs的插件是一個jar包,但至少需要包含findbugs.xml兩個messages.xml文件,將這兩個文件置于FB Plugin項目的根目錄下。

findbugs.xml告訴告訴FindBugs你的插件的內容,及如何加載它:

      
        <
      
      
        FindbugsPlugin 
      
      
        xmlns:xsi
      
      
        ="http://www.w3.org/2001/XMLSchema-instance"
      
      
        

        xsi:noNamespaceSchemaLocation
      
      
        ="findbugsplugin.xsd"
      
      
         

        pluginid
      
      
        ="com.cqu.edu.test.pluginTest"
      
      
        

        provider
      
      
        ="FindBugs Test"
      
      
         

        website
      
      
        ="http://findbugs.sourceforge.net"
      
      
        >
      
      
        <
      
      
        Detector 
      
      
        class
      
      
        ="com.cqu.edu.test.DetectorTutorial"
      
      
         reports
      
      
        ="TUTORIAL_BUG"
      
      
        />
      
      
        <
      
      
        BugPattern 
      
      
        type
      
      
        ="TUTORIAL_BUG"
      
      
         abbrev
      
      
        ="TU"
      
      
         category
      
      
        ="CORRECTNESS"
      
      
        />
      
      
        </
      
      
        FindbugsPlugin
      
      
        >
      
    

messages.xml包括了該插件的英文描述,和其報告的bug pattern:

      
        <?
      
      
        xml version="1.0" encoding="UTF-8"
      
      
        ?>
      
      
        <
      
      
        MessageCollection 
      
      
        xmlns:xsi
      
      
        ="http://www.w3.org/2001/XMLSchema-instance"
      
      
        

        xsi:noNamespaceSchemaLocation
      
      
        ="messagecollection.xsd"
      
      
        >
      
      
        <
      
      
        Plugin
      
      
        >
      
      
        <
      
      
        ShortDescription
      
      
        >
      
      FindBugs Plugin Tutorial Plugin
      
        </
      
      
        ShortDescription
      
      
        >
      
      
        <
      
      
        Details
      
      
        >
      
      Provides detectors as part of the FindBugs detector plugin tutorial.
      
        </
      
      
        Details
      
      
        >
      
      
        </
      
      
        Plugin
      
      
        >
      
      
        <
      
      
        Detector 
      
      
        class
      
      
        ="com.cqu.edu.test.DetectorTutorial"
      
      
        >
      
      
        <
      
      
        Details
      
      
        >
      
      
        

                        Finds instances of BigDecimals being created

                        with doubles.

                
      
      
        </
      
      
        Details
      
      
        >
      
      
        </
      
      
        Detector
      
      
        >
      
      
        <
      
      
        BugPattern 
      
      
        type
      
      
        ="TUTORIAL_BUG"
      
      
        >
      
      
        <
      
      
        ShortDescription
      
      
        >
      
      BigDecimal created from double
      
        </
      
      
        ShortDescription
      
      
        >
      
      
        <
      
      
        LongDescription
      
      
        >
      
      BigDecimal created from double in {1}
      
        </
      
      
        LongDescription
      
      
        >
      
      
        <
      
      
        Details
      
      
        >
      
      
        <![CDATA[
      
      
        

<p>Due to the way double-precision floating point values are represented in Java, creating a new BigDecimal from a double is unreliable and may produce surprising results.</p>


      
      
        ]]>
      
      
        </
      
      
        Details
      
      
        >
      
      
        </
      
      
        BugPattern
      
      
        >
      
      
        <
      
      
        BugCode 
      
      
        abbrev
      
      
        ="TU"
      
      
        >
      
      Tutorial
      
        </
      
      
        BugCode
      
      
        >
      
      
        </
      
      
        MessageCollection
      
      
        >
      
    

將FB Plugin項目build為jar包,勾選圖中文件:

clip_image004

將打包的jar文件放到FindBugs的plugin目錄下,在運行FindBugs時即可被調用。

原文鏈接: https://code.google.com/p/findbugs/wiki/DetectorPluginTutorial

從定義最簡單Findbugs Detector做起


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 人人干人人干 | 久久99久久精品97久久综合 | 久久资源在线 | 欧美亚洲国产精品第一页 | 中文字幕免费在线播放 | 色妇色综合久久夜夜 | 99久久精品一区二区三区 | 亚洲精品乱码久久久久久蜜桃欧美 | 999国产视频 | 日本一级高清不卡视频在线 | 午夜国产精品福利在线观看 | 亚洲加勒比久久88色综合1 | 亚洲综合视频 | 亚洲大片免费 | 最近中文日本字幕免费完整 | 99久久精品国产高清一区二区 | 一道本免费视频 | 五月色婷婷琪琪综合伊人 | 欧美成人私人视频88在线观看 | 亚洲精品久久9热 | 欧美成人午夜视频在线观看 | 国产精品一级香蕉一区 | 真实子伦视频不卡 | 国产大陆亚洲精品国产 | 亚洲精品久久国产小说 | h片在线 | 一区二区三区四区亚洲 | 欧美成人h| 亚洲欧美另类国产综合 | 99视频在线看 | 成人国产三级在线播放 | 亚洲综合免费 | 在线免费黄色片 | 99久久在线| 久久国产精品免费观看 | 午夜一级福利 | 久草视频在线网 | 国产在线精品一区二区高清不卡 | 欧美99视频| 亚洲欧美国产高清va在线播放 | 久久久伊香蕉网站 |