Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom detectors in findbugs

Tags:

java

findbugs

I am trying to add a detector which will detect System.out.println() occurrences. As explained in this post, I've written the detector class, findbugs.xml file and messages.xml file. I created a jar which contains my detector class, findbugs.xml and messages.xml files. I added this jar in my eclipse environment (window->preferences->java->findbugs->Plugins and misc. Settings). But it is showing invalid entry.

Detector class:

    package findbugs.custom.detector;
    import edu.umd.cs.findbugs.BugInstance;
    import edu.umd.cs.findbugs.BugReporter;
    import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
    import edu.umd.cs.findbugs.classfile.ClassDescriptor;
    import edu.umd.cs.findbugs.classfile.FieldDescriptor;
    public class CallToSystemOutPrintlnDetector2 extends OpcodeStackDetector {


private BugReporter bugReporter;


public CallToSystemOutPrintlnDetector2(BugReporter bugReporter) {
    super();
    this.bugReporter = bugReporter;

}


public void sawOpcode(int seen) {
    if (seen == GETSTATIC){

        try {
            FieldDescriptor operand = getFieldDescriptorOperand();
            ClassDescriptor classDescriptor = operand.getClassDescriptor();
            if ("java/lang/System".equals(classDescriptor.getClassName()) && 
                    ("err".equals(operand.getName())||"out".equals(operand.getName()))) {
                reportBug();
            }
        } catch (Exception e) {
            //ignore
        }
    }
}

private void reportBug(){
    this.bugReporter.reportBug(getBugInstance());
}


private BugInstance getBugInstance() {
    return new BugInstance(this, "MY_CALL_TO_SYSTEM_OUT_BUG", 10)
        .addClassAndMethod(this)
        .addSourceLine(this);
}
}

findbugs.xml file:

    <FindbugsPlugin>
    <Detector class="findbugs.custom.detector.CallToSystemOutPrintlnDetector2" speed="fast" />
    <BugPattern abbrev="SYS_OUT_P" type="CALL_TO_SYSTEM_OUT" category="CORRECTNESS" />
    </FindbugsPlugin>

messages.xml file:

    <MessageCollection>
    <Detector class="findbugs.custom.detector.CallToSystemOutPrintlnDetector2">
    <Details>
    <![CDATA[
    <p>This detector warns about SYS_OUTs used in the code. It is a fast detector.</p>
    ]]>
    </Details>
    </Detector>
    <BugPattern type="CALL_TO_SYSTEM_OUT_BUG">
    <ShortDescription>sysout detector</ShortDescription>
    <LongDescription>Found sysout in {1}</LongDescription>
    <Details>
    <![CDATA[
    <p>This is a call to System.out.println/err method. </p>
    which should be avoided.
    ]]>
    </Details>
    </BugPattern>
    <BugCode abbrev="SYS_OUT_P">Found sysout</BugCode>
    </MessageCollection>

How can I correct this?

like image 408
Manoj Avatar asked Nov 09 '22 13:11

Manoj


1 Answers

The error is because of the package hierarchy. My detector class was inside findbugs.custom.detector package, but when I created the jar (using eclipse) I was only selecting the required files (findbugs.xml, messages.xml, detector class). Hence the package information was not included in the jar. Our XML files read the detector class using the attribute class of the Detector tag whose value was findbugs.custom.detector.MyDetectorClass. So when XML files try to read the detector class, they could not find findbugs.custom.detector package.

To build a jar with the package information in it, select the whole project and then create a jar with required files.

like image 164
Manoj Avatar answered Nov 15 '22 06:11

Manoj