From 1900aa79c4f2e5646b732e17d49502da3430ae40 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 8 Feb 2024 08:15:15 +0100 Subject: [PATCH] [core] Remove deprecated methods in AbstractRule AbstractRule#deepCopyValuesTo AbstractRule#addRuleChainVisit AbstractRule#addViolation AbstractRule#addViolationWithMessage --- docs/pages/release_notes.md | 7 ++ .../pmd/lang/rule/AbstractRule.java | 99 +------------------ .../sourceforge/pmd/lang/rule/XPathRule.java | 2 +- .../java/net/sourceforge/pmd/FooRule.java | 2 +- .../java/net/sourceforge/pmd/RuleSetTest.java | 4 +- .../lang/impl/MultiThreadProcessorTest.java | 2 +- .../pmd/test/AbstractMetricTestRule.java | 2 +- 7 files changed, 18 insertions(+), 100 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 70a413d4df..119125ec24 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -205,6 +205,13 @@ The following previously deprecated rules have been finally removed: * Only XPath version 3.1 is now supported. This version of the XPath language is mostly identical to XPath 2.0. XPath rules by default use now {%jdoc core::lang.rule.xpath.XPathVersion#XPATH_3_1 %}. * `net.sourceforge.pmd.lang.rule.AbstractDelegateRule` removed. It has been merged with {%jdoc core::lang.rule.RuleReference %}. + * {%jdoc !!core::lang.rule.AbstractRule %} - the following methods have been removed: + * `deepCopyValuesTo(AbstractRule)` - use {%jdoc core::lang.rule.AbstractRule#deepCopy() %} instead. + * `addRuleChainVisit(Class)` - override {%jdoc core::lang.rule.AbstractRule#buildTargetSelector() %} in order to register nodes for rule chain visits. + * `addViolation(...)` - use {%jdoc core::RuleContext#addViolation(core::lang.ast.Node) %} instead, e.g. via `asCtx(data).addViolation(...)`. + Note: These methods were only marked as deprected in javadoc. + * `addViolationWithMessage(...)` - use {%jdoc core::RuleContext#addViolationWithMessage(core::lang.ast.Node,java.lang.String) %} instead, e.g. via + `asCtx(data).addViolationWithMessage(...)`. Note: These methods were only marked as deprected in javadoc. * pmd-apex * {%jdoc apex::lang.apex.ast.ApexNode %} and {% jdoc apex::lang.apex.ast.ASTApexFile %} * `#getApexVersion()`: In PMD 6, this method has been deprecated but was defined in the class `ApexRootNode`. diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/AbstractRule.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/AbstractRule.java index 7430bd6166..c978a40d87 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/AbstractRule.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/AbstractRule.java @@ -60,34 +60,6 @@ public abstract class AbstractRule extends AbstractPropertySource implements Rul return "rule"; } - /** - * @deprecated Use {@link #deepCopy()} to create verbatim copies of rules. - */ - @Deprecated - public void deepCopyValuesTo(AbstractRule otherRule) { - otherRule.language = language; - otherRule.minimumLanguageVersion = minimumLanguageVersion; - otherRule.maximumLanguageVersion = maximumLanguageVersion; - otherRule.deprecated = deprecated; - otherRule.name = name; - otherRule.since = since; - otherRule.ruleClass = ruleClass; - otherRule.ruleSetName = ruleSetName; - otherRule.message = message; - otherRule.description = description; - otherRule.examples = copyExamples(); - otherRule.externalInfoUrl = externalInfoUrl; - otherRule.priority = priority; - otherRule.propertyDescriptors = new ArrayList<>(getPropertyDescriptors()); - otherRule.propertyValuesByDescriptor = copyPropertyValues(); - otherRule.ruleChainVisits = new LinkedHashSet<>(ruleChainVisits); - otherRule.classRuleChainVisits = new LinkedHashSet<>(classRuleChainVisits); - } - - private List copyExamples() { - return new ArrayList<>(examples); - } - @Override public Language getLanguage() { return language; @@ -237,16 +209,6 @@ public abstract class AbstractRule extends AbstractPropertySource implements Rul return classRuleChainVisits; } - - /** - * @deprecated Override {@link #buildTargetSelector()}, this is - * provided for legacy compatibility - */ - @Deprecated - protected void addRuleChainVisit(Class nodeClass) { - classRuleChainVisits.add(nodeClass); - } - @Override public final RuleTargetSelector getTargetSelector() { if (myStrategy == null) { @@ -256,8 +218,7 @@ public abstract class AbstractRule extends AbstractPropertySource implements Rul } /** - * Create the targeting strategy for this rule. Please override - * this instead of using {@link #addRuleChainVisit(Class)}. + * Create the targeting strategy for this rule. * Use the factory methods of {@link RuleTargetSelector}. */ @NonNull @@ -286,10 +247,10 @@ public abstract class AbstractRule extends AbstractPropertySource implements Rul * asCtx(data).addViolationWithMessage(node, "Some message"); * } * - * In PMD 7, rules will have type-safe access to a RuleContext, and - * this will be deprecated as useless. In PMD 6, you can use this to - * stop using the deprecated {@link #addViolation(Object, Node)} overloads - * of this class. + * In longer term, rules will have type-safe access to a RuleContext, when the + * rules use an appropriate visitor. Many rules have not been refactored yet. + * Once this is done, this method will be deprecated as useless. Until then, + * this is a way to hide the explicit cast to {@link RuleContext} in rules. */ protected final RuleContext asCtx(Object ctx) { if (ctx instanceof RuleContext) { @@ -306,56 +267,6 @@ public abstract class AbstractRule extends AbstractPropertySource implements Rul || rule instanceof RuleReference && this.isThisRule(((RuleReference) rule).getRule()); } - /** - * @see RuleContext#addViolation(Node) - * @deprecated Replace with {@code asCtx(data).addViolation(node)}. - */ - public void addViolation(Object data, Node node) { - asCtx(data).addViolation(node); - } - - /** - * @see RuleContext#addViolation(Node, Object[]) - * - * @deprecated Replace with {@code asCtx(data).addViolation(node, arg)}. - */ - public void addViolation(Object data, Node node, String arg) { - asCtx(data).addViolation(node, arg); - } - - /** - * @see RuleContext#addViolation(Node, Object[]) - * - * @deprecated Replace with {@code asCtx(data).addViolation(node, arg1, arg2)}. - */ - public void addViolation(Object data, Node node, Object... args) { - asCtx(data).addViolation(node, args); - } - - /** - * @see RuleContext#addViolationWithMessage(Node, String) - * @deprecated Replace with {@code asCtx(data).addViolationWithMessage(node, message)}. - */ - public void addViolationWithMessage(Object data, Node node, String message) { - asCtx(data).addViolationWithMessage(node, message); - } - - /** - * @see RuleContext#addViolationWithPosition(Node, int, int, String, Object...) - * @deprecated Replace with {@code asCtx(data).addViolationWithPosition(node, beginLine, endLine, message)}. - */ - public void addViolationWithMessage(Object data, Node node, String message, int beginLine, int endLine) { - asCtx(data).addViolationWithPosition(node, beginLine, endLine, message); - } - - /** - * @see RuleContext#addViolationWithMessage(Node, String, Object...) - * @deprecated Replace with {@code asCtx(data).addViolationWithMessage(node, message, args)}. - */ - public void addViolationWithMessage(Object data, Node node, String message, Object[] args) { - asCtx(data).addViolationWithMessage(node, message, args); - } - /** * Rules are equal if: *
    diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java index e5cc2ea252..12300fc732 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java @@ -134,7 +134,7 @@ public final class XPathRule extends AbstractRule { } for (Node nodeWithViolation : nodesWithViolation) { - addViolation(ctx, nodeWithViolation, nodeWithViolation.getImage()); + ctx.addViolation(nodeWithViolation, nodeWithViolation.getImage()); } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/FooRule.java b/pmd-core/src/test/java/net/sourceforge/pmd/FooRule.java index 540575da39..bd56dbf446 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/FooRule.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/FooRule.java @@ -45,7 +45,7 @@ public class FooRule extends AbstractRule { apply(node.getChild(i), ctx); } if ("Foo".equals(node.getImage())) { - addViolation(ctx, node); + ctx.addViolation(node); } } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java index ca94ab2f1f..c8c0bdbd27 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java @@ -518,7 +518,7 @@ class RuleSetTest { }).addRule(new MockRule() { @Override public void apply(Node target, RuleContext ctx) { - addViolationWithMessage(ctx, target, "Test violation of the second rule in the ruleset"); + ctx.addViolationWithMessage(target, "Test violation of the second rule in the ruleset"); } }).build(); @@ -557,7 +557,7 @@ class RuleSetTest { @Override public void apply(Node target, RuleContext ctx) { - addViolationWithMessage(ctx, target, "Test violation of the second rule in the ruleset"); + ctx.addViolationWithMessage(target, "Test violation of the second rule in the ruleset"); } }).build(); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/impl/MultiThreadProcessorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/impl/MultiThreadProcessorTest.java index 12e381fac1..4938e9343b 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/impl/MultiThreadProcessorTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/impl/MultiThreadProcessorTest.java @@ -111,7 +111,7 @@ class MultiThreadProcessorTest extends AbstractPMDProcessorTest { letTheOtherThreadRun(100); if (hasViolation) { - addViolation(ctx, target); + ctx.addViolation(target); } } diff --git a/pmd-lang-test/src/main/java/net/sourceforge/pmd/test/AbstractMetricTestRule.java b/pmd-lang-test/src/main/java/net/sourceforge/pmd/test/AbstractMetricTestRule.java index 4c18a4eb8b..a72d7728ff 100644 --- a/pmd-lang-test/src/main/java/net/sourceforge/pmd/test/AbstractMetricTestRule.java +++ b/pmd-lang-test/src/main/java/net/sourceforge/pmd/test/AbstractMetricTestRule.java @@ -88,7 +88,7 @@ public abstract class AbstractMetricTestRule> e N result = Metric.compute(metric, target, options); if (result != null && reportLevel.compareTo(result) <= 0) { - addViolationWithMessage(ctx, target, violationMessage(target, result)); + ctx.addViolationWithMessage(target, violationMessage(target, result)); } }