Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 464
Rosdi Kasim Avatar asked Jun 21 '10 02:06

Rosdi Kasim


People also ask

What is CPD in Java?

Duplicate code can be hard to find, especially in a large project. But PMD's Copy/Paste Detector (CPD) can find it for you! CPD works with Java, JSP, C/C++, C#, Go, Kotlin, Ruby, Swift and many more languages. It can be used via command-line, or via an Ant task.

How can I check duplicate code in SonarQube?

SonarQube shows you all the duplications metrics on a project, package, or class basis. Further, you can use the Duplications tab in the file detail view to see the location of a duplication in a class.


2 Answers

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.

like image 182
uthark Avatar answered Sep 21 '22 12:09

uthark


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

like image 36
wsanville Avatar answered Sep 18 '22 12:09

wsanville