Adding custom detectors to findbugs

I am trying to add a detector that will detect the occurrence of System.out.println (). As explained in this post, I wrote a detector class, findbugs.xml file and messages.xml file. I created a jar that contains my detector class, findbugs.xml and messages.xml files. I added this jar to the eclipse environment (window-> preferences-> java-> findbugs-> Plugins and misc. Settings). But it shows an 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 fix this?

+5
source share
1 answer

The error is related to the package hierarchy. My detector class was inside the findbugs.custom.detector package, but when I created the jar (using eclipse), I selected only the files I needed (findbugs.xml, messages.xml, detector class). Consequently, package information was not included in the bank. Our XML files read the detector class using the class attribute of the Detector tag, whose value is findbugs.custom.detector.MyDetectorClass . Therefore, when XML files try to read the detector class, they cannot find the findbugs.custom.detector package.

To create a jar with information about the package in it, select the entire project, and then create a jar with the necessary files.

+1
source

All Articles