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
7 changed files with 146 additions and 1 deletions

View File

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

View File

@ -802,6 +802,39 @@ public class HelloWorldBean {
</example>
</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"
language="java"
since="1.02"

View File

@ -93,6 +93,7 @@
<!-- <rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract" /> -->
<rule ref="category/java/codestyle.xml/ExtendsObject"/>
<!-- <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/IdenticalCatchBranches"/>
<!-- <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>