forked from phoedos/pmd
Fix static methods being whitelisted
This commit is contained in:
@ -15,7 +15,10 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||||||
|
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
|
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;
|
import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
|
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTTypeBody;
|
import net.sourceforge.pmd.lang.java.ast.ASTTypeBody;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTTypeExpression;
|
import net.sourceforge.pmd.lang.java.ast.ASTTypeExpression;
|
||||||
@ -257,6 +260,12 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRulechainRule
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isPartOfStaticInitialization(ASTBodyDeclaration decl) {
|
||||||
|
return decl instanceof ASTFieldDeclaration && ((ASTFieldDeclaration) decl).isStatic()
|
||||||
|
|| decl instanceof ASTInitializer && ((ASTInitializer) decl).isStatic()
|
||||||
|
|| decl instanceof ASTEnumConstant;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return true if removing the qualification from this field access
|
* Return true if removing the qualification from this field access
|
||||||
* would produce an "Illegal forward reference" compiler error. This
|
* would produce an "Illegal forward reference" compiler error. This
|
||||||
@ -275,12 +284,12 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRulechainRule
|
|||||||
// The field must be declared in the same compilation unit
|
// The field must be declared in the same compilation unit
|
||||||
// to be a forward reference.
|
// to be a forward reference.
|
||||||
ASTVariableId fieldDecl = referencedSym.tryGetNode();
|
ASTVariableId fieldDecl = referencedSym.tryGetNode();
|
||||||
if (fieldDecl == null) {
|
if (fieldDecl == null || !fieldDecl.isStatic()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ASTBodyDeclaration enclosing = fieldAccess.ancestors(ASTBodyDeclaration.class)
|
ASTBodyDeclaration enclosing = fieldAccess.ancestors(ASTBodyDeclaration.class)
|
||||||
.first();
|
.first();
|
||||||
if (enclosing != null
|
if (isPartOfStaticInitialization(enclosing)
|
||||||
&& enclosing.getParent().getParent() == fieldDecl.getEnclosingType()) {
|
&& enclosing.getParent().getParent() == fieldDecl.getEnclosingType()) {
|
||||||
// the access is made in the same class
|
// the access is made in the same class
|
||||||
|
|
||||||
|
@ -1173,7 +1173,7 @@ public class Foo {
|
|||||||
</test-code>
|
</test-code>
|
||||||
<test-code>
|
<test-code>
|
||||||
<description>#5263 not forward references</description>
|
<description>#5263 not forward references</description>
|
||||||
<expected-problems>4</expected-problems>
|
<expected-problems>5</expected-problems>
|
||||||
<code><![CDATA[
|
<code><![CDATA[
|
||||||
|
|
||||||
public class EnumX {
|
public class EnumX {
|
||||||
@ -1193,6 +1193,10 @@ public class Foo {
|
|||||||
System.out.println(EnumX.X); // not FWR
|
System.out.println(EnumX.X); // not FWR
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void bar() {
|
||||||
|
System.out.println(EnumX.X); // not FWR
|
||||||
|
}
|
||||||
|
|
||||||
static final String X = "X";
|
static final String X = "X";
|
||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
|
Reference in New Issue
Block a user