safe delete: do not show conflicts about method reference if it occurs in the argumen...
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Tue, 15 Nov 2016 14:13:25 +0000 (15:13 +0100)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Tue, 15 Nov 2016 17:11:31 +0000 (18:11 +0100)
java/java-impl/src/com/intellij/refactoring/safeDelete/JavaSafeDeleteProcessor.java
java/java-tests/testData/refactoring/safeDelete/NoConflictOnDeleteParameterWithMethodRefArg.java [new file with mode: 0644]
java/java-tests/testData/refactoring/safeDelete/NoConflictOnDeleteParameterWithMethodRefArg_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/SafeDeleteTest.java

index 858d6116dd58667e80d3430004381feb220feef3..b5c72021d8871dea5f2d3dcb9fc9f25e626f0253 100644 (file)
@@ -49,9 +49,7 @@ import com.intellij.usageView.UsageInfo;
 import com.intellij.usageView.UsageViewUtil;
 import com.intellij.usages.*;
 import com.intellij.util.ArrayUtil;
-import com.intellij.util.Function;
 import com.intellij.util.IncorrectOperationException;
-import com.intellij.util.Processor;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.containers.HashMap;
 import com.intellij.util.containers.MultiMap;
@@ -255,12 +253,22 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
   @Override
   public Collection<String> findConflicts(PsiElement element, PsiElement[] elements, UsageInfo[] usages) {
     String methodRefFound = null;
-    if (!ApplicationManager.getApplication().isUnitTestMode() && (element instanceof PsiMethod || element instanceof PsiParameter)) {
-      for (UsageInfo usage : usages) {
-        final PsiElement refElement = usage.getElement();
-        if (refElement instanceof PsiMethodReferenceExpression) {
-          methodRefFound = RefactoringBundle.message("expand.method.reference.warning");
-          break;
+    if (element instanceof PsiMethod || element instanceof PsiParameter) {
+      PsiMethod method;
+      if (element instanceof PsiMethod) {
+        method = (PsiMethod)element;
+      }
+      else {
+        PsiElement declarationScope = ((PsiParameter)element).getDeclarationScope();
+        method = declarationScope instanceof PsiMethod ? (PsiMethod)declarationScope : null;
+      }
+      if (method != null) {
+        for (UsageInfo usage : usages) {
+          final PsiElement refElement = usage.getElement();
+          if (refElement instanceof PsiMethodReferenceExpression && method.equals(((PsiMethodReferenceExpression)refElement).resolve())) {
+            methodRefFound = RefactoringBundle.message("expand.method.reference.warning");
+            break;
+          }
         }
       }
     }
diff --git a/java/java-tests/testData/refactoring/safeDelete/NoConflictOnDeleteParameterWithMethodRefArg.java b/java/java-tests/testData/refactoring/safeDelete/NoConflictOnDeleteParameterWithMethodRefArg.java
new file mode 100644 (file)
index 0000000..926b0bb
--- /dev/null
@@ -0,0 +1,13 @@
+import java.util.function.Consumer;
+
+public class Subst {
+  void test1() {
+    test2(Subst::bar);
+  }
+
+  private static void bar(Object o) {
+
+  }
+
+  void test2(final Consumer<Object> consu<caret>mer) {}
+}
diff --git a/java/java-tests/testData/refactoring/safeDelete/NoConflictOnDeleteParameterWithMethodRefArg_after.java b/java/java-tests/testData/refactoring/safeDelete/NoConflictOnDeleteParameterWithMethodRefArg_after.java
new file mode 100644 (file)
index 0000000..87b2e54
--- /dev/null
@@ -0,0 +1,13 @@
+import java.util.function.Consumer;
+
+public class Subst {
+  void test1() {
+    test2();
+  }
+
+  private static void bar(Object o) {
+
+  }
+
+  void test2() {}
+}
index db481b14dc3d4223c59a9a3d6800281c771cd4f6..9e5251c74b9b255a0b5dd3fd8c98a324c169c37e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -312,6 +312,17 @@ public class SafeDeleteTest extends MultiFileTestCase {
   }
 
   public void testParameterInMethodUsedInMethodReference() throws Exception {
+    LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8);
+    try {
+      BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(true);
+      doSingleFileTest();
+    }
+    finally {
+      BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(false);
+    }
+  }
+
+  public void testNoConflictOnDeleteParameterWithMethodRefArg() throws Exception {
     LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_8);
     doSingleFileTest();
   }