創建Eclipse項目
在eclipse中新建一個Java項目,使用jdk 1.7,從版本3.0開始Findbugs要求Java 7。項目名稱符合Java項目名即可,這里以FB Plugin為例。
設置CLASSPATH
為了開發一個detector,我們需要對FindBugs的detector進行擴展,這里要使用到FindBugs的一些jar包。下載最新版本的FindBugs,在lib目錄下找到findbugs.jar, bcel.jar,并將其添加到項目FB Plugin 的Build Path中。
編寫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包,勾選圖中文件:
將打包的jar文件放到FindBugs的plugin目錄下,在運行FindBugs時即可被調用。
原文鏈接: https://code.google.com/p/findbugs/wiki/DetectorPluginTutorial
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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