Merge pull request #3720 from

vgalloy:FinalInterfaceMethodParameterIsUnclear

[java] New rule: FinalParameterInAbstractMethod #3720

* pr-3720:
  [doc] Update release notes (#3720)
  Fix file name
  Fix test class name
  Fix FinalParameterInAbstractMethod.externalInfoUrl
  Rename to FinalParameterInAbstractMethod and target all methods
  [java] New rule: FinalInterfaceMethodParameterIsUnclear
This commit is contained in:
Andreas Dangel 2022-01-13 18:18:41 +01:00
commit e4771c7687
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
7 changed files with 146 additions and 1 deletions

View File

@ -14,6 +14,18 @@ This is a {{ site.pmd.release_type }} release.
### New and noteworthy ### 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
<rule ref="category/java/codestyle.xml/FinalParameterInAbstractMethod" />
```
The rule is part of the quickstart.xml ruleset.
#### Modified rules #### Modified rules
* The Apex rule {% rule "apex/documentation/ApexDoc" %} has a new property `reportProperty`. * The Apex rule {% rule "apex/documentation/ApexDoc" %} has a new property `reportProperty`.
@ -43,6 +55,7 @@ This is a {{ site.pmd.release_type }} release.
* [#3704](https://github.com/pmd/pmd/pull/3704): \[java] Fix for #3686 - Fix ReturnEmptyCollectionRatherThanNull - [Oleksii Dykov](https://github.com/dykov) * [#3704](https://github.com/pmd/pmd/pull/3704): \[java] Fix for #3686 - Fix ReturnEmptyCollectionRatherThanNull - [Oleksii Dykov](https://github.com/dykov)
* [#3713](https://github.com/pmd/pmd/pull/3713): \[java] Enhance UnnecessaryModifier to support records - [Vincent Galloy](https://github.com/vgalloy) * [#3713](https://github.com/pmd/pmd/pull/3713): \[java] Enhance UnnecessaryModifier to support records - [Vincent Galloy](https://github.com/vgalloy)
* [#3719](https://github.com/pmd/pmd/pull/3719): \[java] Upgrade log4j to 2.17.1 - [Daniel Paul Searles](https://github.com/squaresurf) * [#3719](https://github.com/pmd/pmd/pull/3719): \[java] Upgrade log4j to 2.17.1 - [Daniel Paul Searles](https://github.com/squaresurf)
* [#3720](https://github.com/pmd/pmd/pull/3720): \[java] New rule: FinalParameterInAbstractMethod - [Vincent Galloy](https://github.com/vgalloy)
* [#3724](https://github.com/pmd/pmd/pull/3724): \[java] Fix for #3686 - fix FinalFieldCouldBeStatic - [Oleksii Dykov](https://github.com/dykov) * [#3724](https://github.com/pmd/pmd/pull/3724): \[java] Fix for #3686 - fix FinalFieldCouldBeStatic - [Oleksii Dykov](https://github.com/dykov)
{% endtocmaker %} {% endtocmaker %}

View File

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<ruleset name="6420"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
This ruleset contains links to rules that are new in PMD v6.42.0
</description>
<rule ref="category/java/codestyle.xml/FinalParameterInAbstractMethod" />
</ruleset>

View File

@ -541,7 +541,6 @@ return a;
</example> </example>
</rule> </rule>
<rule name="ForLoopCanBeForeach" <rule name="ForLoopCanBeForeach"
language="java" language="java"
since="6.0.0" since="6.0.0"

View File

@ -802,6 +802,39 @@ public class HelloWorldBean {
</example> </example>
</rule> </rule>
<rule name="FinalParameterInAbstractMethod"
language="java"
since="6.42.0"
message="Final parameter in abstract method"
class="net.sourceforge.pmd.lang.rule.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#finalparameterinabstractmethod">
<description>
Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it.
</description>
<priority>1</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[
MethodDeclarator/FormalParameters/FormalParameter[@Final = true()] and
not(Block)
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public interface MyInterface {
void process(final Object arg); // Avoid using final here
}
]]>
</example>
</rule>
<rule name="ForLoopShouldBeWhileLoop" <rule name="ForLoopShouldBeWhileLoop"
language="java" language="java"
since="1.02" since="1.02"

View File

@ -93,6 +93,7 @@
<!-- <rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract" /> --> <!-- <rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract" /> -->
<rule ref="category/java/codestyle.xml/ExtendsObject"/> <rule ref="category/java/codestyle.xml/ExtendsObject"/>
<!-- <rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass" /> --> <!-- <rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass" /> -->
<rule ref="category/java/codestyle.xml/FinalParameterInAbstractMethod"/>
<rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop"/> <rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop"/>
<rule ref="category/java/codestyle.xml/IdenticalCatchBranches"/> <rule ref="category/java/codestyle.xml/IdenticalCatchBranches"/>
<!-- <rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal" /> --> <!-- <rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal" /> -->

View File

@ -0,0 +1,11 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import net.sourceforge.pmd.testframework.PmdRuleTst;
public class FinalParameterInAbstractMethodTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Final is not allowed in interface public method</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Final parameter in abstract method</message>
</expected-messages>
<code><![CDATA[
public interface Foo {
void bar(final int a);
}
]]></code>
</test-code>
<test-code>
<description>Final is allowed in interface static method</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface Foo {
static void bar(final int a) {}
}
]]></code>
</test-code>
<test-code>
<description>Final is allowed in interface private method</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface Foo {
private void bar(final int a) {}
}
]]></code>
</test-code>
<test-code>
<description>Final is allowed in interface default method</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface Foo {
default void bar(final int a) {}
}
]]></code>
</test-code>
<test-code>
<description>Final is allowed in interface inner class method</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface Foo {
class Inner {
public void bar(final int a) {}
}
}
]]></code>
</test-code>
<test-code>
<description>Final is not allowed in abstract method</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Final parameter in abstract method</message>
</expected-messages>
<code><![CDATA[
public abstract class Foo {
abstract void bar(final int a);
}
]]></code>
</test-code>
</test-data>