[java] New rule: StaticNestedRecord

Add the rule to check that nested record are not declared with static modifier
This commit is contained in:
Vincent Galloy
2022-01-03 17:41:44 +01:00
parent bdf3906682
commit b792c14f4d
3 changed files with 62 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTRecordDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTResource;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil;
@ -42,6 +43,7 @@ public class UnnecessaryModifierRule extends AbstractJavaRule {
addRuleChainVisit(ASTFieldDeclaration.class);
addRuleChainVisit(ASTAnnotationMethodDeclaration.class);
addRuleChainVisit(ASTConstructorDeclaration.class);
addRuleChainVisit(ASTRecordDeclaration.class);
}
@ -264,6 +266,14 @@ public class UnnecessaryModifierRule extends AbstractJavaRule {
return data;
}
@Override
public Object visit(ASTRecordDeclaration node, Object data) {
if (node.isStatic()) {
reportUnnecessaryModifiers(data, node, Modifier.STATIC, "record are implicitly static");
}
return data;
}
private boolean isSafeVarargs(final ASTMethodDeclaration node) {
return node.isAnnotationPresent(SafeVarargs.class.getName());

View File

@ -2062,6 +2062,12 @@ public class Bar {
FOO;
}
}
public class FooClass {
static record BarRecord() {} // static ignored
}
public interface FooInterface {
static record BarRecord() {} // static ignored
}
]]>
</example>
</rule>

View File

@ -695,6 +695,52 @@ public interface Bar {
void method() {
}
static class Foo {}
}
]]></code>
</test-code>
<test-code>
<description>Static is not necessary in class</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unnecessary modifier 'static' on record 'BarRecord': record are implicitly static</message>
</expected-messages>
<code><![CDATA[
public class FooClass {
static record BarRecord() {}
}
]]></code>
</test-code>
<test-code>
<description>Correct nested record declaration in classes</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class FooClass {
record BarRecord() {}
}
]]></code>
</test-code>
<test-code>
<description>Static is not necessary in interface</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unnecessary modifier 'static' on record 'BarRecord': record are implicitly static</message>
</expected-messages>
<code><![CDATA[
public interface FooInterface {
static record BarRecord() {}
}
]]></code>
</test-code>
<test-code>
<description>Correct nested record declaration in interface</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface FooInterface {
record BarRecord() {}
}
]]></code>
</test-code>