Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD CPD exclude methods like equals and hashcode?

Tags:

java

pmd

cpd

I cannot find an option how to tell PMD-CPD to skip specific methods. We use generated equals() and hashCode() methods, so the methods look often very similar and CPD reports a lot of them as duplicate code.

I can use some //NOPMD comments in the code, but what is in my eyes not a way how to manage my code. Since I incorporate a tool into code what has nothing to do with the code. CPD helps to avoid coding errors/styles and should not force me to modify my code.

So would be really helpful if someone have some ideas how to solve it.

best wishes

like image 979
hkais Avatar asked Nov 10 '22 04:11

hkais


1 Answers

PMD CPD has no such option, so short of filing a feature request, it is not possible.

However, you could use annotations for suppression as described in the CPD docs:

//enable suppression
@SuppressWarnings("CPD-START")
public Object someMethod(int x) throws Exception {
    // any code here will be ignored for the duplication detection
}
//disable suppression
@SuppressWarnings("CPD-END")
public void nextMethod() {
}

Personally, I don't like this syntax very much, because it makes you annotate completely unrelated methods. nextMethod() has nothing to do with someMethod(), but still gets the CPD-END annotation. But it may stil be better than putting a whole lot of //NOPMD comments. It also excludes the method only for CPD, but not for other PMD detectors, as //NOPMD would.

Your initial wish of not putting information for analyis tools into the code is understandable. However, when I think about it, the annotations and comments do say something about the code, so having the code's meta information in the source is not such a bad thing. If you still don't like it, consider using SonarQube or some other tool with a database behind it.

like image 97
barfuin Avatar answered Nov 15 '22 13:11

barfuin