java - 在 findbugs 中添加自定义检测器

标签 java findbugs

我正在尝试添加一个检测器来检测 System.out.println() 的出现。如 this 中所述发布后,我已经编写了检测器类、findbugs.xml 文件和 messages.xml 文件。 我创建了一个 jar,其中包含我的检测器类、findbugs.xml 和 messages.xml 文件。我在我的 eclipse 环境中添加了这个 jar(window->preferences->java->findbugs->Plugins and misc.Settings)。但它显示无效条目。

检测器类:

    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 文件:

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

messages.xml 文件:

    <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>

我该如何纠正这个问题?

最佳答案

错误是因为包的层次结构。我的检测器类位于 findbugs.custom.detector 包内,但是当我创建 jar(使用 eclipse)时,我只选择了所需的文件(findbugs.xml、messages.xml、检测器类)。 因此,包信息未包含在 jar 中。我们的 XML 文件使用 Detector 标签的属性 class 读取检测器类,该标签的值为 findbugs.custom.detector.MyDetectorClass。 因此,当 XML 文件尝试读取检测器类时,它们找不到 findbugs.custom.detector 包。

要构建包含包信息的 jar,请选择整个项目,然后创建包含所需文件的 jar。

关于java - 在 findbugs 中添加自定义检测器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30345188/

相关文章:

java - 错误 : java. lang.NoSuchMethodError : javax. persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;

java - 解释大 O 循环和数组

java - FindBugs IntelliJ 和 Maven 中的匹配错误级别

调用native方法时返回java.lang.UnsatisfiedLinkError

java - 安卓 Java : bitmap resize define height and width auto

jdbc - DD异常,清理数据库资源: is there a clean solution?

java - 查找非线程安全单例 spring 实现的代码质量规则

java - 数组的长度属性在哪里定义?

java - Findbugs 误报 : Dereference of the result of readLine() without nullcheck