From defining the simplest findbugs detector

Source: Internet
Author: User

Create Eclipse Project

Creating a new Java project in Eclipse, using JDK 1.7, starting with version 3.0, FindBugs requires Java 7. The project name conforms to the Java project name, here is an example of FB plugin.

Set Classpath

In order to develop a detector, we need to extend the detector of findbugs, which uses some jar packages to findbugs. Download the latest version of FindBugs, locate Findbugs.jar, Bcel.jar in the Lib directory, and add it to the project FB Plugin build path.

Write a bug Detector

Here is an example of a findbugs (dmi_bigdecimal_constructed_from_double) that detects that the BIGDECIMAL instance is constructed using a DOUBLE.

New BigDecimal (0.1);

The actual a value is: 0.1000000000000000055511151231257827021181583404541015625.

The specific code is as follows:

 Packagecom.cqu.edu.test;ImportJava.math.BigDecimal;Importedu.umd.cs.findbugs.BugInstance;ImportEdu.umd.cs.findbugs.BugReporter;ImportEdu.umd.cs.findbugs.OpcodeStack;ImportEdu.umd.cs.findbugs.bcel.OpcodeStackDetector; Public classDetectortutorialextendsOpcodestackdetector {PrivateBugreporter Bugreporter;  Publicdetectortutorial (Bugreporter bugreporter) { This. Bugreporter =Bugreporter; } @Override Public voidSawopcode (intseen) {        //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(ValueinstanceofDouble) {                Doublearg =((Double) value). Doublevalue (); String dblstring=double.tostring (ARG); String bigdecimalstring=NewBigDecimal (ARG). toString (); BooleanOK = dblstring.equals (bigdecimalstring) | | Dblstring.equals (bigdecimalstring + ". 0"); if(!OK) {                    BooleanScary = Dblstring.length () <= 8 && dblstring.touppercase (). IndexOf ("E") = = 1; Bugreporter.reportbug (NewBuginstance ( This, "Tutorial_bug", scary?normal_priority:low_priority). Addclassandmethod ( This). AddString (dblstring). Addsourceline ( This)); }            }        }    }}
Deployment and Testing

FindBugs plug-in is a jar package, but need to include at least findbugs.xml two messages.xml files, put these two files in the FB plugin project root directory.

Findbugs.xml tells FindBugs the content of your plugin and how to load it:

<FindbugspluginXmlns: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">        <Detectorclass= "Com.cqu.edu.test.DetectorTutorial"reports= "Tutorial_bug" />        <Bugpatterntype= "Tutorial_bug"abbrev= "TU"category= "correctness"/></Findbugsplugin>

Messages.xml includes the English description of the plugin, and its reported bug pattern:

<?XML version= "1.0" encoding= "UTF-8"?><messagecollectionXmlns: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>        <Detectorclass= "Com.cqu.edu.test.DetectorTutorial">                <Details>Finds instances of Bigdecimals being created with doubles. </Details>        </Detector>        <Bugpatterntype= "Tutorial_bug">                <shortdescription>BigDecimal created from Double</shortdescription>                <longdescription>BigDecimal created from double in {1}</longdescription>                <Details><! [Cdata[<p>due to the ' double-precision floating point values is represented in Java, creating a new BigDecimal from A double is unreliable and may produce surprising results.</p>]]>                </Details>        </Bugpattern>        <Bugcodeabbrev= "TU">Tutorial</Bugcode></messagecollection>

Build the FB plugin project into a jar package and tick the files in the diagram:

The packaged jar file is placed in the plugin directory of the FindBugs and is called when the findbugs is run.

Original link: https://code.google.com/p/findbugs/wiki/DetectorPluginTutorial

From defining the simplest findbugs detector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.