net.sourceforge.pmd.cpd.SimpleRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pmd-core Show documentation
Show all versions of pmd-core Show documentation
PMD is an extensible multilanguage static code analyzer. It finds common programming flaws like unused variables,
empty catch blocks, unnecessary object creation, and so forth. It's mainly concerned with Java and
Apex, but supports 16 other languages. It comes with 400+ built-in rules. It can be
extended with custom rules. It uses JavaCC and Antlr to parse source files into abstract syntax trees
(AST) and runs rules against them to find violations. Rules can be written in Java or using a XPath query.
Currently, PMD supports Java, JavaScript, Salesforce.com Apex and Visualforce,
Kotlin, Swift, Modelica, PLSQL, Apache Velocity, JSP, WSDL, Maven POM, HTML, XML and XSL.
Scala is supported, but there are currently no Scala rules available.
Additionally, it includes CPD, the copy-paste-detector. CPD finds duplicated code in
Coco, C/C++, C#, Dart, Fortran, Gherkin, Go, Groovy, HTML, Java, JavaScript, JSP, Julia, Kotlin,
Lua, Matlab, Modelica, Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex and
Visualforce, Scala, Swift, T-SQL, Typescript, Apache Velocity, WSDL, XML and XSL.
The newest version!
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import net.sourceforge.pmd.lang.document.Chars;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.util.StringUtil;
public class SimpleRenderer implements CPDReportRenderer {
private String separator;
private boolean trimLeadingWhitespace;
public static final String DEFAULT_SEPARATOR = "=====================================================================";
public SimpleRenderer() {
this(false);
}
public SimpleRenderer(boolean trimLeadingWhitespace) {
this(DEFAULT_SEPARATOR);
this.trimLeadingWhitespace = trimLeadingWhitespace;
}
public SimpleRenderer(String theSeparator) {
separator = theSeparator;
}
@Override
public void render(CPDReport report, Writer writer0) throws IOException {
PrintWriter writer = new PrintWriter(writer0);
Iterator matches = report.getMatches().iterator();
if (matches.hasNext()) {
renderOn(report, writer, matches.next());
}
while (matches.hasNext()) {
Match match = matches.next();
writer.println(separator);
renderOn(report, writer, match);
}
writer.flush();
}
private void renderOn(CPDReport report, PrintWriter writer, Match match) throws IOException {
writer.append("Found a ").append(String.valueOf(match.getLineCount())).append(" line (").append(String.valueOf(match.getTokenCount()))
.append(" tokens) duplication in the following files: ").println();
for (Mark mark : match) {
FileLocation loc = mark.getLocation();
writer.append("Starting at line ")
.append(String.valueOf(loc.getStartLine()))
.append(" of ").append(report.getDisplayName(loc.getFileId()))
.println();
}
writer.println(); // add a line to separate the source from the desc above
Chars source = report.getSourceCodeSlice(match.getFirstMark());
if (trimLeadingWhitespace) {
for (Chars line : StringUtil.linesWithTrimIndent(source)) {
line.writeFully(writer);
writer.println();
}
return;
}
source.writeFully(writer);
writer.println();
}
}