IDEA-52997: Groovy: good code is red. A field and a property of the same name are...
[idea/community.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / lang / resolve / CollectClassMembersUtil.java
index 2b55e7647e721de8648923fd762ecaf19cefcc96..df3823da00ff4101793cf60c19a935029c14fbd6 100644 (file)
@@ -23,6 +23,8 @@ import com.intellij.psi.util.*;
 import com.intellij.util.containers.HashMap;
 import com.intellij.util.containers.HashSet;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
 
 import java.util.ArrayList;
@@ -89,6 +91,14 @@ public class CollectClassMembersUtil {
       String name = field.getName();
       if (!allFields.containsKey(name)) {
         allFields.put(name, new CandidateInfo(field, substitutor));
+      } else if (hasExplicitVisibilityModifiers(field)) {
+        final CandidateInfo candidateInfo = allFields.get(name);
+        final PsiElement element = candidateInfo.getElement();
+        if (element instanceof GrField &&
+            !(((GrField)element).getModifierList()).hasExplicitVisibilityModifiers() &&
+            aClass == ((GrField)element).getContainingClass()) { //replace property-field with field with explicit visibilityModifier 
+          allFields.put(name, new CandidateInfo(field, substitutor));
+        }
       }
     }
 
@@ -112,6 +122,15 @@ public class CollectClassMembersUtil {
     }
   }
 
+  private static boolean hasExplicitVisibilityModifiers(PsiField field) {
+    if (field instanceof GrField) {
+      return ((GrModifierList)field.getModifierList()).hasExplicitVisibilityModifiers();
+    }
+    else {
+      return true;
+    }
+  }
+
   private static void addMethod(Map<String, List<CandidateInfo>> allMethods, PsiMethod method, PsiSubstitutor substitutor) {
     String name = method.getName();
     List<CandidateInfo> methods = allMethods.get(name);