Skip to content

Commit

Permalink
Merge branch 'master' into #699
Browse files Browse the repository at this point in the history
  • Loading branch information
dskalenko committed Feb 28, 2016
2 parents 864e46b + 4243a5b commit e22113c
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
* @since 1.0
*/
public final class Sample {
/**
* Utility constructor.
*/
private Sample() {
// do nothing
}

/**
* Test method.
* @return Stream.
* @checkstyle NonStaticMethod (2 lines)
*/
public static InputStream test() {
public InputStream test() {
return IOUtils.toInputStream("oops");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
* @since 1.0
*/
public final class Sample {
/**
* Utility constructor.
*/
private Sample() {
// do nothing
}

/**
* Test method.
* @return Stream.
* @checkstyle NonStaticMethod (2 lines)
*/
public static InputStream test() {
public InputStream test() {
return IOUtils.toInputStream("oops");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@
* @since 1.0
*/
public final class Sample {
/**
* Utility constructor.
*/
private Sample() {
// do nothing
}

/**
* Test method.
* @return Stream.
* @checkstyle NonStaticMethod (2 lines)
*/
public static String test() {
public String test() {
return "oops";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @version $Id$
* @since 1.0
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public final class Main {
/**
* Utility constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,42 @@ public final class Violations {
*/
private Integer var;

/**
* Utility constructor.
*/
private Violations() {
// do nothing
}

/**
* Calculate square of a number.
* @param num The number
* @return The square
* @checkstyle NonStaticMethod (2 lines)
*/
public static int square(final int num) {
public int square(final int num) {
return num * num;
}

/**
* Returns Foo.
* @return Foo.
* @checkstyle NonStaticMethod (2 lines)
*/
public static Foo doSmth() {
public Foo doSmth() {
final String name = "test".toUpperCase();
return new Foo(name);
}

/**
* Returns Foo again.
* @return Foo.
* @checkstyle NonStaticMethod (2 lines)
*/
public static Foo doSmthElse() {
public Foo doSmthElse() {
String name = "other";
name = String.format("%s append", name);
return new Foo(name);
}

/**
* Prints something.
* @checkstyle NonStaticMethod (2 lines)
*/
public static void print() {
public void print() {
final String message = "hello";
System.out.println(message);
}
Expand Down
34 changes: 34 additions & 0 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,38 @@
</properties>
<priority>3</priority>
</rule>

<rule name="ProhibitPublicStaticMethods"
message="Public static methods are prohibited."
language="java"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
Public static methods are prohibited.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceBodyDeclaration[
MethodDeclaration[@Static='true' and @Public='true'
and not (
MethodDeclarator[
count(FormalParameters/FormalParameter)=1
and @Image='main'
and FormalParameters/FormalParameter[1]/Type/ReferenceType/ClassOrInterfaceType[@Image='String']
and FormalParameters/FormalParameter[@Varargs='true']
] and not(ResultType/Type)
)
] and (
Annotation/MarkerAnnotation/Name[@Image!='BeforeClass' and @Image!='AfterClass'
and @Image!='Parameterized.Parameters']
or not (Annotation)
)
]
]]>
</value>
</property>
</properties>
<priority>3</priority>
</rule>
</ruleset>
51 changes: 51 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 @@ -90,6 +90,12 @@ public final class PMDValidatorTest {
private static final String PLAIN_ASSERTIONS =
"Avoid using Plain JUnit assertions";

/**
* Error message used to inform about using public static method.
*/
private static final String STATIC_METHODS =
"Public static methods are prohibited";

/**
* PMDValidator can find violations in Java file(s).
* @throws Exception If something wrong happens inside.
Expand Down Expand Up @@ -508,4 +514,49 @@ public void allowsNonTransientFields() throws Exception {
).validate();
}

/**
* PMDValidator can prohibit public static methods.
* @throws Exception If something wrong happens inside.
*/
@Test
public void prohibitsPublicStaticMethods() throws Exception {
new PMDAssert(
"StaticPublicMethod.java",
Matchers.is(false),
Matchers.containsString(PMDValidatorTest.STATIC_METHODS)
).validate();
}

/**
* PMDValidator can allow public static void main(String...args) method.
* @throws Exception If something wrong happens inside.
*/
@Test
public void allowsPublicStaticMainMethod() throws Exception {
new PMDAssert(
"StaticPublicVoidMainMethod.java",
Matchers.is(true),
Matchers.not(
Matchers.containsString(PMDValidatorTest.STATIC_METHODS)
)
).validate();
}

/**
* PMDValidator can allow JUnit public static methods marked with:<br>
* BeforeClass annotation.<br>
* AfterClass annotation.<br>
* Parameterized.Parameters annotation.
* @throws Exception If something wrong happens inside.
*/
@Test
public void allowsJunitFrameworkPublicStaticMethods() throws Exception {
new PMDAssert(
"JunitStaticPublicMethods.java",
Matchers.is(true),
Matchers.not(
Matchers.containsString(PMDValidatorTest.STATIC_METHODS)
)
).validate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package foo.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collection;
import java.util.Collections;

public class SomeTest {

@BeforeClass
public static void beforeClass(){
// setup before class
}

@Test
public void emptyTest(){
//test something
}

@AfterClass
public static void afterClass() throws Exception {
//tear down after class
}

@Parameterized.Parameters
public static Collection parameters() {
return Collections.EMPTY_LIST;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
public final class StaticAccessToStaticFields {
private static int num = 1;

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

public int another() {
return 0;
}

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

public final class StaticPublicMethod {

private FieldInitConstructor() {
super();
}

public static StaticPublicMethod create() {
return new StaticPublicMethod();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package foo;

public class StaticPublicVoidMainMethod {

public static final InnerClass INNER = new InnerClass(10);

public static void main(final String... args) {
// allow main method
}

public void doNothing() {
//do nothing here
}

public static class InnerClass {
private final int number;

public InnerClass(final int num) {
this.number = num;
}

public int calculate() {
return number;
}
}
}

0 comments on commit e22113c

Please sign in to comment.