From 918684c154ab2ced66f451a086afc1dfeddc5e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 21 Nov 2024 16:51:05 +0100 Subject: [PATCH] Fix static methods being whitelisted --- .../UnnecessaryFullyQualifiedNameRule.java | 13 +++++++++++-- .../codestyle/xml/UnnecessaryFullyQualifiedName.xml | 6 +++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java index 172e5cfc9a..20e072efed 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java @@ -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.ASTClassType; +import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant; 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.ASTTypeBody; 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 * 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 // to be a forward reference. ASTVariableId fieldDecl = referencedSym.tryGetNode(); - if (fieldDecl == null) { + if (fieldDecl == null || !fieldDecl.isStatic()) { return false; } ASTBodyDeclaration enclosing = fieldAccess.ancestors(ASTBodyDeclaration.class) .first(); - if (enclosing != null + if (isPartOfStaticInitialization(enclosing) && enclosing.getParent().getParent() == fieldDecl.getEnclosingType()) { // the access is made in the same class diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml index 083d644566..eae7f46f93 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml @@ -1173,7 +1173,7 @@ public class Foo { #5263 not forward references - 4 + 5