Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD/CPD: Ignore bits of code using comments

Is there a way to tell PMD to ignore checking parts of code for duplication?

For example, can I do something like this:

// CPD-Ignore-On
...
// CPD-Ignore-Off

Currently I have PMD set up like this using Maven, but don't see any arguments that would like me do what I want unless I am missing something.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <minimumTokens>40</minimumTokens>
                <targetJdk>1.5</targetJdk>
                <ignoreIdentifiers>true</ignoreIdentifiers>
                <ignoreLiterals>true</ignoreLiterals>
            </configuration>
        </plugin>
like image 477
digiarnie Avatar asked Apr 23 '11 23:04

digiarnie


People also ask

What is PMD CPD?

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 do I prevent duplicate codes?

Don't Repeat Yourself (DRY): Using DRY or Do not Repeat Yourself principle, you make sure that you stay away from duplicate code as often as you can. Rather you replace the duplicate code with abstractions or use data normalization. To reduce duplicity in a function, one can use loops and trees.


1 Answers

After digging around enough, I finally came across it.

By adding the annotations @SuppressWarnings("CPD-START") and @SuppressWarnings("CPD-END") all code within will be ignored by CPD - thus you can avoid false positives.

Source - http://pmd.sourceforge.net/pmd-5.0.5/cpd-usage.html.

like image 109
Avik Avatar answered Oct 05 '22 17:10

Avik