Java PMD 和 FindBugs 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4297014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:31:16  来源:igfitidea点击:

What are the differences between PMD and FindBugs?

javastatic-analysisfindbugspmd

提问by Thomas Owens

There was a question comparing PMD and CheckStyle. However, I can't find a nice breakdown on the differences/similarities between PMD and FindBugs. I believe a key difference is that PMD works on source code, while FindBugs works on compiled bytecode files. But in terms of capabilities, should it be an either/or choice or do they complement each other?

有一个比较 PMD 和 CheckStyle问题。但是,我找不到关于 PMD 和 FindBugs 之间差异/相似之处的详细分类。我认为一个关键的区别是 PMD 对源代码有效,而 FindBugs 对编译后的字节码文件有效。但就能力而言,它应该是一个/或一个选择还是它们相互补充?

采纳答案by snakile

I'm using both. I think they complement each other.

我两个都用。我认为它们是相辅相成的。

As you said, PMDworks on source code and therefore finds problems like: violation of naming conventions, lack of curly braces, misplaced null check, long parameter list, unnecessary constructor, missing break in switch, etc. PMD also tells you about the Cyclomatic complexityof your code which I find very helpful (FindBugs doesn't tell you about the Cyclomatic complexity).

正如您所说,PMD对源代码有效,因此会发现诸如:违反命名约定、缺少花括号、空检查错位、参数列表过长、不必要的构造函数、开关中缺少中断等问题。 PMD 还告诉您有关Cyclomatic复杂的代码,我觉得非常有帮助的(FindBugs的不告诉你的圈复杂度)。

FindBugsworks on bytecode. Here are some problems FindBugs finds which PMD doesn't: equals() method fails on subtypes, clone method may return null, reference comparison of Boolean values, impossible cast, 32bit int shifted by an amount not in the range of 0-31, a collection which contains itself, equals method always returns true, an infinite loop, etc.

FindBugs适用于字节码。以下是 FindBugs 发现哪些 PMD 没有发现的一些问题:equals() 方法在子类型上失败,clone 方法可能返回 null,布尔值的引用比较,不可能的强制转换,32 位整数的偏移量不在 0-31 的范围内,包含自身的集合、equals 方法始终返回 true、无限循环等。

Usually each of them finds a different set of problems. Use both. These tools taught me a lot about how to write good Java code.

通常他们每个人都会发现一组不同的问题。两者都用。这些工具教会了我很多关于如何编写好的 Java 代码的知识。

回答by Dekel

The best feature of PMD, is its XPath Rules, bundled with a Rule Designer to let you easily construct new rules from code samples (similar to RegEx and XPath GUI builders). FindBugs is stronger out of the box, but constructing project specific rules and patterns is very important.

PMD 的最佳特性是它的XPath Rules,它与 Rule Designer 捆绑在一起,让您可以轻松地从代码示例中构建新规则(类似于 RegEx 和 XPath GUI 构建器)。FindBugs 开箱即用更强大,但构建项目特定的规则和模式非常重要。

For example, I encountered a performance problem involving 2 nested for loops, resulting in a O(n^2) running time, which could easily be avoided. I used PMD to construct an ad-hoc query, to review other instances of nested for loops - //ForStatement/Statement//ForStatement. This pointed out 2 more instances of the problem. This is not a generic rule whatsoever.

例如,我遇到了一个涉及 2 个嵌套 for 循环的性能问题,导致运行时间为 O(n^2),这很容易避免。我使用 PMD 构建了一个临时查询,以查看嵌套 for 循环的其他实例 - //ForStatement/Statement//ForStatement。这指出了该问题的另外 2 个实例。这不是一个通用的规则。

回答by kunal saxena

PMD is

PMD是

  • famous
  • used widely in industry
  • you can add your rules in xml
  • gives you detailed analysis in Errors levels and warning levels
  • you can also scan your code for "copy and paste lines". Duplicate code. This gives good idea about implementing java oops.
  • 著名的
  • 广泛应用于工业
  • 您可以在 xml 中添加规则
  • 为您提供错误级别和警告级别的详细分析
  • 您还可以扫描“复制和粘贴行”的代码。重复的代码。这提供了关于实现 java oops 的好主意。