PR review

This commit is contained in:
Clément Fournier
2022-02-27 18:56:49 +01:00
parent 9217999bb5
commit af576dbf78
6 changed files with 65 additions and 56 deletions

View File

@ -36,6 +36,7 @@ try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
The `PMD` class still supports methods related to CLI execution: `runPmd` and `main`.
All other members are now deprecated for removal.
The CLI itself remains compatible, if you run PMD via command-line, no action is required on your part.
### Fixed Issues

View File

@ -56,7 +56,7 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger;
* Entry point for PMD's CLI. Use {@link #runPmd(PMDConfiguration)}
* or {@link #runPmd(String...)} to mimic a CLI run. This class is
* not a supported programmatic API anymore, use {@link PmdAnalysis}
* for fine control over the PMD execution.
* for fine control over the PMD integration and execution.
*
* <p><strong>Warning:</strong> This class is not intended to be instantiated or subclassed. It will
* be made final in PMD7.

View File

@ -49,6 +49,7 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger;
* config.setReportFormat("xml");
*
* try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
* // here you can add more rulesets or files
* pmd.performAnalysis();
* }
* }</pre>
@ -128,7 +129,8 @@ public final class PmdAnalysis implements AutoCloseable {
}
/**
* Add a new renderer. The given renderer must already be started.
* Add a new renderer. The given renderer must not already be started,
* it will be started by {@link #performAnalysis()}.
*
* @throws NullPointerException If the parameter is null
*/

View File

@ -4,15 +4,17 @@
package net.sourceforge.pmd.benchmark;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
@ -29,13 +31,12 @@ import net.sourceforge.pmd.RulesetsFactoryUtils;
import net.sourceforge.pmd.SourceCodeProcessor;
import net.sourceforge.pmd.lang.AbstractParser;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageFilenameFilter;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.document.FileCollector;
import net.sourceforge.pmd.lang.document.TextFile;
import net.sourceforge.pmd.util.log.SimplePmdLogger;
import net.sourceforge.pmd.util.FileUtil;
import net.sourceforge.pmd.util.datasource.DataSource;
/**
* @deprecated use {@link TimeTracker} instead
@ -43,7 +44,6 @@ import net.sourceforge.pmd.util.log.SimplePmdLogger;
@Deprecated
public final class Benchmarker {
private static final Logger LOGGER = Logger.getLogger(Benchmarker.class.getName());
private static final Map<String, BenchmarkResult> BENCHMARKS_BY_NAME = new HashMap<>();
private Benchmarker() { }
@ -96,42 +96,40 @@ public final class Benchmarker {
String targetjdk = findOptionalStringValue(args, "--targetjdk", "1.4");
Language language = LanguageRegistry.getLanguage("Java");
LanguageVersion languageVersion = language.getVersion(targetjdk);
LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
if (languageVersion != null) {
discoverer.setDefaultLanguageVersion(languageVersion);
if (languageVersion == null) {
languageVersion = language.getDefaultVersion();
}
String srcDir = findOptionalStringValue(args, "--source-directory", "/usr/local/java/src/java/lang/");
List<DataSource> dataSources = FileUtil.collectFiles(srcDir, new LanguageFilenameFilter(language));
try (FileCollector collector = FileCollector.newCollector(discoverer, new SimplePmdLogger(LOGGER))) {
boolean debug = findBooleanSwitch(args, "--debug");
boolean parseOnly = findBooleanSwitch(args, "--parse-only");
boolean debug = findBooleanSwitch(args, "--debug");
boolean parseOnly = findBooleanSwitch(args, "--parse-only");
if (debug) {
System.out.println("Using " + language.getName() + " " + languageVersion.getVersion());
}
if (parseOnly) {
Parser parser = PMD.parserFor(languageVersion, null);
parseStress(parser, dataSources, debug);
} else {
String ruleset = findOptionalStringValue(args, "--ruleset", "");
if (debug) {
System.out.println("Using " + language.getName() + " " + languageVersion.getVersion());
System.out.println("Checking directory " + srcDir);
}
if (parseOnly) {
Parser parser = PMD.parserFor(languageVersion, null);
parseStress(parser, collector, debug);
Set<RuleDuration> results = new TreeSet<>();
RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory();
if (StringUtils.isNotBlank(ruleset)) {
stress(languageVersion, factory.createRuleSet(ruleset), dataSources, results, debug);
} else {
String ruleset = findOptionalStringValue(args, "--ruleset", "");
if (debug) {
System.out.println("Checking directory " + srcDir);
Iterator<RuleSet> i = factory.getRegisteredRuleSets();
while (i.hasNext()) {
stress(languageVersion, i.next(), dataSources, results, debug);
}
Set<RuleDuration> results = new TreeSet<>();
RuleSetFactory factory = RulesetsFactoryUtils.defaultFactory();
if (StringUtils.isNotBlank(ruleset)) {
stress(languageVersion, factory.createRuleSet(ruleset), collector, results, debug);
} else {
Iterator<RuleSet> i = factory.getRegisteredRuleSets();
while (i.hasNext()) {
stress(languageVersion, i.next(), collector, results, debug);
}
}
TextReport report = new TextReport();
report.generate(results, System.err);
}
TextReport report = new TextReport();
report.generate(results, System.err);
}
}
@ -144,13 +142,14 @@ public final class Benchmarker {
* boolean
* @throws IOException
*/
private static void parseStress(Parser parser, FileCollector dataSources, boolean debug) throws IOException {
private static void parseStress(Parser parser, List<DataSource> dataSources, boolean debug) throws IOException {
long start = System.currentTimeMillis();
for (TextFile ds : dataSources.getCollectedFiles()) {
String contents = ds.readContents();
AbstractParser.doParse(parser, ds.getDisplayName(), new StringReader(contents));
for (DataSource ds : dataSources) {
try (DataSource dataSource = ds; InputStreamReader reader = new InputStreamReader(dataSource.getInputStream())) {
AbstractParser.doParse(parser, dataSource.getNiceFileName(false, null), reader);
}
}
if (debug) {
@ -165,7 +164,7 @@ public final class Benchmarker {
* LanguageVersion
* @param ruleSet
* RuleSet
* @param files
* @param dataSources
* List<DataSource>
* @param results
* Set<RuleDuration>
@ -174,7 +173,7 @@ public final class Benchmarker {
* @throws PMDException
* @throws IOException
*/
private static void stress(LanguageVersion languageVersion, RuleSet ruleSet, FileCollector files,
private static void stress(LanguageVersion languageVersion, RuleSet ruleSet, List<DataSource> dataSources,
Set<RuleDuration> results, boolean debug) throws PMDException, IOException {
for (Rule rule: ruleSet.getRules()) {
@ -190,10 +189,11 @@ public final class Benchmarker {
RuleContext ctx = new RuleContext();
long start = System.currentTimeMillis();
for (TextFile ds : files.getCollectedFiles()) {
String source = ds.readContents();
ctx.setSourceCodeFile(new File(ds.getPathId()));
new SourceCodeProcessor(config).processSourceCode(new StringReader(source), ruleSets, ctx);
for (DataSource ds : dataSources) {
try (DataSource dataSource = ds; InputStream stream = new BufferedInputStream(dataSource.getInputStream())) {
ctx.setSourceCodeFile(new File(dataSource.getNiceFileName(false, null)));
new SourceCodeProcessor(config).processSourceCode(stream, ruleSets, ctx);
}
}
long end = System.currentTimeMillis();
long elapsed = end - start;

View File

@ -29,7 +29,6 @@ import java.util.Objects;
import java.util.Set;
import net.sourceforge.pmd.PmdAnalysis;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.lang.Language;
@ -63,9 +62,10 @@ public final class FileCollector implements AutoCloseable {
}
/**
* Experimental: please use {@link PmdAnalysis#files()} instead of this
* Internal API: please use {@link PmdAnalysis#files()} instead of
* creating a collector yourself.
*/
@Experimental
@InternalApi
public static FileCollector newCollector(LanguageVersionDiscoverer discoverer, PmdLogger logger) {
return new FileCollector(discoverer, logger);
}

View File

@ -107,14 +107,20 @@ public abstract class BaseCLITest {
* Returns the log output.
*/
protected String runTest(int expectedExitCode, String... args) {
ByteArrayOutputStream console = new ByteArrayOutputStream();
PrintStream out = new PrintStream(console);
System.setOut(out);
System.setErr(out);
StatusCode statusCode = PMD.runPmd(args);
assertEquals(expectedExitCode, statusCode.toInt());
return console.toString();
PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
try {
ByteArrayOutputStream console = new ByteArrayOutputStream();
PrintStream out = new PrintStream(console);
System.setOut(out);
System.setErr(out);
StatusCode statusCode = PMD.runPmd(args);
assertEquals(expectedExitCode, statusCode.toInt());
return console.toString();
} finally {
System.setOut(oldOut);
System.setOut(oldErr);
}
}
@Deprecated