Search code examples
reportoutputpmdcpd

How to change PMD's Copy Paste Detector (CPD) report output


I'd like to modify CPD to only spit out the Found a X line (Y tokens) duplication in the following files: ... when generating a report, i.e. suppress the source lines of code. I have the /src/ files and attempted to modify SimpleRenderer.java in /src/net/sourceforge/pmd/cpd/ by commenting out

String source = match.getSourceCodeSlice();

if (trimLeadingWhitespace) {
    String[] lines = source.split("[" + PMD.EOL + "]");
    int trimDepth = StringUtil.maxCommonLeadingWhitespaceForAll(lines);
    if (trimDepth > 0) {
        lines = StringUtil.trimStartOn(lines, trimDepth);
    }
    for (int i=0; i<lines.length; i++) {
        rpt.append(lines[i]).append(PMD.EOL);
    }  
    return;
}

However the report has not changed. I'm a bit of a Java novice, so keep that in mind. Do I need to rebuild the pmd-4.2.x in Eclipse somehow?


Solution

  • There are different ways to achieve this:

    1. Without modifying PMD/CPD at all by using egrep. You can e.g. post-filter the report:

      bin/run.sh cpd --minimum-tokens 100 --files src --encoding UTF-8 \
        | egrep "^Found a |^Starting at line "
      

      This would output now only the lines which start with "Found a " or "Starting at line ".

    2. Modifying PMD/CPD to adjust the report format. I would however suggest, to implement this modified report format as a separate format, e.g. naming it "text_without_sources", rather than changing the default format. You would then call CPD with bin/run.sh cpd --format text_without_sources ....

      In that case, you'll need to build PMD from sources. PMD uses Maven to build (you can use eclipse during development - but the package is built with maven). After a mvn clean package in the top-directory of the cloned sources from https://github.com/pmd/pmd you'll find the binary in the directory pmd-dist/target/.

      Look at how the reports are integrated in CPDConfiguration.java - you can add your own version of the SimpleRenderer.

    3. Create a feature-request at https://sourceforge.net/p/pmd/bugs/