Tools to detect duplicated code (Java) [closed]

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.


Solution 1:

I use the following tools:

  • PMD/CPD (BSD-style License).
  • Checkstyle (LGPL License) - support was removed, see details.

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.

Solution 2:

Most of the tools listed on the Wikipedia article on Duplicate Code Tools will detect duplicates in many different languages, including Java.