From 049f2c7c596d015e5e496279ae1c43332657eb39 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 20 Jan 2024 17:28:33 +0100 Subject: [PATCH] [core] XPathRule - remove version property (#4309) --- .../userdocs/extending/defining_properties.md | 1 - .../userdocs/extending/writing_xpath_rules.md | 3 +- docs/pages/pmd/userdocs/migrating_to_pmd7.md | 2 +- docs/pages/release_notes.md | 5 ++ .../resources/category/apex/bestpractices.xml | 1 - .../resources/category/apex/performance.xml | 2 - .../pmd/lang/rule/xpath/XPathRule.java | 46 ++----------------- .../xpath/internal/SaxonXPathRuleQuery.java | 3 +- .../net/sourceforge/pmd/xml/j2ee.xml | 9 ---- .../test/resources/rulesets/dummy/basic.xml | 6 +-- .../pmd/docs/RuleDocGenerator.java | 1 - .../resources/rulesets/ruledoctest/sample.xml | 3 -- .../rulesets/ruledoctest/sample2.xml | 1 - .../resources/category/html/bestpractices.xml | 1 - .../resources/category/java/bestpractices.xml | 2 - .../resources/category/java/codestyle.xml | 3 -- .../main/resources/category/java/design.xml | 1 - .../resources/category/java/errorprone.xml | 5 -- .../resources/category/java/performance.xml | 1 - .../pmd/ant/classpathtest/ruleset.xml | 1 - .../category/kotlin/bestpractices.xml | 1 - .../main/resources/rulesets/dummy/basic.xml | 1 - .../pmd/lang/xml/rule/DomXPathRule.java | 2 - .../resources/category/xml/bestpractices.xml | 1 - 24 files changed, 14 insertions(+), 88 deletions(-) diff --git a/docs/pages/pmd/userdocs/extending/defining_properties.md b/docs/pages/pmd/userdocs/extending/defining_properties.md index 750ed79ebe..a1bef2fe1c 100644 --- a/docs/pages/pmd/userdocs/extending/defining_properties.md +++ b/docs/pages/pmd/userdocs/extending/defining_properties.md @@ -165,7 +165,6 @@ with a backslash when needed. ```xml - diff --git a/docs/pages/pmd/userdocs/extending/writing_xpath_rules.md b/docs/pages/pmd/userdocs/extending/writing_xpath_rules.md index c8bccc167a..3fd5c012e8 100644 --- a/docs/pages/pmd/userdocs/extending/writing_xpath_rules.md +++ b/docs/pages/pmd/userdocs/extending/writing_xpath_rules.md @@ -27,7 +27,8 @@ with opt-in support for XPath 2.0. See [the Saxonica documentation](https://www.saxonica.com/html/documentation/expressions/xpath31new.html) for an introduction to new features in XPath 3.1. -The property `version` of {% jdoc core::lang.rule.XPathRule %} is deprecated and will be removed. +The property `version` of {% jdoc core::lang.rule.XPathRule %} is deprecated and +has been removed with PMD 7. ## DOM representation of ASTs diff --git a/docs/pages/pmd/userdocs/migrating_to_pmd7.md b/docs/pages/pmd/userdocs/migrating_to_pmd7.md index ef04f34a48..8118caa4fa 100644 --- a/docs/pages/pmd/userdocs/migrating_to_pmd7.md +++ b/docs/pages/pmd/userdocs/migrating_to_pmd7.md @@ -93,7 +93,7 @@ XPath 2.0 is available in PMD 6 already and can be used right away. PMD 7 will u won't support XPath 1.0 anymore. The difference between XPath 2.0 and XPath 3.1 is not big, so your XPath 2.0 can be expected to work in PMD 7 without any further changes. So the migration path is to simply migrate to XPath 2.0. -After you have migrated your XPath rules to XPath 2.0, remove the "version" property, since that will be removed +After you have migrated your XPath rules to XPath 2.0, remove the "version" property, since that has been removed with PMD 7. PMD 7 by default uses XPath 3.1. See below [XPath](#xpath-migrating-from-10-to-20) for details. Then change the `class` attribute of your rule to `net.sourceforge.pmd.lang.rule.xpath.XPathRule` - because the diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 814730cb48..4b4f2eb93b 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -152,6 +152,11 @@ in the Migration Guide. #### API Changes +**XPath Rules** +* The property `version` was already deprecated and has finally been removed. Please don't define the version + property anymore in your custom XPath rules. By default, the latest XPath version will be used, which + is XPath 3.1. + **Moved classes/consolidated packages** * pmd-core diff --git a/pmd-apex/src/main/resources/category/apex/bestpractices.xml b/pmd-apex/src/main/resources/category/apex/bestpractices.xml index 637a647543..f2d723cb8c 100644 --- a/pmd-apex/src/main/resources/category/apex/bestpractices.xml +++ b/pmd-apex/src/main/resources/category/apex/bestpractices.xml @@ -116,7 +116,6 @@ annotation for test classes and methods. 3 - 3 - 3 - VERSION_DESCRIPTOR = - PropertyFactory.enumProperty("version", getXPathVersions()) - .desc("XPath specification version") - .defaultValue(XPathVersion.DEFAULT) - .build(); - /** * This is initialized only once when calling {@link #apply(Node, RuleContext)} or {@link #getTargetSelector()}. */ @@ -75,7 +60,6 @@ public final class XPathRule extends AbstractRule { @Deprecated public XPathRule() { definePropertyDescriptor(XPATH_DESCRIPTOR); - definePropertyDescriptor(VERSION_DESCRIPTOR); } /** @@ -93,7 +77,6 @@ public final class XPathRule extends AbstractRule { Objects.requireNonNull(expression, "XPath expression is null"); setProperty(XPathRule.XPATH_DESCRIPTOR, expression); - setProperty(XPathRule.VERSION_DESCRIPTOR, XPathVersion.ofId(version.getXmlName())); } @@ -104,14 +87,6 @@ public final class XPathRule extends AbstractRule { return rule; } - /** - * Returns the version for this rule. Returns null if this is not - * set or invalid. - */ - public XPathVersion getVersion() { - return getProperty(VERSION_DESCRIPTOR); - } - /** * Returns the XPath expression that implements this rule. */ @@ -143,11 +118,7 @@ public final class XPathRule extends AbstractRule { @Override public void initialize(LanguageProcessor languageProcessor) { String xpath = getXPathExpression(); - XPathVersion version = getVersion(); - - if (version == null) { - throw new IllegalStateException("Invalid XPath version, should have been caught by Rule::dysfunctionReason"); - } + XPathVersion version = XPathVersion.DEFAULT; try { xpathRuleQuery = new SaxonXPathRuleQuery(xpath, @@ -181,9 +152,8 @@ public final class XPathRule extends AbstractRule { private void logXPathRuleChainUsage(boolean usesRuleChain) { - LOG.debug("{} rule chain for XPath {} rule: {} ({})", + LOG.debug("{} rule chain for XPath rule: {} ({})", usesRuleChain ? "Using" : "no", - getProperty(XPathRule.VERSION_DESCRIPTOR), getName(), getRuleSetName()); } @@ -191,19 +161,9 @@ public final class XPathRule extends AbstractRule { @Override public String dysfunctionReason() { - if (getVersion() == null) { - return "Invalid XPath version '" + getProperty(VERSION_DESCRIPTOR) + "'"; - } else if (StringUtils.isBlank(getXPathExpression())) { + if (StringUtils.isBlank(getXPathExpression())) { return "Missing XPath expression"; } return null; } - - private static Map getXPathVersions() { - Map tmp = new HashMap<>(); - for (XPathVersion v : XPathVersion.values()) { - tmp.put(v.getXmlName(), v); - } - return Collections.unmodifiableMap(tmp); - } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/SaxonXPathRuleQuery.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/SaxonXPathRuleQuery.java index 4995b4ee95..bb14c23b4c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/SaxonXPathRuleQuery.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/xpath/internal/SaxonXPathRuleQuery.java @@ -19,7 +19,6 @@ import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.RootNode; import net.sourceforge.pmd.lang.rule.xpath.PmdXPathException; import net.sourceforge.pmd.lang.rule.xpath.PmdXPathException.Phase; -import net.sourceforge.pmd.lang.rule.xpath.XPathRule; import net.sourceforge.pmd.lang.rule.xpath.XPathVersion; import net.sourceforge.pmd.lang.rule.xpath.impl.XPathHandler; import net.sourceforge.pmd.properties.PropertyDescriptor; @@ -194,7 +193,7 @@ public class SaxonXPathRuleQuery { for (final PropertyDescriptor propertyDescriptor : properties.keySet()) { final String name = propertyDescriptor.name(); - if (!"xpath".equals(name) && !XPathRule.VERSION_DESCRIPTOR.name().equals(name)) { + if (!"xpath".equals(name)) { staticCtx.declareProperty(propertyDescriptor); } } diff --git a/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml b/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml index af716d45fb..40af25174a 100644 --- a/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml +++ b/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml @@ -20,7 +20,6 @@ 3 - 4 - 4 - 4 - 4 - 4 - 3 - 3 - 3 - Test 3 - - + Test Deprecated rule 3 - - + diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java index 5f69327654..13677e50e7 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java @@ -437,7 +437,6 @@ public class RuleDocGenerator { properties.remove(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR); properties.remove(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR); properties.remove(XPathRule.XPATH_DESCRIPTOR); - properties.remove(XPathRule.VERSION_DESCRIPTOR); if (!properties.isEmpty()) { lines.add("**This rule has the following properties:**"); diff --git a/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml b/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml index 08d13c7522..119a865b0d 100644 --- a/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml +++ b/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml @@ -57,7 +57,6 @@ Here might be <script>alert('XSS');</script> as well. And "quotes". 3 - 3 - 3 - 3 - 3 - 3 - - 1 - 3 - - 3 - 3 - java.lang.Enum,java.lang.Class @@ -1091,7 +1090,6 @@ class Foo { ]]> 3 - 2 - 3 - 1 - 3 - 3 - 3 - 3 - 3 -