FindBugs Detector Implementation (2)

Source: Internet
Author: User

As mentioned in the FINDBUGS detector implementation (1), FindBugs mainly has 5 kinds of detectors, this log introduces FindBugs in class, method, field structure of the detector implementation. As mentioned earlier, stack-based and simple bytecode patterns are inherited from the Opcodestackdetector class, and the Sawopcode method is implemented to detect each byte code.

Generally before writing a detector, we should have a code pattern that we want to detect, but as a learning, here are some simple patterns to use as an example:

    1. The Equals function was overridden, but the hashcode function was not overridden.
    2. The Hashcode function is spelled as hashcode.

FindBugs in class, method, field level detector implementation to inherit the Preorderdetector class, simply introduce some of the classes:

Preordervisitor Preordervisitor inherits Bettervisitor, and on the basis of Bettervisitor, adds a number of methods to get information from the class, method, or field that is currently accessed. In addition, the class also provides some visitafter methods to implement the order of access, in the following example, we will see its role. Like the following method Preordervisitor there are too many, here is not listed, there may be some methods can not be directly aware of its role, most likely we do not understand some of the Java features of the sake of:
 Public fielddescriptor getfielddescriptor ()  Public methoddescriptor getmethoddescriptor ()  Public classdescriptor getclassdescriptor ()  Public @SlashedClassName  String getclassname ()publicvoid  visitafter (Code obj) Public void visitafter (Javaclass obj)

Here's an example.
 Public classDemopluginextendsPreorderdetector {PrivateBugreporter Bugreporter;  PublicDemoplugin (bugreporter reporter) {bugreporter=reporter; }     Public BooleanHashashcode;  Public Booleanhasequal;  Publicmethodannotation equals; @Override Public voidvisit (Javaclass obj) {System.out.println ("Visit class!!!!!!!!!" +obj.getclassname ()); Hashashcode=false; Hasequal=false; equals=NULL; } @Override Public voidvisit (Method me) {if(!me.ispublic ())return; if("Equals". Equals (Me.getname ()) && "(ljava/lang/object;) Z". Equals (Me.getsignature ())) {hasequal=true; System.out.println ("Find Equals () method"); equals= Methodannotation.fromvisitedmethod ( This); }                if("Hashcode". Equals (Me.getname ()) && "() I". Equals (Me.getsignature ())) {Hashashcode=true; System.out.println ("Find Hashcode () method"); }                if("Hashcode". Equals (Me.getname ()) && "() I". Equals (Me.getsignature ())) {Bugreporter.reportbug (NewBuginstance ( This, "Hashcode wrong SPELL", normal_priority). AddClass (Getdottedclassname ()). Addmethod (Methodannotation.fromvisitedmethod ( This))); }} @Override Public voidvisitafter (Javaclass obj) {if(Hasequal &&!)Hashashcode) {Buginstance instance=NewBuginstance ( This, "EQUALS without Hashcode", normal_priority).            AddClass (Getdottedclassname ()). Addmethod (equals);        Bugreporter.reportbug (instance); }    }}

Here we inherit Preorderdetector, and Preorderdetector inherits Preordervisitor because Buginstance needs to use detector for instance, And Preorderdetector implements the detector interface.

The above method we used the last variable hashashcode,hasequals and equals, the first two variables to save the current access to the class exists hashcode and Equals method, the third variable holds the method comments (to report a bug, here first regardless).

Each of the three variables is reset in the visit (Javaclass obj) method each time, so that all classes are instrumented. The visit method is used to determine if the Equals, Hashcode, hashcode methods are used, and the corresponding state is modified. If this is the Hashcode method, the bug is reported directly.

When the Visitafter (Javaclass obj) method executes, the current class is already instrumented, so that you can determine whether the Equals method is present, and there is no case of the Hashcode method.

By adding Fandbugs.xml and Massage.xml to your project, you can detect the patterns that we define.

The above is only a general step to customize the detector on the class, method, field level, examples are only to illustrate the process, as long as we are familiar with the pattern we want to detect, can be achieved through the above method.

FindBugs Detector Implementation (2)

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.