safe delete: ensure not to delete call qualifier (IDEA-135727) webstorm/15
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 28 Jan 2015 16:05:26 +0000 (19:05 +0300)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 28 Jan 2015 16:06:42 +0000 (19:06 +0300)
java/java-impl/src/com/intellij/refactoring/safeDelete/SafeDeleteJavaCallerChooser.java
java/java-tests/testData/refactoring/safeDelete/NoDeepDeleteParameterUsedInCallQualifier.java [new file with mode: 0644]
java/java-tests/testData/refactoring/safeDelete/NoDeepDeleteParameterUsedInCallQualifier_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/SafeDeleteTest.java

index ef96f251d7c22458dc397967c46aca67778aaefc..ef6e3351b656ccca59aee715a4005a9b6d295819 100644 (file)
@@ -135,13 +135,15 @@ abstract class SafeDeleteJavaCallerChooser extends JavaCallerChooser {
                   public boolean process(PsiReference reference) {
                     final PsiElement element = reference.getElement();
                     if (element instanceof PsiReferenceExpression) {
-                      PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class);
+                      PsiCallExpression parent = PsiTreeUtil.getParentOfType(element, PsiCallExpression.class);
                       while (parent != null) {
-                        final PsiMethod resolved = ((PsiCallExpression)parent).resolveMethod();
+                        final PsiMethod resolved = parent.resolveMethod();
                         if (scope.equals(resolved)) {
+                          if (usedInQualifier(element, parent)) return false;
                           return true;
                         }
                         if (nodeMethod.equals(resolved)) {
+                          if (usedInQualifier(element, parent)) return false;
                           ref.set(true);
                           return true;
                         }
@@ -151,8 +153,19 @@ abstract class SafeDeleteJavaCallerChooser extends JavaCallerChooser {
                     }
                     return true;
                   }
+
+                  private boolean usedInQualifier(PsiElement element, PsiCallExpression parent) {
+                    PsiExpression qualifier = null;
+                    if (parent instanceof PsiMethodCallExpression) {
+                      qualifier = ((PsiMethodCallExpression)parent).getMethodExpression();
+                    }
+                    else if (parent instanceof PsiNewExpression) {
+                      qualifier = ((PsiNewExpression)parent).getQualifier();
+                    }
+                    return PsiTreeUtil.isAncestor(qualifier, element, true);
+                  }
                 }) && ref.get()) {
-                  return (PsiParameter)parameter;
+                  return parameter;
                 }
               }
             }
diff --git a/java/java-tests/testData/refactoring/safeDelete/NoDeepDeleteParameterUsedInCallQualifier.java b/java/java-tests/testData/refactoring/safeDelete/NoDeepDeleteParameterUsedInCallQualifier.java
new file mode 100644 (file)
index 0000000..e4b8a42
--- /dev/null
@@ -0,0 +1,9 @@
+class Clazz {
+  void callee(Clazz cl<caret>azz) {}
+}
+
+class User {
+  void caller(Clazz clazz) {
+    clazz.callee(clazz);
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/safeDelete/NoDeepDeleteParameterUsedInCallQualifier_after.java b/java/java-tests/testData/refactoring/safeDelete/NoDeepDeleteParameterUsedInCallQualifier_after.java
new file mode 100644 (file)
index 0000000..59d1cf4
--- /dev/null
@@ -0,0 +1,9 @@
+class Clazz {
+  void callee() {}
+}
+
+class User {
+  void caller(Clazz clazz) {
+    clazz.callee();
+  }
+}
\ No newline at end of file
index 477d15097cd0d67b0dc82e4cecfc9aa57d33ec62..6d24883fad47cb8b28d63d444669e4522396d4a5 100644 (file)
@@ -88,6 +88,10 @@ public class SafeDeleteTest extends MultiFileTestCase {
     doSingleFileTest();
   }
 
+  public void testNoDeepDeleteParameterUsedInCallQualifier() throws Exception {
+    doSingleFileTest();
+  }
+
   public void testToDeepDeleteParameterOverriders() throws Exception {
     doSingleFileTest();
   }