From 70eac686e40cf65543c5b03a31480d07ccd75d24 Mon Sep 17 00:00:00 2001 From: Vincent Galloy Date: Wed, 5 Jan 2022 13:45:59 +0100 Subject: [PATCH 1/6] [java] New rule: FinalInterfaceMethodParameterIsUnclear Add the rule to check that public method in interface do not use final qualifier which is unclear --- .../resources/category/java/bestpractices.xml | 35 +++++++++++ .../resources/rulesets/java/quickstart.xml | 1 + ...InterfaceMethodParameterIsUnclearTest.java | 11 ++++ ...FinalInterfaceMethodParameterIsUnclear.xml | 62 +++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 95e6477a70..4850136d1c 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -541,6 +541,41 @@ return a; + + + Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it. + + 1 + + + + + + + + + + + + + diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java new file mode 100644 index 0000000000..9665917419 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java @@ -0,0 +1,11 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.rule.bestpractices; + +import net.sourceforge.pmd.testframework.PmdRuleTst; + +public class FinalInterfaceMethodParameterIsUnclearTest extends PmdRuleTst { + // no additional unit tests +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml new file mode 100644 index 0000000000..1b220dd42f --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml @@ -0,0 +1,62 @@ + + + + + Final is not allowed in interface public method + 1 + + Using final in public (non default) interface method is unclear + + + + + + Final is allowed in interface static method + 0 + + + + + Final is allowed in interface private method + 0 + + + + + Final is allowed in interface default method + 0 + + + + + Final is allowed in interface inner class method + 0 + + + + From b1c22db14ad94fc2ae33578065a942f0d3703cba Mon Sep 17 00:00:00 2001 From: Vincent Galloy Date: Thu, 6 Jan 2022 09:03:58 +0100 Subject: [PATCH 2/6] Rename to FinalParameterInAbstractMethod and target all methods --- .../resources/category/java/bestpractices.xml | 36 ------------------- .../resources/category/java/codestyle.xml | 33 +++++++++++++++++ .../resources/rulesets/java/quickstart.xml | 2 +- .../FinalParameterInAbstractMethod.java} | 4 +-- .../xml/FinalParameterInAbstractMethod.xml} | 15 +++++++- 5 files changed, 50 insertions(+), 40 deletions(-) rename pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/{bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java => codestyle/FinalParameterInAbstractMethod.java} (56%) rename pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/{bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml => codestyle/xml/FinalParameterInAbstractMethod.xml} (78%) diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 4850136d1c..b204e0a9ed 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -541,42 +541,6 @@ return a; - - - Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it. - - 1 - - - - - - - - - - - - - + + + Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it. + + 1 + + + + + + + + + + + + + - @@ -94,6 +93,7 @@ + diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java similarity index 56% rename from pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java rename to pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java index 9665917419..b1751d5bca 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/FinalInterfaceMethodParameterIsUnclearTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java @@ -2,10 +2,10 @@ * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ -package net.sourceforge.pmd.lang.java.rule.bestpractices; +package net.sourceforge.pmd.lang.java.rule.codestyle; import net.sourceforge.pmd.testframework.PmdRuleTst; -public class FinalInterfaceMethodParameterIsUnclearTest extends PmdRuleTst { +public class FinalParameterInAbstractMethod extends PmdRuleTst { // no additional unit tests } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/FinalParameterInAbstractMethod.xml similarity index 78% rename from pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml rename to pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/FinalParameterInAbstractMethod.xml index 1b220dd42f..a7b02ae104 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/FinalInterfaceMethodParameterIsUnclear.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/FinalParameterInAbstractMethod.xml @@ -8,7 +8,7 @@ Final is not allowed in interface public method 1 - Using final in public (non default) interface method is unclear + Final parameter in abstract method + + Final is not allowed in abstract method + 1 + + Final parameter in abstract method + + + + From 31d39c935e825bd7364cfed93c38a5a76e05497f Mon Sep 17 00:00:00 2001 From: Vincent Galloy Date: Thu, 6 Jan 2022 09:12:11 +0100 Subject: [PATCH 3/6] Fix FinalParameterInAbstractMethod.externalInfoUrl --- pmd-java/src/main/resources/category/java/codestyle.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index 7f35358098..069dab6da7 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -808,7 +808,7 @@ public class HelloWorldBean { message="Final parameter in abstract method" class="net.sourceforge.pmd.lang.rule.XPathRule" typeResolution="true" - externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#finalparameterinabstractmethod"> + externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#finalparameterinabstractmethod"> Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it. From 59422ffb55a60ca2a4b6b79325cd522115edfd1e Mon Sep 17 00:00:00 2001 From: Vincent Galloy Date: Thu, 6 Jan 2022 17:30:57 +0100 Subject: [PATCH 4/6] Fix test class name --- .../java/rule/codestyle/FinalParameterInAbstractMethod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java index b1751d5bca..1402092c39 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java @@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.codestyle; import net.sourceforge.pmd.testframework.PmdRuleTst; -public class FinalParameterInAbstractMethod extends PmdRuleTst { +public class FinalParameterInAbstractMethodTest extends PmdRuleTst { // no additional unit tests } From ba44f3116a07352d822f76747d5a176e9c8a41b0 Mon Sep 17 00:00:00 2001 From: Vincent Galloy Date: Thu, 6 Jan 2022 17:31:21 +0100 Subject: [PATCH 5/6] Fix file name --- ...bstractMethod.java => FinalParameterInAbstractMethodTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/{FinalParameterInAbstractMethod.java => FinalParameterInAbstractMethodTest.java} (100%) diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethodTest.java similarity index 100% rename from pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethod.java rename to pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/FinalParameterInAbstractMethodTest.java From 4b14c09b4d5776c6aed896e99a796a764afe8431 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 13 Jan 2022 18:17:36 +0100 Subject: [PATCH 6/6] [doc] Update release notes (#3720) --- docs/pages/release_notes.md | 13 +++++++++++++ .../src/main/resources/rulesets/releases/6420.xml | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 pmd-core/src/main/resources/rulesets/releases/6420.xml diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 365b5caa9a..3cca4da41a 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -14,6 +14,18 @@ This is a {{ site.pmd.release_type }} release. ### New and noteworthy +#### New rules + +* The new Java rule {% rule "java/codestyle/FinalParameterInAbstractMethod" %} detects parameters that are + declared as final in interfaces or abstract methods. Declaring the parameters as final is useless + because the implementation may choose to not respect it. + +```xml + +``` + + The rule is part of the quickstart.xml ruleset. + #### Modified rules * The Apex rule {% rule "apex/documentation/ApexDoc" %} has a new property `reportProperty`. @@ -37,6 +49,7 @@ This is a {{ site.pmd.release_type }} release. * [#3688](https://github.com/pmd/pmd/pull/3688): \[java] Bump log4j to 2.16.0 - [Sergey Nuyanzin](https://github.com/snuyanzin) * [#3693](https://github.com/pmd/pmd/pull/3693): \[apex] ApexDoc: Add reportProperty property - [Steve Babula](https://github.com/babula) * [#3713](https://github.com/pmd/pmd/pull/3713): \[java] Enhance UnnecessaryModifier to support records - [Vincent Galloy](https://github.com/vgalloy) +* [#3720](https://github.com/pmd/pmd/pull/3720): \[java] New rule: FinalParameterInAbstractMethod - [Vincent Galloy](https://github.com/vgalloy) {% endtocmaker %} diff --git a/pmd-core/src/main/resources/rulesets/releases/6420.xml b/pmd-core/src/main/resources/rulesets/releases/6420.xml new file mode 100644 index 0000000000..0770021fcc --- /dev/null +++ b/pmd-core/src/main/resources/rulesets/releases/6420.xml @@ -0,0 +1,13 @@ + + + + +This ruleset contains links to rules that are new in PMD v6.42.0 + + + + +