[java] Update rule MissingStaticMethodInNonInstantiatableClass
This commit is contained in:
@@ -235,7 +235,7 @@
|
||||
<!-- <rule ref="category/java/errorprone.xml/MethodWithSameNameAsEnclosingClass"/> -->
|
||||
<!-- <rule ref="category/java/errorprone.xml/MisplacedNullCheck"/> -->
|
||||
<rule ref="category/java/errorprone.xml/MissingSerialVersionUID"/>
|
||||
<!-- <rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"/> -->
|
||||
<rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"/>
|
||||
<!-- <rule ref="category/java/errorprone.xml/MoreThanOneLogger"/> -->
|
||||
<!-- <rule ref="category/java/errorprone.xml/NonCaseLabelInSwitchStatement"/> -->
|
||||
<rule ref="category/java/errorprone.xml/NonStaticInitializer"/>
|
||||
|
@@ -2329,35 +2329,31 @@ See the property `annotations`.
|
||||
<![CDATA[
|
||||
//ClassOrInterfaceDeclaration[@Nested= false()]
|
||||
[
|
||||
(
|
||||
(: at least one constructor :)
|
||||
./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration
|
||||
(: at least one constructor :) ./ClassOrInterfaceBody/ConstructorDeclaration
|
||||
and
|
||||
(: only private constructors :)count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration) = count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private= true()])
|
||||
(: only private constructors :) count(./ClassOrInterfaceBody/ConstructorDeclaration) = count(./ClassOrInterfaceBody/ConstructorDeclaration[pmd-java:modifiers() = "private"])
|
||||
and
|
||||
(: all constructors must not be annotated :)
|
||||
(every $x in $annotations satisfies
|
||||
not(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration/
|
||||
../Annotation/MarkerAnnotation/Name[pmd-java:typeIs($x)]))
|
||||
)
|
||||
and
|
||||
(: no static methods :)not(.//MethodDeclaration[@Static= true()])
|
||||
and
|
||||
(: no (public, package-private, protected) static fields :)not(.//FieldDeclaration[@Private= false()][@Static= true()])
|
||||
and
|
||||
(: no nested classes, that are public and static, and have no constructors at all or a public constructor :)
|
||||
(: and have a method returning the outer class type :)
|
||||
(: or the inner class extends the outer class :)not(.//ClassOrInterfaceDeclaration[@Nested= true()]
|
||||
[@Public= true()]
|
||||
[@Static= true()]
|
||||
[not(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration) or ./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Public= true()]]
|
||||
[(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration
|
||||
[@Public= true()]
|
||||
[./ResultType/Type/ReferenceType/ClassOrInterfaceType
|
||||
[@Image = //ClassOrInterfaceDeclaration[@Nested= false()]/@SimpleName]
|
||||
(every $x in $annotations satisfies
|
||||
not(./ClassOrInterfaceBody/ConstructorDeclaration/ModifierList/Annotation[pmd-java:typeIs($x)]))
|
||||
]
|
||||
(: no static methods :)
|
||||
[not(.//MethodDeclaration[pmd-java:modifiers() = "static"])]
|
||||
(: no (public, package-private, protected) static fields :)
|
||||
[not(.//FieldDeclaration[not(pmd-java:modifiers() = "private")][pmd-java:modifiers() = "static"])]
|
||||
(: no nested classes, that are public and static, and have no constructors at all or a public constructor :)
|
||||
(: and have a method returning the outer class type :)
|
||||
(: or the inner class extends the outer class :)
|
||||
[not(.//ClassOrInterfaceDeclaration[@Nested = true()]
|
||||
[pmd-java:modifiers() = ("public", "static")]
|
||||
[not(./ClassOrInterfaceBody/ConstructorDeclaration) or ./ClassOrInterfaceBody/ConstructorDeclaration[pmd-java:modifiers() = "public"]]
|
||||
[(./ClassOrInterfaceBody/MethodDeclaration
|
||||
[pmd-java:modifiers() = "public"]
|
||||
[./ClassOrInterfaceType
|
||||
[@SimpleName = //ClassOrInterfaceDeclaration[@Nested = false()]/@SimpleName]
|
||||
]
|
||||
) or (
|
||||
./ExtendsList/ClassOrInterfaceType[@Image = //ClassOrInterfaceDeclaration[@Nested=false()]/@SimpleName]
|
||||
./ExtendsList/ClassOrInterfaceType[@SimpleName = //ClassOrInterfaceDeclaration[@Nested = false()]/@SimpleName]
|
||||
)]
|
||||
)
|
||||
]
|
||||
|
@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.errorprone;
|
||||
|
||||
import net.sourceforge.pmd.testframework.PmdRuleTst;
|
||||
|
||||
@org.junit.Ignore("Rule has not been updated yet")
|
||||
public class MissingStaticMethodInNonInstantiatableClassTest extends PmdRuleTst {
|
||||
// no additional unit tests
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ public class Foo {
|
||||
<test-code>
|
||||
<description>simple failure</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>1</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
private Foo() {}
|
||||
@@ -37,6 +38,7 @@ public class Foo {
|
||||
<test-code>
|
||||
<description>failure with multiple constructors</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>1</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
private Foo(){}
|
||||
@@ -96,6 +98,7 @@ public class Foo {
|
||||
<test-code>
|
||||
<description>not ok, non-public static field</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>1</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
private static int BUZ = 2;
|
||||
@@ -150,7 +153,10 @@ public class Suit {
|
||||
<description>#1125 Missing Static Method In Non Instantiatable Class / Factory</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class AccountSelectionSubForm extends Form implements WidgetEventListener
|
||||
import java.awt.Frame;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class AccountSelectionSubForm extends Frame implements ActionListener
|
||||
{
|
||||
public static class Factory
|
||||
{
|
||||
@@ -159,7 +165,7 @@ public class AccountSelectionSubForm extends Form implements WidgetEventListener
|
||||
// do
|
||||
}
|
||||
// factory method which creates the outer class
|
||||
public AccountSelectionSubForm create( Form parent, boolean supportAllAccountsSelection )
|
||||
public AccountSelectionSubForm create( Frame parent, boolean supportAllAccountsSelection )
|
||||
{
|
||||
return new AccountSelectionSubForm();
|
||||
}
|
||||
@@ -288,6 +294,7 @@ public class Foo {
|
||||
<test-code>
|
||||
<description>#1832 but fail with both private constructors annotated with @PersistenceConstructor</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>3</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
|
||||
@@ -312,6 +319,8 @@ public class Foo {
|
||||
<description>#2102 [java] False positive MissingStaticMethodInNonInstantiatableClass when inheritors are instantiable</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.util.Function;
|
||||
|
||||
public abstract class MyADT {
|
||||
private MyADT() {
|
||||
}
|
||||
|
Reference in New Issue
Block a user