From df43a9108cbc549d09800ebcca3bd5414816ff4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 27 Jun 2020 17:10:57 +0200 Subject: [PATCH 01/15] Add warning for missing language attribute Also, avoid parsing rulesets multiple times, otherwise the warning is duplicated --- .../net/sourceforge/pmd/RuleSetFactory.java | 23 +++++++++++++++---- .../sourceforge/pmd/rules/RuleFactory.java | 6 ++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index 4603f6f727..af571ff11a 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -8,9 +8,11 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.logging.Level; @@ -60,6 +62,8 @@ public class RuleSetFactory { private final boolean warnDeprecated; private final RuleSetFactoryCompatibility compatibilityFilter; + private final Map parsedRulesets = new HashMap<>(); + /** * @deprecated Use {@link RulesetsFactoryUtils#defaultFactory()} */ @@ -338,10 +342,15 @@ public class RuleSetFactory { private Rule createRule(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences) throws RuleSetNotFoundException { if (ruleSetReferenceId.isAllRules()) { - throw new IllegalArgumentException( - "Cannot parse a single Rule from an all Rule RuleSet reference: <" + ruleSetReferenceId + ">."); + throw new IllegalArgumentException("Cannot parse a single Rule from an all Rule RuleSet reference: <" + ruleSetReferenceId + ">."); + } + RuleSet ruleSet; + if (parsedRulesets.containsKey(ruleSetReferenceId)) { + ruleSet = parsedRulesets.get(ruleSetReferenceId); + } else { + ruleSet = createRuleSet(ruleSetReferenceId, withDeprecatedRuleReferences); + parsedRulesets.put(ruleSetReferenceId, ruleSet); } - RuleSet ruleSet = createRuleSet(ruleSetReferenceId, withDeprecatedRuleReferences); return ruleSet.getRuleByName(ruleSetReferenceId.getRuleName()); } @@ -610,12 +619,18 @@ public class RuleSetFactory { // Stop if we're looking for a particular Rule, and this element is not // it. if (StringUtils.isNotBlank(ruleSetReferenceId.getRuleName()) - && !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) { + && !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) { return; } Rule rule = new RuleFactory(resourceLoader).buildRule(ruleElement); rule.setRuleSetName(ruleSetBuilder.getName()); + if (StringUtils.isBlank(ruleElement.getAttribute("language"))) { + LOG.warning("Rule " + ruleSetReferenceId.toString() + "/" + rule.getName() + " does not mention attribute" + + " language='" + rule.getLanguage().getTerseName() + "'," + + " please mention it explicitly to be compatible with PMD 7"); + } + ruleSetBuilder.addRule(rule); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java index 217bfe2084..5ff8a4e41c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java @@ -152,9 +152,9 @@ public class RuleFactory { String name = ruleElement.getAttribute(NAME); RuleBuilder builder = new RuleBuilder(name, - resourceLoader, - ruleElement.getAttribute(CLASS), - ruleElement.getAttribute("language")); + resourceLoader, + ruleElement.getAttribute(CLASS), + ruleElement.getAttribute("language")); if (ruleElement.hasAttribute(MINIMUM_LANGUAGE_VERSION)) { builder.minimumLanguageVersion(ruleElement.getAttribute(MINIMUM_LANGUAGE_VERSION)); From a2506692b97e21457caf598093439c2f962a9265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 27 Jun 2020 17:24:23 +0200 Subject: [PATCH 02/15] Fix vm rules --- .../main/resources/category/vm/bestpractices.xml | 6 ++++-- pmd-vm/src/main/resources/category/vm/design.xml | 16 ++++++++++------ .../main/resources/category/vm/errorprone.xml | 6 ++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pmd-vm/src/main/resources/category/vm/bestpractices.xml b/pmd-vm/src/main/resources/category/vm/bestpractices.xml index c17fa32ed8..7b0cd89c0d 100644 --- a/pmd-vm/src/main/resources/category/vm/bestpractices.xml +++ b/pmd-vm/src/main/resources/category/vm/bestpractices.xml @@ -9,7 +9,8 @@ Rules which enforce generally accepted best practices. - 2 - - 3 - 3 - - 2 - diff --git a/pmd-vm/src/main/resources/category/vm/errorprone.xml b/pmd-vm/src/main/resources/category/vm/errorprone.xml index ad2c1b05bd..18eed15d75 100644 --- a/pmd-vm/src/main/resources/category/vm/errorprone.xml +++ b/pmd-vm/src/main/resources/category/vm/errorprone.xml @@ -9,7 +9,8 @@ Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors. - 2 - Date: Sat, 27 Jun 2020 17:58:56 +0200 Subject: [PATCH 03/15] Fix ruleset name for anonymous rsets --- pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index af571ff11a..fff794daa4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -626,7 +626,7 @@ public class RuleSetFactory { rule.setRuleSetName(ruleSetBuilder.getName()); if (StringUtils.isBlank(ruleElement.getAttribute("language"))) { - LOG.warning("Rule " + ruleSetReferenceId.toString() + "/" + rule.getName() + " does not mention attribute" + LOG.warning("Rule " + ruleSetReferenceId.getRuleSetFileName() + "/" + rule.getName() + " does not mention attribute" + " language='" + rule.getLanguage().getTerseName() + "'," + " please mention it explicitly to be compatible with PMD 7"); } From 15dbd030614cb1165d29af56c39a3f8e124c9aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 27 Jun 2020 17:40:18 +0200 Subject: [PATCH 04/15] Fix missing lang attributes in ruleset tests --- .../sourceforge/pmd/RuleSetFactoryTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java index 5d72338d36..0fad774784 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetFactoryTest.java @@ -604,7 +604,7 @@ public class RuleSetFactoryTest { } @Test(expected = IllegalArgumentException.class) - public void testIncorrectMinimumLanugageVersion() throws RuleSetNotFoundException { + public void testIncorrectMinimumLanguageVersion() throws RuleSetNotFoundException { loadFirstRule(INCORRECT_MINIMUM_LANGUAGE_VERSION); } @@ -633,7 +633,7 @@ public class RuleSetFactoryTest { } @Test(expected = IllegalArgumentException.class) - public void testIncorrectMaximumLanugageVersion() throws RuleSetNotFoundException { + public void testIncorrectMaximumLanguageVersion() throws RuleSetNotFoundException { loadFirstRule(INCORRECT_MAXIMUM_LANGUAGE_VERSION); } @@ -985,6 +985,7 @@ public class RuleSetFactoryTest { + " testdesc\n" + "\n" @@ -997,6 +998,7 @@ public class RuleSetFactoryTest { + " testdesc\n" + "\n" @@ -1034,6 +1036,7 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + "\n" @@ -1045,11 +1048,13 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + "\n" + "\n" + "\n" + "\n" + "\n" @@ -1059,6 +1064,7 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + "\n" + "\n" @@ -1080,6 +1086,7 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + "\n" + "3\n" @@ -1098,6 +1105,7 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + "\n" @@ -1110,7 +1118,8 @@ public class RuleSetFactoryTest { + "\n" + + "class=\"net.sourceforge.pmd.lang.rule.MockRule\" " + + "language=\"dummy\">\n" + ""; private static final String INCORRECT_LANGUAGE = "\n" @@ -1186,8 +1195,8 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + "\n" + ""; @@ -1216,6 +1225,7 @@ public class RuleSetFactoryTest { + "\n" + "testdesc\n" + " Date: Sat, 27 Jun 2020 17:57:21 +0200 Subject: [PATCH 05/15] Fix apex rules --- .../resources/category/apex/bestpractices.xml | 6 ++++ .../resources/category/apex/codestyle.xml | 8 +++++ .../main/resources/category/apex/design.xml | 33 ++++++++++++------- .../resources/category/apex/documentation.xml | 1 + .../resources/category/apex/errorprone.xml | 6 ++++ .../resources/category/apex/performance.xml | 3 ++ .../main/resources/category/apex/security.xml | 10 ++++++ .../sourceforge/pmd/RuleSetFactoryTest.java | 10 +++--- pmd-test/pom.xml | 6 +++- .../pmd/AbstractRuleSetFactoryTest.java | 4 +++ 10 files changed, 70 insertions(+), 17 deletions(-) diff --git a/pmd-apex/src/main/resources/category/apex/bestpractices.xml b/pmd-apex/src/main/resources/category/apex/bestpractices.xml index f91fa5095a..5c13033efc 100644 --- a/pmd-apex/src/main/resources/category/apex/bestpractices.xml +++ b/pmd-apex/src/main/resources/category/apex/bestpractices.xml @@ -10,6 +10,7 @@ Rules which enforce generally accepted best practices. - - - - - - - - - - - \n" + private static final String REFERENCE_TO_DEPRECATED_RULE = "\n" + "\n" + "testdesc\n" - + "\n" + - ""; + + "\n" + + ""; private static final String REFERENCE_TO_RULESET_WITH_DEPRECATED_RULE = "\n" + "\n" + "testdesc\n" - + "\n" + - ""; + + "\n" + + ""; private static final String DFA = "\n" + "\n" diff --git a/pmd-test/pom.xml b/pmd-test/pom.xml index 22f3af5c00..24b5bbbcff 100644 --- a/pmd-test/pom.xml +++ b/pmd-test/pom.xml @@ -44,7 +44,11 @@ commons-io commons-io - + + com.github.stefanbirkner + system-rules + compile + org.mockito mockito-core diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java b/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java index 23834d26b2..164e0590a3 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/AbstractRuleSetFactoryTest.java @@ -33,6 +33,7 @@ import javax.xml.parsers.SAXParserFactory; import org.apache.commons.io.FilenameUtils; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemErrRule; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; @@ -50,6 +51,9 @@ import net.sourceforge.pmd.util.ResourceLoader; * subclassed for each language. */ public abstract class AbstractRuleSetFactoryTest { + @org.junit.Rule + public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests(); + private static SAXParserFactory saxParserFactory; private static ValidateDefaultHandler validateDefaultHandler; private static SAXParser saxParser; From 3dd49db9ccb5cd8845d7a0c17989c7865699e21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 27 Jun 2020 18:05:46 +0200 Subject: [PATCH 06/15] Reorder attributes (name first) --- .../main/resources/category/apex/design.xml | 44 +++++++++---------- .../net/sourceforge/pmd/RuleSetFactory.java | 1 + .../resources/category/vm/bestpractices.xml | 8 ++-- .../src/main/resources/category/vm/design.xml | 20 ++++----- .../main/resources/category/vm/errorprone.xml | 8 ++-- 5 files changed, 41 insertions(+), 40 deletions(-) diff --git a/pmd-apex/src/main/resources/category/apex/design.xml b/pmd-apex/src/main/resources/category/apex/design.xml index 156c08c2c5..b79afaa0e8 100644 --- a/pmd-apex/src/main/resources/category/apex/design.xml +++ b/pmd-apex/src/main/resources/category/apex/design.xml @@ -9,8 +9,8 @@ Rules that help you discover design issues. - - - - - - - - - - - ."); } RuleSet ruleSet; + // java8: computeIfAbsent if (parsedRulesets.containsKey(ruleSetReferenceId)) { ruleSet = parsedRulesets.get(ruleSetReferenceId); } else { diff --git a/pmd-vm/src/main/resources/category/vm/bestpractices.xml b/pmd-vm/src/main/resources/category/vm/bestpractices.xml index 7b0cd89c0d..f758c2bab4 100644 --- a/pmd-vm/src/main/resources/category/vm/bestpractices.xml +++ b/pmd-vm/src/main/resources/category/vm/bestpractices.xml @@ -9,8 +9,8 @@ Rules which enforce generally accepted best practices. - 2 - - 3 - 3 - - 2 - - 2 - Date: Sat, 27 Jun 2020 18:13:21 +0200 Subject: [PATCH 07/15] Update release notes --- docs/pages/release_notes.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index b8f8783555..addf7b5b50 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -18,6 +18,12 @@ This is a {{ site.pmd.release_type }} release. ### API Changes +* In PMD 7, The `language` attribute will be required on all `rule` elements that +declare a new rule. Some base rule classes set the language implicitly in their +constructor, and so this is not required in all cases for the rule to work. But this +behavior will be discontinued in PMD 7, so missing `language` attributes are now +reported as a forward compatibility warning. + ### External Contributions {% endtocmaker %} From b7104654110720b25b74ed04d631994cfabeba67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 6 Jul 2020 13:54:10 +0200 Subject: [PATCH 08/15] Fix warning --- pmd-java/src/main/resources/category/java/bestpractices.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 9c2dbf7138..622790bb28 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -308,6 +308,7 @@ public class Foo { Date: Mon, 6 Jul 2020 14:02:21 +0200 Subject: [PATCH 09/15] Fix other offenders --- .../resources/rulesets/apex/metrics_test.xml | 3 ++ .../resources/category/java/bestpractices.xml | 13 ++++++++ .../resources/category/java/codestyle.xml | 17 +++++++++++ .../main/resources/category/java/design.xml | 26 ++++++++++++++++ .../resources/category/java/documentation.xml | 3 ++ .../resources/category/java/errorprone.xml | 30 +++++++++++++++++++ .../category/java/multithreading.xml | 3 ++ .../resources/category/java/performance.xml | 17 ++++++++++- .../main/resources/category/java/security.xml | 2 ++ .../resources/rulesets/java/metrics_test.xml | 11 +++++++ .../category/ecmascript/bestpractices.xml | 1 + .../main/resources/category/jsp/codestyle.xml | 1 + .../main/resources/category/jsp/design.xml | 1 + .../main/resources/category/jsp/security.xml | 1 + .../main/resources/category/vf/security.xml | 2 ++ 15 files changed, 130 insertions(+), 1 deletion(-) diff --git a/pmd-apex/src/test/resources/rulesets/apex/metrics_test.xml b/pmd-apex/src/test/resources/rulesets/apex/metrics_test.xml index cbb1c7324f..2064793b70 100644 --- a/pmd-apex/src/test/resources/rulesets/apex/metrics_test.xml +++ b/pmd-apex/src/test/resources/rulesets/apex/metrics_test.xml @@ -10,16 +10,19 @@ diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 622790bb28..fd675b0b46 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -35,6 +35,7 @@ public abstract class Foo { @@ -483,6 +489,7 @@ public void bar(String string) { diff --git a/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml b/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml index 5b39319cd2..28c82246d6 100644 --- a/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml +++ b/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml @@ -37,6 +37,7 @@ with (object) { Date: Mon, 20 Jul 2020 00:12:40 +0200 Subject: [PATCH 10/15] Update to kotest --- pmd-java/pom.xml | 9 ++----- .../lang/java/ast/ASTCatchStatementTest.kt | 6 ++--- .../pmd/lang/java/ast/Java11Test.kt | 4 +-- .../pmd/lang/java/ast/KotlinTestingDsl.kt | 8 +++--- .../pmd/lang/java/ast/ParserTestSpec.kt | 10 ++++--- .../lang/java/ast/SwitchExpressionTests.kt | 2 +- .../pmd/lang/java/ast/WildcardBoundsTest.kt | 2 +- pmd-lang-test/pom.xml | 10 +++++-- .../pmd/cpd/test/CpdTextComparisonTest.kt | 2 +- .../pmd/lang/ast/test/AstMatcherDslAdapter.kt | 6 ++--- .../pmd/lang/ast/test/TestUtils.kt | 6 ++--- pmd-modelica/pom.xml | 9 ++----- .../lang/modelica/ast/ModelicaCoordsTest.kt | 6 ++--- pmd-scala-modules/pmd-scala-common/pom.xml | 9 ++----- .../pmd/lang/scala/ast/ScalaTreeTests.kt | 4 +-- pom.xml | 27 +++++++++++-------- 16 files changed, 59 insertions(+), 61 deletions(-) diff --git a/pmd-java/pom.xml b/pmd-java/pom.xml index f3e4264c30..5b84ecba96 100644 --- a/pmd-java/pom.xml +++ b/pmd-java/pom.xml @@ -172,13 +172,8 @@ test - io.kotlintest - kotlintest-assertions - test - - - io.kotlintest - kotlintest-core + io.kotest + kotest-assertions-core-jvm test diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTCatchStatementTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTCatchStatementTest.kt index 8c4d34181f..f6534b1415 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTCatchStatementTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTCatchStatementTest.kt @@ -1,8 +1,8 @@ package net.sourceforge.pmd.lang.java.ast -import io.kotlintest.matchers.collections.shouldContainExactly -import io.kotlintest.should -import io.kotlintest.shouldBe +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.should +import io.kotest.matchers.shouldBe import net.sourceforge.pmd.lang.java.ast.JavaVersion.* import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Earliest import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Latest diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/Java11Test.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/Java11Test.kt index 110b01c3e1..4f7e3fa53d 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/Java11Test.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/Java11Test.kt @@ -1,5 +1,5 @@ -import io.kotlintest.shouldBe +import io.kotest.matchers.shouldBe import net.sourceforge.pmd.lang.java.ast.* import net.sourceforge.pmd.lang.java.ast.JavaVersion.* import net.sourceforge.pmd.lang.java.ast.JavaVersion.Companion.Latest @@ -88,4 +88,4 @@ class Java11Test : ParserTestSpec({ } } -}) \ No newline at end of file +}) diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt index dc34b3db28..e3401c2c37 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/KotlinTestingDsl.kt @@ -1,7 +1,7 @@ package net.sourceforge.pmd.lang.java.ast -import io.kotlintest.matchers.string.shouldContain -import io.kotlintest.shouldThrow +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.string.shouldContain import net.sourceforge.pmd.lang.ast.Node import net.sourceforge.pmd.lang.ast.test.Assertions import net.sourceforge.pmd.lang.ast.test.NodeSpec @@ -56,7 +56,7 @@ enum class JavaVersion : Comparable { * * These are implicitly used by [matchExpr] and [matchStmt], which specify a matcher directly * on the strings, using their type parameter and the info in this test context to parse, find - * the node, and execute the matcher in a single call. These may be used by [io.kotlintest.should], + * the node, and execute the matcher in a single call. These may be used by [io.kotest.matchers.should], * e.g. * * parserTest("Test ShiftExpression operator") { @@ -69,7 +69,7 @@ enum class JavaVersion : Comparable { * Import statements in the parsing contexts can be configured by adding types to [importedTypes], * or strings to [otherImports]. * - * Technically the utilities provided by this class may be used outside of [io.kotlintest.specs.FunSpec]s, + * Technically the utilities provided by this class may be used outside of [io.kotest.specs.FunSpec]s, * e.g. in regular JUnit tests, but I think we should strive to uniformize our testing style, * especially since KotlinTest defines so many. * diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt index 741b2bff65..dc65e36790 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt @@ -1,10 +1,12 @@ package net.sourceforge.pmd.lang.java.ast -import io.kotlintest.AbstractSpec -import io.kotlintest.TestContext -import io.kotlintest.TestType +import io.kotest.core.config.Project +import io.kotest.core.spec.style.DslDrivenSpec +import io.kotest.core.test.TestContext +import io.kotest.core.test.TestName +import io.kotest.core.test.TestType import net.sourceforge.pmd.lang.ast.test.Assertions -import io.kotlintest.should as kotlintestShould +import io.kotest.matchers.should as kotlintestShould /** * Base class for grammar tests that use the DSL. Tests are layered into diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/SwitchExpressionTests.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/SwitchExpressionTests.kt index 4dd28b2bb2..7455d07bc8 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/SwitchExpressionTests.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/SwitchExpressionTests.kt @@ -4,7 +4,7 @@ package net.sourceforge.pmd.lang.java.ast -import io.kotlintest.shouldBe +import io.kotest.matchers.shouldBe /** * @author Clément Fournier diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/WildcardBoundsTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/WildcardBoundsTest.kt index b62a9ac638..c5f174e2f7 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/WildcardBoundsTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/WildcardBoundsTest.kt @@ -1,6 +1,6 @@ package net.sourceforge.pmd.lang.java.ast -import io.kotlintest.shouldBe +import io.kotest.matchers.shouldBe class WildcardBoundsTest : ParserTestSpec({ diff --git a/pmd-lang-test/pom.xml b/pmd-lang-test/pom.xml index 6fa4a14da8..0ffd937ce3 100644 --- a/pmd-lang-test/pom.xml +++ b/pmd-lang-test/pom.xml @@ -105,8 +105,14 @@ of the pmd-lang-test module --> - io.kotlintest - kotlintest-assertions + io.kotest + kotest-assertions-core-jvm + compile + + + + io.kotest + kotest-runner-junit5-jvm compile diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/cpd/test/CpdTextComparisonTest.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/cpd/test/CpdTextComparisonTest.kt index 2b198984cd..8335f58b12 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/cpd/test/CpdTextComparisonTest.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/cpd/test/CpdTextComparisonTest.kt @@ -4,7 +4,7 @@ package net.sourceforge.pmd.cpd.test -import io.kotlintest.shouldThrow +import io.kotest.assertions.throwables.shouldThrow import net.sourceforge.pmd.cpd.SourceCode import net.sourceforge.pmd.cpd.TokenEntry import net.sourceforge.pmd.cpd.Tokenizer diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt index 419fafabff..3d90212be9 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt @@ -26,7 +26,7 @@ object NodeTreeLikeAdapter : DoublyLinkedTreeLikeAdapter { /** A subtree matcher written in the DSL documented on [TreeNodeWrapper]. */ typealias NodeSpec = TreeNodeWrapper.() -> Unit -/** A function feedable to [io.kotlintest.should], which fails the test if an [AssertionError] is thrown. */ +/** A function feedable to [io.kotest.matchers.should], which fails the test if an [AssertionError] is thrown. */ typealias Assertions = (M) -> Unit /** A shorthand for [baseShouldMatchSubtree] providing the [NodeTreeLikeAdapter]. */ @@ -37,7 +37,7 @@ inline fun Node?.shouldMatchNode(ignoreChildren: Boolean = fa /** * Returns [an assertion function][Assertions] asserting that its parameter conforms to the given [NodeSpec]. * - * Use it with [io.kotlintest.should], e.g. `node should matchNode {}`. + * Use it with [io.kotest.matchers.should], e.g. `node should matchNode {}`. * * See also the samples on [TreeNodeWrapper]. * @@ -50,7 +50,7 @@ inline fun Node?.shouldMatchNode(ignoreChildren: Boolean = fa * Assertions may consist of [NWrapper.child] calls, which perform the same type of node * matching on a child of the tested node. * - * @return A matcher for AST nodes, suitable for use by [io.kotlintest.should]. + * @return A matcher for AST nodes, suitable for use by [io.kotest.matchers.should]. */ inline fun matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: NodeSpec) : Assertions = { it.shouldMatchNode(ignoreChildren, nodeSpec) } diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt index 779308632c..47dba9b1d4 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/TestUtils.kt @@ -4,10 +4,10 @@ package net.sourceforge.pmd.lang.ast.test -import io.kotlintest.should +import io.kotest.matchers.should import kotlin.reflect.KCallable import kotlin.reflect.jvm.isAccessible -import io.kotlintest.shouldBe as ktShouldBe +import io.kotest.matchers.shouldBe as ktShouldBe /** * Extension to add the name of a property to error messages. @@ -48,7 +48,7 @@ private fun assertWrapper(callable: KCallable, right: V, asserter: (N, * have to use the name of the getter instead of that of the generated * property (with the get prefix). * - * If this conflicts with [io.kotlintest.shouldBe], use the equivalent [shouldEqual] + * If this conflicts with [io.kotest.matchers.shouldBe], use the equivalent [shouldEqual] * */ infix fun KCallable.shouldBe(expected: V?) = this.shouldEqual(expected) diff --git a/pmd-modelica/pom.xml b/pmd-modelica/pom.xml index 3c3f5de689..15ccca423b 100644 --- a/pmd-modelica/pom.xml +++ b/pmd-modelica/pom.xml @@ -120,13 +120,8 @@ test - io.kotlintest - kotlintest-assertions - test - - - io.kotlintest - kotlintest-core + io.kotest + kotest-assertions-core-jvm test diff --git a/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt b/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt index dfa62e29b5..7cc7f3855c 100644 --- a/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt +++ b/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt @@ -4,9 +4,9 @@ package net.sourceforge.pmd.lang.modelica.ast -import io.kotlintest.should -import io.kotlintest.shouldBe -import io.kotlintest.specs.AbstractFunSpec +import io.kotest.matchers.should +import io.kotest.matchers.shouldBe +import io.kotest.specs.AbstractFunSpec import net.sourceforge.pmd.lang.LanguageRegistry import net.sourceforge.pmd.lang.ast.Node import net.sourceforge.pmd.lang.ast.test.matchNode diff --git a/pmd-scala-modules/pmd-scala-common/pom.xml b/pmd-scala-modules/pmd-scala-common/pom.xml index 4304adf8c6..e7c639d0df 100644 --- a/pmd-scala-modules/pmd-scala-common/pom.xml +++ b/pmd-scala-modules/pmd-scala-common/pom.xml @@ -140,13 +140,8 @@ test - io.kotlintest - kotlintest-assertions - test - - - io.kotlintest - kotlintest-core + io.kotest + kotest-assertions-core-jvm test diff --git a/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt b/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt index 32d808af26..5b18e1d8e5 100644 --- a/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt +++ b/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt @@ -4,8 +4,8 @@ package net.sourceforge.pmd.lang.scala.ast -import io.kotlintest.should -import io.kotlintest.specs.AbstractFunSpec +import io.kotest.matchers.should +import io.kotest.specs.AbstractFunSpec import net.sourceforge.pmd.lang.LanguageRegistry import net.sourceforge.pmd.lang.ast.Node import net.sourceforge.pmd.lang.ast.test.matchNode diff --git a/pom.xml b/pom.xml index 4e0026061d..539d5e7174 100644 --- a/pom.xml +++ b/pom.xml @@ -86,7 +86,7 @@ ${maven.compiler.test.target} 1.3.0 - 3.1.8 + 4.1.2 0.10.1 @@ -269,9 +269,9 @@ - io.kotlintest - kotlintest-runner-junit5 - ${kotlintest.version} + io.kotest + kotest-runner-junit5-jvm + ${kotest.version} @@ -826,17 +826,22 @@ ${kotlin.version} test - - io.kotlintest - kotlintest-assertions - ${kotlintest.version} + io.kotest + kotest-runner-junit5-jvm + ${kotest.version} test - io.kotlintest - kotlintest-core - ${kotlintest.version} + io.kotest + kotest-assertions-core-jvm + ${kotest.version} + test + + + io.kotest + kotest-property-jvm + ${kotest.version} test From 6d73eaf3c74aa73d513cdfbf4f8076b2877e0303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 20 Jul 2020 16:06:13 +0200 Subject: [PATCH 11/15] Fix custom parser spec Add kotest-runner-junit5-jvm as a dependency, in pmd-java. This makes kotest discoverable as Junit tests, which IDEs can pick up on (at least IntelliJ does). Update kotlin version to latest stable (1.3.72) --- pmd-java/pom.xml | 8 ++- .../pmd/lang/java/ast/ParserTestSpec.kt | 61 +++++++++++++------ pmd-lang-test/pom.xml | 5 -- pmd-modelica/pom.xml | 2 +- .../lang/modelica/ast/ModelicaCoordsTest.kt | 4 +- pmd-scala-modules/pmd-scala-common/pom.xml | 2 +- .../pmd/lang/scala/ast/ScalaTreeTests.kt | 4 +- pom.xml | 8 +-- 8 files changed, 55 insertions(+), 39 deletions(-) diff --git a/pmd-java/pom.xml b/pmd-java/pom.xml index 5b84ecba96..250db350df 100644 --- a/pmd-java/pom.xml +++ b/pmd-java/pom.xml @@ -176,6 +176,12 @@ kotest-assertions-core-jvm test + + + io.kotest + kotest-runner-junit5-jvm + test + com.google.guava guava @@ -188,7 +194,7 @@ org.jetbrains.kotlin - kotlin-test + kotlin-test-junit test diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt index dc65e36790..c877e8e860 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ParserTestSpec.kt @@ -2,9 +2,14 @@ package net.sourceforge.pmd.lang.java.ast import io.kotest.core.config.Project import io.kotest.core.spec.style.DslDrivenSpec +import io.kotest.core.spec.style.scopes.Lifecycle +import io.kotest.core.spec.style.scopes.RootScope +import io.kotest.core.spec.style.scopes.RootTestRegistration +import io.kotest.core.test.TestCaseConfig import io.kotest.core.test.TestContext import io.kotest.core.test.TestName import io.kotest.core.test.TestType +import io.kotest.runner.junit.platform.IntelliMarker import net.sourceforge.pmd.lang.ast.test.Assertions import io.kotest.matchers.should as kotlintestShould @@ -16,14 +21,23 @@ import io.kotest.matchers.should as kotlintestShould * * @author Clément Fournier */ -abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec() { +abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : DslDrivenSpec(), RootScope, IntelliMarker { init { body() } - fun test(name: String, test: TestContext.() -> Unit) = - addTestCase(name, test, defaultTestCaseConfig, TestType.Test) + override fun lifecycle(): Lifecycle = Lifecycle.from(this) + override fun defaultConfig(): TestCaseConfig = actualDefaultConfig() + override fun registration(): RootTestRegistration = RootTestRegistration.from(this) + + fun test(name: String, disabled: Boolean = false, test: suspend TestContext.() -> Unit) = + registration().addTest( + name = TestName(name), + xdisabled = disabled, + test = test, + config = actualDefaultConfig() + ) /** * Defines a group of tests that should be named similarly, @@ -36,14 +50,19 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec() * regression tests without bothering to find a name. * * @param name Name of the container test - * @param spec Assertions. Each call to [io.kotlintest.should] on a string + * @param spec Assertions. Each call to [io.kotest.matchers.should] on a string * receiver is replaced by a [GroupTestCtx.should], which creates a * new parser test. * */ fun parserTestGroup(name: String, - spec: GroupTestCtx.() -> Unit) = - addTestCase(name, { GroupTestCtx(this).spec() }, defaultTestCaseConfig, TestType.Container) + disabled: Boolean = false, + spec: suspend GroupTestCtx.() -> Unit) = + registration().addContainerTest( + name = TestName(name), + test = { GroupTestCtx(this).spec() }, + xdisabled = disabled + ) /** * Defines a group of tests that should be named similarly. @@ -55,14 +74,14 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec() * * @param name Name of the container test * @param javaVersion Language versions to use when parsing - * @param spec Assertions. Each call to [io.kotlintest.should] on a string + * @param spec Assertions. Each call to [io.kotest.matchers.should] on a string * receiver is replaced by a [GroupTestCtx.should], which creates a * new parser test. * */ fun parserTest(name: String, javaVersion: JavaVersion = JavaVersion.Latest, - spec: GroupTestCtx.VersionedTestCtx.() -> Unit) = + spec: suspend GroupTestCtx.VersionedTestCtx.() -> Unit) = parserTest(name, listOf(javaVersion), spec) /** @@ -76,44 +95,46 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec() * * @param name Name of the container test * @param javaVersions Language versions for which to generate tests - * @param spec Assertions. Each call to [io.kotlintest.should] on a string + * @param spec Assertions. Each call to [io.kotest.matchers.should] on a string * receiver is replaced by a [GroupTestCtx.should], which creates a * new parser test. */ fun parserTest(name: String, javaVersions: List, - spec: GroupTestCtx.VersionedTestCtx.() -> Unit) = + spec: suspend GroupTestCtx.VersionedTestCtx.() -> Unit) = parserTestGroup(name) { onVersions(javaVersions) { spec() } } - private fun containedParserTestImpl( + private suspend fun containedParserTestImpl( context: TestContext, name: String, javaVersion: JavaVersion, assertions: ParserTestCtx.() -> Unit) { context.registerTestCase( - name = name, - spec = this, + name = TestName(name), test = { ParserTestCtx(javaVersion).assertions() }, - config = defaultTestCaseConfig, + config = actualDefaultConfig(), type = TestType.Test ) } + private fun actualDefaultConfig() = + defaultTestConfig ?: defaultTestCaseConfig() + ?: Project.testCaseConfig() + inner class GroupTestCtx(private val context: TestContext) { - fun onVersions(javaVersions: List, spec: VersionedTestCtx.() -> Unit) { + suspend fun onVersions(javaVersions: List, spec: suspend VersionedTestCtx.() -> Unit) { javaVersions.forEach { javaVersion -> context.registerTestCase( - name = "Java ${javaVersion.pmdName}", - spec = this@ParserTestSpec, + name = TestName("Java ${javaVersion.pmdName}"), test = { VersionedTestCtx(this, javaVersion).spec() }, - config = defaultTestCaseConfig, + config = actualDefaultConfig(), type = TestType.Container ) } @@ -121,11 +142,11 @@ abstract class ParserTestSpec(body: ParserTestSpec.() -> Unit) : AbstractSpec() inner class VersionedTestCtx(private val context: TestContext, javaVersion: JavaVersion) : ParserTestCtx(javaVersion) { - infix fun String.should(matcher: Assertions) { + suspend infix fun String.should(matcher: Assertions) { containedParserTestImpl(context, "'$this'", javaVersion = javaVersion) { this@should kotlintestShould matcher } } } } -} \ No newline at end of file +} diff --git a/pmd-lang-test/pom.xml b/pmd-lang-test/pom.xml index 0ffd937ce3..a7983c9e49 100644 --- a/pmd-lang-test/pom.xml +++ b/pmd-lang-test/pom.xml @@ -115,11 +115,6 @@ kotest-runner-junit5-jvm compile - - org.jetbrains.kotlin - kotlin-test - compile - org.jetbrains diff --git a/pmd-modelica/pom.xml b/pmd-modelica/pom.xml index 15ccca423b..6644b84b6e 100644 --- a/pmd-modelica/pom.xml +++ b/pmd-modelica/pom.xml @@ -111,7 +111,7 @@ org.jetbrains.kotlin - kotlin-test + kotlin-test-junit test diff --git a/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt b/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt index 7cc7f3855c..d2b0bc9ac6 100644 --- a/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt +++ b/pmd-modelica/src/test/kotlin/net/sourceforge/pmd/lang/modelica/ast/ModelicaCoordsTest.kt @@ -4,16 +4,16 @@ package net.sourceforge.pmd.lang.modelica.ast +import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.should import io.kotest.matchers.shouldBe -import io.kotest.specs.AbstractFunSpec import net.sourceforge.pmd.lang.LanguageRegistry import net.sourceforge.pmd.lang.ast.Node import net.sourceforge.pmd.lang.ast.test.matchNode import net.sourceforge.pmd.lang.ast.test.shouldBe import java.io.StringReader -class ModelicaCoordsTest : AbstractFunSpec({ +class ModelicaCoordsTest : FunSpec({ test("Test line/column numbers for implicit nodes") { diff --git a/pmd-scala-modules/pmd-scala-common/pom.xml b/pmd-scala-modules/pmd-scala-common/pom.xml index e7c639d0df..246d1ae27d 100644 --- a/pmd-scala-modules/pmd-scala-common/pom.xml +++ b/pmd-scala-modules/pmd-scala-common/pom.xml @@ -151,7 +151,7 @@ org.jetbrains.kotlin - kotlin-test + kotlin-test-junit test diff --git a/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt b/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt index 5b18e1d8e5..48867dd383 100644 --- a/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt +++ b/pmd-scala-modules/pmd-scala-common/src/test/kotlin/net/sourceforge/pmd/lang/scala/ast/ScalaTreeTests.kt @@ -4,15 +4,15 @@ package net.sourceforge.pmd.lang.scala.ast +import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.should -import io.kotest.specs.AbstractFunSpec import net.sourceforge.pmd.lang.LanguageRegistry import net.sourceforge.pmd.lang.ast.Node import net.sourceforge.pmd.lang.ast.test.matchNode import net.sourceforge.pmd.lang.ast.test.shouldBe import java.io.StringReader -class ScalaTreeTests : AbstractFunSpec({ +class ScalaTreeTests : FunSpec({ test("Test line/column numbers") { diff --git a/pom.xml b/pom.xml index 539d5e7174..60bd8bc977 100644 --- a/pom.xml +++ b/pom.xml @@ -85,7 +85,7 @@ ${maven.compiler.test.target} - 1.3.0 + 1.3.72 4.1.2 0.10.1 @@ -820,12 +820,6 @@ ${kotlin.version} test - - org.jetbrains.kotlin - kotlin-test - ${kotlin.version} - test - io.kotest kotest-runner-junit5-jvm From 3621bc30693dd7da72d5e89702b6903f461fcc2d Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 30 Jul 2020 10:14:33 +0200 Subject: [PATCH 12/15] [doc] Update writing rules intro for explaining language attribute --- .../pmd/userdocs/extending/writing_rules_intro.md | 5 +++++ docs/pages/release_notes.md | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/pages/pmd/userdocs/extending/writing_rules_intro.md b/docs/pages/pmd/userdocs/extending/writing_rules_intro.md index e98398684f..2c0b4dc478 100644 --- a/docs/pages/pmd/userdocs/extending/writing_rules_intro.md +++ b/docs/pages/pmd/userdocs/extending/writing_rules_intro.md @@ -120,6 +120,11 @@ Example: ``` +{% include note.html content="In PMD 7, the `language` attribute will be required on all `rule` + elements that declare a new rule. Some base rule classes set the language implicitly in their + constructor, and so this is not required in all cases for the rule to work. But this + behavior will be discontinued in PMD 7, so missing `language` attributes are + reported beginning with PMD 6.27.0 as a forward compatibility warning." %} ## Resource index diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 09f7fb58ce..07ddb85d80 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -21,11 +21,11 @@ This is a {{ site.pmd.release_type }} release. ### API Changes -* In PMD 7, The `language` attribute will be required on all `rule` elements that -declare a new rule. Some base rule classes set the language implicitly in their -constructor, and so this is not required in all cases for the rule to work. But this -behavior will be discontinued in PMD 7, so missing `language` attributes are now -reported as a forward compatibility warning. +* XML rule definition in rulesets: In PMD 7, the `language` attribute will be required on all `rule` + elements that declare a new rule. Some base rule classes set the language implicitly in their + constructor, and so this is not required in all cases for the rule to work. But this + behavior will be discontinued in PMD 7, so missing `language` attributes are now + reported as a forward compatibility warning. ### External Contributions From e663d5dd9b0e1a2ceec07903deb82edd6b008e0c Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 30 Jul 2020 10:18:18 +0200 Subject: [PATCH 13/15] [doc] Update release notes, fixes #724 --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 07ddb85d80..dbcda98037 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -18,6 +18,8 @@ This is a {{ site.pmd.release_type }} release. * apex-bestpractices * [#2626](https://github.com/pmd/pmd/issues/2626): \[apex] UnusedLocalVariable - false positive on case insensitivity allowed in Apex +* core + * [#724](https://github.com/pmd/pmd/issues/724): \[core] Avoid parsing rulesets multiple times ### API Changes From b5315f556cbfc9da05b2b3ac63d6363bbfea4c9b Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 30 Jul 2020 10:30:53 +0200 Subject: [PATCH 14/15] [doc] Update release notes, fixes #2653 --- docs/pages/release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index c095e78be5..2fe35bef3e 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -30,6 +30,7 @@ This is a {{ site.pmd.release_type }} release. * core * [#710](https://github.com/pmd/pmd/issues/710): \[core] Review used dependencies * [#2594](https://github.com/pmd/pmd/issues/2594): \[core] Update exec-maven-plugin and align it in all project + * [#2653](https://github.com/pmd/pmd/issues/2653): \[lang-test] Upgrade kotlintest to Kotest * java-design * [#2174](https://github.com/pmd/pmd/issues/2174): \[java] LawOfDemeter: False positive with 'this' pointer * [#2189](https://github.com/pmd/pmd/issues/2189): \[java] LawOfDemeter: False positive when casting to derived class From a5eb60243dfa12d3ca2cc8eb3bfff091654c66ab Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 30 Jul 2020 11:31:29 +0200 Subject: [PATCH 15/15] [java] Deprecate ASTThrowStatement#getFirstClassOrInterfaceTypeImage() Refs #2665 --- docs/pages/release_notes.md | 6 ++++++ .../pmd/lang/java/ast/ASTThrowStatement.java | 3 +++ .../java/rule/design/ExceptionAsFlowControlRule.java | 10 ++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index e5edabd1ea..a81826574e 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -30,6 +30,12 @@ This is a {{ site.pmd.release_type }} release. behavior will be discontinued in PMD 7, so missing `language` attributes are now reported as a forward compatibility warning. +#### Deprecated API + +##### For removal + +* {% jdoc !!pmd-java::lang.java.ast.ASTThrowStatement#getFirstClassOrInterfaceTypeImage() %} + ### External Contributions * [#2677](https://github.com/pmd/pmd/pull/2677): \[java] RedundantFieldInitializer can not detect a special case for char initialize: `char foo = '\0';` - [Mykhailo Palahuta](https://github.com/Drofff) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java index bbf4c23b62..65f83d7cb3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTThrowStatement.java @@ -37,7 +37,10 @@ public class ASTThrowStatement extends AbstractJavaNode { * * @return the image of the first ASTClassOrInterfaceType node found or * null + * @deprecated This method is too specific and doesn't support all cases. + * It will be removed with PMD 7. */ + @Deprecated public final String getFirstClassOrInterfaceTypeImage() { final ASTClassOrInterfaceType t = getFirstDescendantOfType(ASTClassOrInterfaceType.class); return t == null ? null : t.getImage(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExceptionAsFlowControlRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExceptionAsFlowControlRule.java index a18f218d7c..9e491325cf 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExceptionAsFlowControlRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExceptionAsFlowControlRule.java @@ -6,6 +6,8 @@ package net.sourceforge.pmd.lang.java.rule.design; import java.util.List; +import org.apache.commons.lang3.StringUtils; + import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter; @@ -35,8 +37,7 @@ public class ExceptionAsFlowControlRule extends AbstractJavaRule { ASTFormalParameter fp = (ASTFormalParameter) catchStmt.getChild(0); ASTType type = fp.getFirstDescendantOfType(ASTType.class); ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class); - if (node.getFirstClassOrInterfaceTypeImage() != null - && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) { + if (isExceptionOfTypeThrown(node, name.getImage())) { addViolation(data, name); } } @@ -44,4 +45,9 @@ public class ExceptionAsFlowControlRule extends AbstractJavaRule { return data; } + private boolean isExceptionOfTypeThrown(ASTThrowStatement throwStatement, String typeName) { + final ASTClassOrInterfaceType t = throwStatement.getFirstDescendantOfType(ASTClassOrInterfaceType.class); + String thrownTypeName = t == null ? null : t.getImage(); + return StringUtils.equals(thrownTypeName, typeName); + } }