[java] New rule: FinalInterfaceMethodParameterIsUnclear

Add the rule to check that public method in interface do not use final qualifier which is unclear
This commit is contained in:
Vincent Galloy
2022-01-05 13:45:59 +01:00
parent 72c4bbb31d
commit 70eac686e4
4 changed files with 109 additions and 0 deletions

View File

@ -541,6 +541,41 @@ return a;
</example>
</rule>
<rule name="FinalInterfaceMethodParameterIsUnclear"
language="java"
since="6.42.0"
message="Using final in public (non default) interface method is unclear"
class="net.sourceforge.pmd.lang.rule.XPathRule"
typeResolution="true"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_bestpractices.html#finalinterfacemethodparameterisunclear">
<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[
//ClassOrInterfaceDeclaration[@Interface = true()]
/ClassOrInterfaceBody
/ClassOrInterfaceBodyDeclaration
/MethodDeclaration[@Public = true() and @Default = false() and @Static = false()]
/MethodDeclarator
/FormalParameters
/FormalParameter[@Final = true()]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public interface MyInterface {
void process(final Object arg); // Avoid using final here
}
]]>
</example>
</rule>
<rule name="ForLoopCanBeForeach"
language="java"

View File

@ -20,6 +20,7 @@
<rule ref="category/java/bestpractices.xml/ConstantsInInterface"/>
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt"/>
<rule ref="category/java/bestpractices.xml/DoubleBraceInitialization"/>
<rule ref="category/java/bestpractices.xml/FinalInterfaceMethodParameterIsUnclear"/>
<rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach"/>
<!-- <rule ref="category/java/bestpractices.xml/ForLoopVariableCount" /> -->
<rule ref="category/java/bestpractices.xml/GuardLogStatement"/>

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.bestpractices;
import net.sourceforge.pmd.testframework.PmdRuleTst;
public class FinalInterfaceMethodParameterIsUnclearTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -0,0 +1,62 @@
<?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>Using final in public (non default) interface method is unclear</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-data>