[core] More deprecations around PMD/Report/RuleContext (#3762)

This commit is contained in:
Andreas Dangel
2022-02-18 11:33:05 +01:00
parent 3dde3bc739
commit 7733be75ff
9 changed files with 72 additions and 23 deletions

View File

@ -24,12 +24,21 @@ This is a {{ site.pmd.release_type }} release.
#### Deprecated API
Some API deprecations were performed in core PMD classes, to improve compatibility with PMD 7.
- {% jdoc core::Report %}: construction methods like addViolation or createReport
- {% jdoc core::Report %}: the constructor and other construction methods like addViolation or createReport
- {% jdoc core::RuleContext %}: all constructors, getters and setters. A new set
of stable methods, matching those in PMD 7, was added to replace the `addViolation`
overloads of {% jdoc core::lang.rule.AbstractRule %}. In PMD 7, `RuleContext` will
be the API to report violations, and it can already be used as such in PMD 6.
- The field {% jdoc core::PMD#configuration %} is unused and will be removed.
#### Internal API
Those APIs are not intended to be used by clients, and will be hidden or removed with PMD 7.0.0.
You can identify them with the `@InternalApi` annotation. You'll also get a deprecation warning.
- {% jdoc core::RuleSet %}: methods that serve to apply rules, including `apply`, `start`, `end`, `removeDysfunctionalRules`
- {% jdoc !!core::renderers.AbstractAccumulatingRenderer#renderFileReport(Report) %} is internal API
and should not be overridden in own renderers.
#### Changed API

View File

@ -61,6 +61,17 @@ import net.sourceforge.pmd.util.log.ScopedLogHandlersManager;
* process is controlled via interactions with this class. A command line
* interface is supported, as well as a programmatic API for integrating PMD
* with other software such as IDEs and Ant.
*
* <p>Main entrypoints are:
* <ul>
* <li>{@link #main(String[])} which exits the java process</li>
* <li>{@link #runPmd(String...)} which returns a {@link StatusCode}</li>
* <li>{@link #runPmd(PMDConfiguration)}</li>
* <li>{@link #processFiles(PMDConfiguration, List, Collection, List)}</li>
* </ul>
*
* <p><strong>Warning:</strong> This class is not intended to be instantiated or subclassed. It will
* be made final in PMD7.
*/
public class PMD {
@ -77,7 +88,11 @@ public class PMD {
/**
* Contains the configuration with which this PMD instance has been created.
*
* @deprecated this configuration field is unused and will be removed. The class
* {@link PMD} should not be instantiated or subclassed.
*/
@Deprecated
protected final PMDConfiguration configuration;
private final SourceCodeProcessor rulesetsFileProcessor;

View File

@ -58,6 +58,17 @@ public class Report implements Iterable<RuleViolation> {
private long end;
private final List<SuppressedViolation> suppressedRuleViolations = new ArrayList<>();
/**
* @deprecated {@link Report} instances are created by PMD. There is no need
* to create a own report. This constructor will be hidden
* in PMD7.
*/
@Deprecated
@InternalApi
public Report() { // NOPMD - UnnecessaryConstructor
// TODO: should be package-private, you have to use a listener to build a report.
}
/**
* Creates a new, initialized, empty report for the given file name.
*

View File

@ -250,15 +250,13 @@ public class RuleContext {
*
* @param filename
* The file name.
* @deprecated This method will be removed. The file should only be
* set with {@link #setSourceCodeFile(File)}. Setting the filename here
* has no effect.
* @deprecated Internal API, removed in PMD 7
*/
@Deprecated
@InternalApi
public void setSourceCodeFilename(String filename) {
// ignored, does nothing.
LOG.warning("The method RuleContext::setSourceCodeFilename(String) has been deprecated and will be removed."
+ "Setting the filename here has no effect. Use RuleContext::setSourceCodeFile(File) instead.");
LOG.warning("The method RuleContext::setSourceCodeFilename(String) has been deprecated and will be removed.");
}
/**

View File

@ -7,6 +7,7 @@ package net.sourceforge.pmd.renderers;
import java.io.IOException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.util.datasource.DataSource;
/**
@ -16,8 +17,8 @@ import net.sourceforge.pmd.util.datasource.DataSource;
* quite large in some scenarios. Consider using
* {@link AbstractIncrementingRenderer} which can use significantly less memory.
*
* Subclasses should implement the {@link #end()} method to output the
* {@link #report}.
* <p>Subclasses should only implement the {@link #end()} method to output the
* complete {@link #report}.
*
* @see AbstractIncrementingRenderer
*/
@ -42,7 +43,15 @@ public abstract class AbstractAccumulatingRenderer extends AbstractRenderer {
// does nothing - override if necessary
}
/**
* {@inheritDoc}
*
* @deprecated This is internal API. Do not override when extending {@link AbstractAccumulatingRenderer}.
* In PMD7 this method will be made final.
*/
@Override
@InternalApi
@Deprecated
public void renderFileReport(Report report) throws IOException {
this.report.merge(report);
}

View File

@ -34,15 +34,17 @@ public class ExcludeLinesTest extends RuleTst {
@Test
public void testAlternateMarker() throws Exception {
PMD p = new PMD();
p.getConfiguration().setSuppressMarker("FOOBAR");
PMDConfiguration configuration = new PMDConfiguration();
configuration.setSuppressMarker("FOOBAR");
RuleContext ctx = new RuleContext();
Report r = new Report();
ctx.setReport(r);
ctx.setSourceCodeFile(new File("n/a"));
ctx.setLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion());
RuleSet rules = RuleSet.forSingleRule(rule);
p.getSourceCodeProcessor().processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx);
SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(configuration);
sourceCodeProcessor.processSourceCode(new StringReader(TEST3), new RuleSets(rules), ctx);
assertTrue(r.isEmpty());
assertEquals(r.getSuppressedRuleViolations().size(), 1);
}

View File

@ -13,7 +13,7 @@ import java.util.Iterator;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.PMDException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
@ -21,6 +21,7 @@ import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.SourceCodeProcessor;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.xpath.MetricFunction;
@ -50,14 +51,14 @@ public class XPathMetricFunctionTest {
private Iterator<RuleViolation> getViolations(Rule rule, String code) throws PMDException {
PMD p = new PMD();
RuleContext ctx = new RuleContext();
Report report = new Report();
ctx.setReport(report);
ctx.setSourceCodeFile(new File("n/a"));
ctx.setIgnoreExceptions(false); // for test, we want immediate exceptions thrown and not collect them
RuleSet rules = RuleSet.forSingleRule(rule);
p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(new PMDConfiguration());
sourceCodeProcessor.processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
return report.iterator();
}

View File

@ -15,6 +15,7 @@ import java.util.List;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.PMDException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
@ -22,6 +23,7 @@ import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.SourceCodeProcessor;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.Parser;
@ -200,13 +202,13 @@ public class XPathRuleTest extends RuleTst {
}
private static Report getReportForTestString(Rule r, String test) throws PMDException {
PMD p = new PMD();
RuleContext ctx = new RuleContext();
Report report = new Report();
ctx.setReport(report);
ctx.setSourceCodeFile(new File("n/a"));
RuleSet rules = RuleSet.forSingleRule(r);
p.getSourceCodeProcessor().processSourceCode(new StringReader(test), new RuleSets(rules), ctx);
SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(new PMDConfiguration());
sourceCodeProcessor.processSourceCode(new StringReader(test), new RuleSets(rules), ctx);
return report;
}

View File

@ -34,7 +34,7 @@ import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.PMDException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
@ -44,6 +44,7 @@ import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.RulesetsFactoryUtils;
import net.sourceforge.pmd.SourceCodeProcessor;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.properties.PropertyDescriptor;
@ -268,18 +269,18 @@ public abstract class RuleTst {
public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion,
boolean isUseAuxClasspath) {
try {
PMD p = new PMD();
p.getConfiguration().setDefaultLanguageVersion(languageVersion);
p.getConfiguration().setIgnoreIncrementalAnalysis(true);
PMDConfiguration configuration = new PMDConfiguration();
configuration.setDefaultLanguageVersion(languageVersion);
configuration.setIgnoreIncrementalAnalysis(true);
if (isUseAuxClasspath) {
// configure the "auxclasspath" option for unit testing
p.getConfiguration().prependClasspath(".");
configuration.prependClasspath(".");
} else {
// simple class loader, that doesn't delegate to parent.
// this allows us in the tests to simulate PMD run without
// auxclasspath, not even the classes from the test dependencies
// will be found.
p.getConfiguration().setClassLoader(new ClassLoader() {
configuration.setClassLoader(new ClassLoader() {
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("java.") || name.startsWith("javax.")) {
@ -295,7 +296,8 @@ public abstract class RuleTst {
ctx.setLanguageVersion(languageVersion);
ctx.setIgnoreExceptions(false);
RuleSet rules = RuleSet.forSingleRule(rule);
p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
SourceCodeProcessor sourceCodeProcessor = new SourceCodeProcessor(configuration);
sourceCodeProcessor.processSourceCode(new StringReader(code), new RuleSets(rules), ctx);
} catch (Exception e) {
throw new RuntimeException(e);
}