Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 18, 2016
2 parents 76525db + 915a024 commit 1c7e5c8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
19 changes: 18 additions & 1 deletion qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,22 @@
<priority>3</priority>
</rule>


<rule name="StaticAccessToStaticFields"
message="Static fields should be accessed in a static way [CLASS_NAME.FIELD_NAME]."
language="java"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
Avoid accessing static fields directly.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Name[@Image = //FieldDeclaration[@Static='true']/@VariableName]
]]>
</value>
</property>
</properties>
<priority>3</priority>
</rule>
</ruleset>
43 changes: 43 additions & 0 deletions qulice-pmd/src/test/java/com/qulice/pmd/PMDValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
@SuppressWarnings("PMD.TooManyMethods")
public final class PMDValidatorTest {

/**
* Error message for forbidding access to static fields
* other than with a static way.
* @checkstyle LineLength (2 lines)
*/
private static final String STATIC_ACCESS = "%s\\[\\d+-\\d+\\]: Static fields should be accessed in a static way \\[CLASS_NAME.FIELD_NAME\\]\\.";

/**
* Error message for forbidding instructions inside a constructor
* other than field initialization or call to other contructors.
Expand Down Expand Up @@ -315,4 +322,40 @@ public void acceptsCallToConstructorInConstructor()
)
).validate();
}

/**
* PMDValidator accepts calls to static fields
* in a static way.
* @throws Exception If something wrong happens inside.
*/
@Test
public void acceptsCallToStaticFieldsInStaticWay()
throws Exception {
final String file = "StaticAccessToStaticFields.java";
new PMDAssert(
file, Matchers.is(true),
Matchers.not(
RegexMatchers.containsPattern(
String.format(PMDValidatorTest.STATIC_ACCESS, file)
)
)
).validate();
}

/**
* PMDValidator forbids calls to static fields
* in a non static way.
* @throws Exception If something wrong happens inside.
*/
@Test
public void forbidsCallToStaticFieldsInNonStaticWay()
throws Exception {
final String file = "NonStaticAccessToStaticFields.java";
new PMDAssert(
file, Matchers.is(false),
RegexMatchers.containsPattern(
String.format(PMDValidatorTest.STATIC_ACCESS, file)
)
).validate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foo;

public final class NonStaticAccessToStaticFields {
private static int num = 1;

public static int number() {
return num;
}

public int another() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foo;

public final class StaticAccessToStaticFields {
private static int num = 1;

public static int number() {
return StaticAccessToStaticFields.num;
}

public int another() {
return 0;
}
}

0 comments on commit 1c7e5c8

Please sign in to comment.