Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] ConstantsInInterface: Add ConstantsInInterface rule. Effective Java, 19 #93

Merged
merged 2 commits into from May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions pmd-java/src/main/resources/rulesets/java/design.xml
Expand Up @@ -1949,4 +1949,50 @@ public final class Foo {
</example>
</rule>

<rule name="ConstantsInInterface"
language="java"
since="5.5"
message="Avoid constants in interfaces. Interfaces define types, constants are implementation details better placed in classes or enums. See Effective Java, item 19."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#ConstantsInInterface">
<description>
Avoid constants in interfaces. Interfaces should define types, constants are implementation details
better placed in classes or enums. See Effective Java, item 19.
</description>
<priority>3</priority>
<properties>
<property name="ignoreIfHasMethods" type="Boolean" description="Whether to ignore constants in interfaces if the interface defines any methods" value="true"/>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Interface='true'][$ignoreIfHasMethods='false' or not(.//MethodDeclaration)]//FieldDeclaration
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public interface ConstantInterface {
public static final int CONST1 = 1; // violation, no fields allowed in interface!
static final int CONST2 = 1; // violation, no fields allowed in interface!
final int CONST3 = 1; // violation, no fields allowed in interface!
int CONST4 = 1; // violation, no fields allowed in interface!
}

// with ignoreIfHasMethods = false
public interface AnotherConstantInterface {
public static final int CONST1 = 1; // violation, no fields allowed in interface!

int anyMethod();
}

// with ignoreIfHasMethods = true
public interface YetAnotherConstantInterface {
public static final int CONST1 = 1; // no violation

int anyMethod();
}
]]>
</example>
</rule>
</ruleset>
Expand Up @@ -73,5 +73,6 @@ public void setUp() {
addRule(RULESET, "UseNotifyAllInsteadOfNotify");
addRule(RULESET, "UseUtilityClass");
addRule(RULESET, "UseVarargs");
addRule(RULESET, "ConstantsInInterface");
}
}
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
constants in interface, no methods
]]></description>
<expected-problems>4</expected-problems>
<code><![CDATA[
public interface Foo {
public static final int CONST1 = 1;
static final int CONST2 = 1;
final int CONST3 = 1;
int CONST4 = 1;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
constants in interface, with methods, ignoreIfHasMethods is false
]]></description>
<rule-property name="ignoreIfHasMethods">false</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public interface AnotherConstantInterface {
public static final int CONST1 = 1;

int anyMethod();
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
constants in interface, with methods, ignoreIfHasMethods is true
]]></description>
<rule-property name="ignoreIfHasMethods">true</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
public interface AnotherConstantInterface {
public static final int CONST1 = 1;

int anyMethod();
}
]]></code>
</test-code>
</test-data>