diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java index 98ed9ed1a6..90d5dfc2b1 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java @@ -4,10 +4,9 @@ package net.sourceforge.pmd.cli.commands.internal; -import java.io.BufferedWriter; import java.io.File; import java.io.IOException; -import java.io.OutputStreamWriter; +import java.nio.charset.Charset; import java.nio.file.Path; import java.util.Arrays; import java.util.Iterator; @@ -21,9 +20,11 @@ import net.sourceforge.pmd.cli.commands.typesupport.internal.CpdLanguageTypeSupp import net.sourceforge.pmd.cli.internal.ExecutionResult; import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.cpd.CPDConfiguration; +import net.sourceforge.pmd.cpd.CPDReport; import net.sourceforge.pmd.cpd.Language; import net.sourceforge.pmd.cpd.Tokenizer; import net.sourceforge.pmd.internal.LogMessages; +import net.sourceforge.pmd.util.IOUtil; import picocli.CommandLine.Command; import picocli.CommandLine.Option; @@ -34,7 +35,7 @@ import picocli.CommandLine.ParameterException; public class CpdCommand extends AbstractAnalysisPmdSubcommand { @Option(names = { "--language", "-l" }, description = "The source code language.%nValid values: ${COMPLETION-CANDIDATES}", - defaultValue = "java", converter = CpdLanguageTypeSupport.class, completionCandidates = CpdLanguageTypeSupport.class) + defaultValue = CPDConfiguration.DEFAULT_LANGUAGE, converter = CpdLanguageTypeSupport.class, completionCandidates = CpdLanguageTypeSupport.class) private Language language; @Option(names = "--minimum-tokens", @@ -48,7 +49,7 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand { @Option(names = { "--format", "-f" }, description = "Report format.%nValid values: ${COMPLETION-CANDIDATES}%n" + "Alternatively, you can provide the fully qualified name of a custom CpdRenderer in the classpath.", - defaultValue = "text", completionCandidates = CpdSupportedReportFormatsCandidates.class) + defaultValue = CPDConfiguration.DEFAULT_RENDERER, completionCandidates = CpdSupportedReportFormatsCandidates.class) private String rendererName; @Option(names = "--ignore-literals", @@ -110,14 +111,17 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand { configuration.setMinimumTileSize(minimumTokens); configuration.setNonRecursive(nonRecursive); configuration.setNoSkipBlocks(noSkipBlocks); - configuration.setRendererName(null); + configuration.setRendererName(rendererName); configuration.setSkipBlocksPattern(skipBlocksPattern); configuration.setSkipDuplicates(skipDuplicates); configuration.setSkipLexicalErrors(skipLexicalErrors); configuration.setSourceEncoding(encoding.getEncoding().name()); configuration.setURI(uri == null ? null : uri.toString()); - configuration.setCPDRenderer(CPDConfiguration.getCPDRendererFromString(rendererName, encoding.getEncoding().name())); + configuration.postContruct(); + // Pass extra parameters as System properties to allow language + // implementation to retrieve their associate values... + CPDConfiguration.setSystemProperties(configuration); return configuration; } @@ -133,7 +137,8 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand { try { cpd.go(); - configuration.getCPDRenderer().render(cpd.getMatches(), new BufferedWriter(new OutputStreamWriter(System.out))); + final CPDReport report = cpd.toReport(); + configuration.getCPDReportRenderer().render(report, IOUtil.createWriter(Charset.defaultCharset(), null)); if (cpd.getMatches().hasNext() && configuration.isFailOnViolation()) { return ExecutionResult.VIOLATIONS_FOUND; diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageTypeSupport.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageTypeSupport.java index a250f61b5c..a84dfcff24 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageTypeSupport.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageTypeSupport.java @@ -22,13 +22,13 @@ public class PmdLanguageTypeSupport implements ITypeConverter, Iterabl @Override public Language convert(final String value) throws Exception { - return LanguageRegistry.getLanguages().stream() + return LanguageRegistry.PMD.getLanguages().stream() .filter(l -> l.getTerseName().equals(value)).findFirst() .orElseThrow(() -> new TypeConversionException("Unknown language: " + value)); } @Override public Iterator iterator() { - return LanguageRegistry.getLanguages().stream().map(Language::getTerseName).iterator(); + return LanguageRegistry.PMD.getLanguages().stream().map(Language::getTerseName).iterator(); } } diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageVersionTypeSupport.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageVersionTypeSupport.java index 97c31b6975..f935056259 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageVersionTypeSupport.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/typesupport/internal/PmdLanguageVersionTypeSupport.java @@ -26,14 +26,14 @@ public class PmdLanguageVersionTypeSupport implements ITypeConverter iterator() { // Explicit language-version pairs, such as "java-18" or "apex-54". // We build these directly to retain aliases. "java-8" works, but the canonical name for the LanguageVersion is java-1.8 - return LanguageRegistry.getLanguages().stream() + return LanguageRegistry.PMD.getLanguages().stream() .flatMap(l -> l.getVersionNamesAndAliases().stream().map(v -> l.getTerseName() + "-" + v)) .collect(Collectors.toCollection(TreeSet::new)).iterator(); } @Override public LanguageVersion convert(final String value) throws Exception { - return LanguageRegistry.getLanguages().stream() + return LanguageRegistry.PMD.getLanguages().stream() .filter(l -> value.startsWith(l.getTerseName() + "-")) .map(l -> l.getVersion(value.substring(l.getTerseName().length() + 1))) .filter(lv -> lv != null) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java index 0b322797d4..ab6134cf2d 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDConfiguration.java @@ -344,7 +344,7 @@ public class CPDConfiguration extends AbstractConfiguration { return cpdRenderer; } - CPDReportRenderer getCPDReportRenderer() { + public CPDReportRenderer getCPDReportRenderer() { return cpdReportRenderer; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/treeexport/TreeExportConfiguration.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/treeexport/TreeExportConfiguration.java index d7cc078cdc..a36f8f0106 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/treeexport/TreeExportConfiguration.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/treeexport/TreeExportConfiguration.java @@ -14,7 +14,7 @@ import net.sourceforge.pmd.lang.LanguageRegistry; public class TreeExportConfiguration extends AbstractConfiguration { private String format = "xml"; - private Language language = LanguageRegistry.getDefaultLanguage(); + private Language language = LanguageRegistry.PMD.getLanguageById("java"); private Properties properties = new Properties(); private Path file; private boolean readStdin;