IDEA-79297 warn if there would be overriders during extract method/change signature/etc
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Tue, 24 Jan 2012 11:58:55 +0000 (15:58 +0400)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Tue, 24 Jan 2012 16:35:31 +0000 (20:35 +0400)
java/java-impl/src/com/intellij/refactoring/util/ConflictsUtil.java

index 0ae871c7447fe7a29ef2b442bd294fd4214084ed..f21c11ad813a5b9a7b1fc6be9199d6d56526e357 100644 (file)
@@ -22,10 +22,12 @@ package com.intellij.refactoring.util;
 
 import com.intellij.psi.*;
 import com.intellij.psi.impl.source.resolve.FileContextUtil;
+import com.intellij.psi.search.searches.ClassInheritorsSearch;
 import com.intellij.psi.util.PsiFormatUtil;
 import com.intellij.psi.util.PsiUtil;
 import com.intellij.refactoring.RefactoringBundle;
 import com.intellij.usageView.UsageViewUtil;
+import com.intellij.util.Processor;
 import com.intellij.util.containers.MultiMap;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -54,9 +56,10 @@ public class ConflictsUtil {
 
   public static void checkMethodConflicts(@Nullable PsiClass aClass,
                                           PsiMethod refactoredMethod,
-                                          PsiMethod prototype,
+                                          final PsiMethod prototype,
                                           final MultiMap<PsiElement,String> conflicts) {
     if (prototype == null) return;
+    final String protoMethodInfo = getMethodPrototypeString(prototype);
 
     PsiMethod method = aClass != null ? aClass.findMethodBySignature(prototype, true) : null;
 
@@ -71,7 +74,6 @@ public class ConflictsUtil {
       }
       else { // method somewhere in base class
         if (JavaPsiFacade.getInstance(method.getProject()).getResolveHelper().isAccessible(method, aClass, null)) {
-          String protoMethodInfo = getMethodPrototypeString(prototype);
           String className = CommonRefactoringUtil.htmlEmphasize(UsageViewUtil.getDescriptiveName(method.getContainingClass()));
           if (PsiUtil.getAccessLevel(prototype.getModifierList()) >= PsiUtil.getAccessLevel(method.getModifierList()) ) {
             boolean isMethodAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
@@ -88,6 +90,18 @@ public class ConflictsUtil {
         }
       }
     }
+    if (aClass != null && prototype.hasModifierProperty(PsiModifier.PRIVATE)) {
+      ClassInheritorsSearch.search(aClass).forEach(new Processor<PsiClass>() {
+        @Override
+        public boolean process(PsiClass aClass) {
+          final PsiMethod[] methods = aClass.findMethodsBySignature(prototype, false);
+          for (PsiMethod method : methods) {
+            conflicts.putValue(method, "Method " + RefactoringUIUtil.getDescription(method, true) + " will override method of the base class " + RefactoringUIUtil.getDescription(aClass, false));
+          }
+          return true;
+        }
+      });
+    }
   }
 
   private static String getMethodPrototypeString(final PsiMethod prototype) {