检测重复代码的工具 (Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3081748/
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
Tools to detect duplicated code (Java)
提问by Rosdi Kasim
I am in a project where previous programmers have been copy-pasting codes all over the place. These codes are actually identical (or very similar) and they could have been refactored into one.
我在一个项目中,以前的程序员一直在到处复制粘贴代码。这些代码实际上是相同的(或非常相似的),它们可以被重构为一个。
I have spent countless hours refactoring these codes manually but I think there must be a better way. Some are very trivial static methods that could have been moved into an ancestor class (but instead was copy pasted all over by previous junior programmers).
我花了无数个小时手动重构这些代码,但我认为必须有更好的方法。有些是非常微不足道的静态方法,它们本可以移到祖先类中(但被以前的初级程序员复制粘贴了一遍)。
Is there a code analysis tool that can detect this and provide reports/recommendations? I prefer free/open source tool if possible.
是否有代码分析工具可以检测到这一点并提供报告/建议?如果可能,我更喜欢免费/开源工具。
采纳答案by uthark
I use the following tools:
我使用以下工具:
- PMD/CPD(BSD-style License).
Checkstyle(LGPL License)- support was removed, see details.
- PMD/CPD(BSD 风格的许可证)。
Checkstyle(LGPL 许可证)- 支持已删除,请参阅详细信息。
Both tools have code duplication detection support. But both of them lack the ability to advise you how to refactor your code.
这两种工具都支持代码重复检测。但是他们都缺乏建议您如何重构代码的能力。
JetBrains IntelliJ IDEA Ultimate has good static code analysis with code duplication support, but it is not free.
JetBrains IntelliJ IDEA Ultimate 具有良好的静态代码分析和代码复制支持,但它不是免费的。
回答by wsanville
Most of the tools listed on the Wikipedia article on Duplicate Code Toolswill detect duplicates in many different languages, including Java.
回答by Pascal Thivent
回答by Nikolaus Gradwohl
http://checkstyle.sourceforge.net/has support for finding duplicates
回答by Ira Baxter
See our SD Java CloneDR, a tool for detecting exact and near-miss duplicate code in large Java systems.
请参阅我们的SD Java CloneDR,这是一种用于在大型 Java 系统中检测准确和几乎未命中的重复代码的工具。
The CloneDR will find code clones in spite of whitespace changes, line breaks, comment insertions deletions, modification of constants or identifiers, and in a number of cases, even replacement of one statement by another or a block of statements.
尽管有空格更改、换行符、注释插入删除、常量或标识符修改,并且在许多情况下,甚至可以用另一个语句或语句块替换一个语句,但 CloneDR 仍会找到代码克隆。
It shows where each set of clones is found, each individual clone, an abstraction of the clones having their shared commonality and parameterization of the abstraction to show how each clone instance can be derived from the abstraction.
它显示了每组克隆的位置、每个单独的克隆、具有共享共性的克隆的抽象以及抽象的参数化以显示如何从抽象派生每个克隆实例。
It finds 10-20% clones in most Java systems.
它在大多数 Java 系统中找到 10-20% 的克隆。
回答by pepe
SonarQube can detect duplicated codes but does not give recommendation on eliminating them. It is free and - although with the default setup it can only detect lexically identical clones
SonarQube 可以检测重复代码,但不提供消除它们的建议。它是免费的 - 尽管在默认设置下它只能检测词法相同的克隆