From cd465918b5fbb77a350c0476ae875ca7c431c7db Mon Sep 17 00:00:00 2001 From: Maikel Steneker Date: Wed, 17 Apr 2019 15:05:20 +0200 Subject: [PATCH] Changed location for AssignmentToNonFinalStatic violations from field declaration at to assignment in constructor. This makes it easier to identify the code that needs to be adjusted in order to resolve the violation. --- .../errorprone/AssignmentToNonFinalStaticRule.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java index 4870e31778..0fdb1a95e1 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AssignmentToNonFinalStaticRule.java @@ -33,16 +33,15 @@ public class AssignmentToNonFinalStaticRule extends AbstractJavaRule { continue; } - if (initializedInConstructor(entry.getValue())) { - addViolation(data, decl.getNode(), decl.getImage()); + final Node location = initializedInConstructor(entry.getValue()); + if (location != null) { + addViolation(data, location, decl.getImage()); } } return super.visit(node, data); } - private boolean initializedInConstructor(List usages) { - boolean initInConstructor = false; - + private Node initializedInConstructor(List usages) { for (NameOccurrence occ : usages) { // specifically omitting prefix and postfix operators as there are // legitimate usages of these with static fields, e.g. typesafe enum pattern. @@ -50,12 +49,12 @@ public class AssignmentToNonFinalStaticRule extends AbstractJavaRule { Node node = occ.getLocation(); Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class); if (constructor != null) { - initInConstructor = true; + return node; } } } - return initInConstructor; + return null; } }