This commit is contained in:
Clément Fournier committed 2023-02-11 18:10:35 +01:00
1 parent 27a4aba871
commit 65d953bfb4
27 files changed
+467 -571

No files matched your search

@@ -13,18 +13,18 @@ import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.cli.commands.typesupport.internal.CpdLanguageTypeSupport;
import net.sourceforge.pmd.cli.internal.CliExitCode;
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.CpdAnalysis;
import net.sourceforge.pmd.cpd.Tokenizer;
import net.sourceforge.pmd.internal.LogMessages;
import net.sourceforge.pmd.internal.util.IOUtil;
import net.sourceforge.pmd.lang.Language;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@@ -130,17 +130,22 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand {
protected CliExitCode execute() {
final Logger logger = LoggerFactory.getLogger(CpdCommand.class);
// TODO : Create a new CpdAnalysis to match PmdAnalysis
final CPDConfiguration configuration = toConfiguration();
final CPD cpd = new CPD(configuration);
try {
cpd.go();
try (CpdAnalysis cpd = new CpdAnalysis(configuration)){
final CPDReport report = cpd.toReport();
configuration.getCPDReportRenderer().render(report, IOUtil.createWriter(Charset.defaultCharset(), null));
MutableBoolean hasViolations = new MutableBoolean();
cpd.performAnalysis(report -> {
try {
configuration.getCPDReportRenderer().render(report, IOUtil.createWriter(Charset.defaultCharset(), null));
hasViolations.setValue(!report.getMatches().isEmpty());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
if (cpd.getMatches().hasNext() && configuration.isFailOnViolation()) {
if (hasViolations.booleanValue() && configuration.isFailOnViolation()) {
return CliExitCode.VIOLATIONS_FOUND;
}
} catch (IOException | RuntimeException e) {
@@ -4,30 +4,18 @@
package net.sourceforge.pmd.cli.commands.typesupport.internal;
import java.util.Arrays;
import java.util.Iterator;
import net.sourceforge.pmd.cpd.Language;
import net.sourceforge.pmd.cpd.LanguageFactory;
import picocli.CommandLine.ITypeConverter;
import net.sourceforge.pmd.lang.LanguageRegistry;
/**
* Provider of candidates / conversion support for supported CPD languages.
* Provider of candidates / conversion support for supported PMD languages.
*
* Beware, the help will report this on runtime, and be accurate to available
* modules in the classpath, but autocomplete will include all available at build time.
*/
public class CpdLanguageTypeSupport implements ITypeConverter<Language>, Iterable<String> {
public class CpdLanguageTypeSupport extends LanguageTypeSupport {
@Override
public Iterator<String> iterator() {
return Arrays.stream(LanguageFactory.supportedLanguages).iterator();
}
@Override
public Language convert(final String languageString) {
// TODO : If an unknown value is passed, AnyLanguage is returned silently…
return LanguageFactory.createLanguage(languageString);
public CpdLanguageTypeSupport() {
super(LanguageRegistry.CPD);
}
}
@@ -0,0 +1,42 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cli.commands.typesupport.internal;
import java.util.Iterator;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.TypeConversionException;
/**
* Provider of candidates / conversion support for supported PMD languages.
* <p>
* Beware, the help will report this on runtime, and be accurate to available
* modules in the classpath, but autocomplete will include all available at build time.
*/
public class LanguageTypeSupport implements ITypeConverter<Language>, Iterable<String> {
private final LanguageRegistry languageRegistry;
public LanguageTypeSupport(LanguageRegistry languageRegistry) {
this.languageRegistry = languageRegistry;
}
@Override
public Language convert(final String value) {
Language lang = languageRegistry.getLanguageById(value);
if (lang == null) {
throw new TypeConversionException("Unknown language: " + value);
}
return lang;
}
@Override
public Iterator<String> iterator() {
return languageRegistry.getLanguages().stream().map(Language::getId).iterator();
}
}
@@ -4,31 +4,18 @@
package net.sourceforge.pmd.cli.commands.typesupport.internal;
import java.util.Iterator;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.TypeConversionException;
/**
* Provider of candidates / conversion support for supported PMD languages.
*
* Beware, the help will report this on runtime, and be accurate to available
* modules in the classpath, but autocomplete will include all available at build time.
*/
public class PmdLanguageTypeSupport implements ITypeConverter<Language>, Iterable<String> {
public class PmdLanguageTypeSupport extends LanguageTypeSupport {
@Override
public Language convert(final String value) throws Exception {
return LanguageRegistry.PMD.getLanguages().stream()
.filter(l -> l.getTerseName().equals(value)).findFirst()
.orElseThrow(() -> new TypeConversionException("Unknown language: " + value));
public PmdLanguageTypeSupport() {
super(LanguageRegistry.PMD);
}
@Override
public Iterator<String> iterator() {
return LanguageRegistry.PMD.getLanguages().stream().map(Language::getTerseName).iterator();
}
}