[java] Detect more unnecessary finals
- Mark unnecessary finals in anonymous class methods. - Mark unnecessary finals in enum instance methods. - Fixes #412
This commit is contained in:
@ -62,7 +62,8 @@ public class Foo {
|
||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/unnecessary.html#UnnecessaryFinalModifier">
|
||||
<description>
|
||||
When a class has the final modifier, all the methods are automatically final and do not need to be
|
||||
tagged as such. Similarly, private methods can't be overriden, and therefore do not need to be tagged either.
|
||||
tagged as such. Similarly, methods that can't be overridden (private methods, methods of anonymous classes,
|
||||
methods of enum instance) do not need to be tagged either.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
@ -73,7 +74,9 @@ tagged as such. Similarly, private methods can't be overriden, and therefore do
|
||||
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
|
||||
[count(./Annotation/MarkerAnnotation/Name[@Image='SafeVarargs' or @Image='java.lang.SafeVarargs']) = 0]
|
||||
/MethodDeclaration[@Final='true']
|
||||
| //MethodDeclaration[@Final='true' and @Private='true']
|
||||
| //MethodDeclaration[@Final='true' and @Private='true']
|
||||
| //EnumConstant/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[@Final='true']
|
||||
| //AllocationExpression/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[@Final='true']
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
|
@ -124,6 +124,34 @@ public class TestClass {
|
||||
private final int getValue() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>Unnecessary final of enum method</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public enum Foo {
|
||||
BAR {
|
||||
@Override
|
||||
public final void magic() {}
|
||||
};
|
||||
|
||||
public void magic() {}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>Unnecessary final of anonymous class method</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void stuff() {
|
||||
Listener list = new Listener() {
|
||||
@Override
|
||||
public final void onEvent() {}
|
||||
};
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user