IDEA-163082 "can be replaced with addAll" inspection suggests a recursive call; suppo...
authorTagir Valeev <Tagir.Valeev@jetbrains.com>
Tue, 25 Oct 2016 04:38:24 +0000 (11:38 +0700)
committerTagir Valeev <Tagir.Valeev@jetbrains.com>
Tue, 25 Oct 2016 04:38:24 +0000 (11:38 +0700)
java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAddAllInsideOther.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeAddAllInsideAddAll.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeAddAllInsideOther.java [new file with mode: 0644]

index c28b084908aeefbebf611ce03fc8c6202a6b091e..18508b26bb76ef2c6417742765b8b87dd3c8878c 100644 (file)
@@ -259,7 +259,13 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
     final PsiVariable variable = tb.getVariable();
     final PsiMethodCallExpression methodCallExpression = tb.getSingleMethodCall();
     LOG.assertTrue(methodCallExpression != null);
-    return isIdentityMapping(variable, methodCallExpression.getArgumentList().getExpressions()[0]);
+    if (!isIdentityMapping(variable, methodCallExpression.getArgumentList().getExpressions()[0])) return false;
+    PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression();
+    if(qualifierExpression == null || qualifierExpression instanceof PsiThisExpression) {
+      PsiMethod method = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethod.class);
+      return method == null || !method.getName().equals("addAll");
+    }
+    return true;
   }
 
   private static boolean isCollectCall(TerminalBlock tb) {
@@ -280,7 +286,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
         }
         qualifierClass = PsiUtil.resolveClassInType(qualifierExpression.getType());
       }
-      else if (qualifierExpression == null) {
+      else if (qualifierExpression == null || qualifierExpression instanceof PsiThisExpression) {
         final PsiClass enclosingClass = PsiTreeUtil.getParentOfType(methodCallExpression, PsiClass.class);
         if (PsiUtil.getEnclosingStaticElement(methodCallExpression, enclosingClass) == null) {
           qualifierClass = enclosingClass;
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAddAllInsideOther.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAddAllInsideOther.java
new file mode 100644 (file)
index 0000000..44af5a5
--- /dev/null
@@ -0,0 +1,22 @@
+// "Replace with addAll" "true"
+
+import java.util.*;
+
+public class Main {
+  static class MyList extends AbstractCollection<String> {
+    @Override
+    public Iterator<String> iterator() {
+      return Collections.emptyIterator();
+    }
+
+    @Override
+    public int size() {
+      return 0;
+    }
+
+    public boolean myAdd(Collection<? extends String> c) {
+        this.addAll(c);
+      return true;
+    }
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeAddAllInsideAddAll.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeAddAllInsideAddAll.java
new file mode 100644 (file)
index 0000000..c38c39e
--- /dev/null
@@ -0,0 +1,25 @@
+// "Replace with addAll" "false"
+
+import java.util.*;
+
+public class Main {
+  static class MyList extends AbstractCollection<String> {
+    @Override
+    public Iterator<String> iterator() {
+      return Collections.emptyIterator();
+    }
+
+    @Override
+    public int size() {
+      return 0;
+    }
+
+    @Override
+    public boolean addAll(Collection<? extends String> c) {
+      for (String e : <caret>c) {
+        add(e);
+      }
+      return true;
+    }
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeAddAllInsideOther.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeAddAllInsideOther.java
new file mode 100644 (file)
index 0000000..669d538
--- /dev/null
@@ -0,0 +1,24 @@
+// "Replace with addAll" "true"
+
+import java.util.*;
+
+public class Main {
+  static class MyList extends AbstractCollection<String> {
+    @Override
+    public Iterator<String> iterator() {
+      return Collections.emptyIterator();
+    }
+
+    @Override
+    public int size() {
+      return 0;
+    }
+
+    public boolean myAdd(Collection<? extends String> c) {
+      for (String e : <caret>c) {
+        this.add(e);
+      }
+      return true;
+    }
+  }
+}
\ No newline at end of file