remove this qualifier on anonym -> method ref fix
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 28 Oct 2015 17:14:50 +0000 (18:14 +0100)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 28 Oct 2015 18:45:32 +0000 (19:45 +0100)
java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeMethodReferenceInspection.java
java/java-analysis-impl/src/com/intellij/codeInspection/LambdaCanBeMethodReferenceInspection.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2methodReference/afterUnqualifiedThis.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2methodReference/beforeUnqualifiedThis.java [new file with mode: 0644]

index ea2209f232eee80ea1d2f624a1bad0d07fdb4e3f..04a925b970d9d687f40cd0052180c171724537e0 100644 (file)
@@ -19,7 +19,6 @@ import com.intellij.codeInsight.FileModificationService;
 import com.intellij.codeInsight.daemon.GroupNames;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Condition;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
@@ -69,7 +68,7 @@ public class AnonymousCanBeMethodReferenceInspection extends BaseJavaBatchLocalI
           final PsiMethod method = aClass.getMethods()[0];
           final PsiCodeBlock body = method.getBody();
           final PsiCallExpression callExpression =
-            LambdaCanBeMethodReferenceInspection.canBeMethodReferenceProblem(body, method.getParameterList().getParameters(), aClass.getBaseClassType());
+            LambdaCanBeMethodReferenceInspection.canBeMethodReferenceProblem(body, method.getParameterList().getParameters(), aClass.getBaseClassType(), aClass.getParent());
           if (callExpression != null) {
             final PsiMethod resolveMethod = callExpression.resolveMethod();
             if (resolveMethod != method &&
@@ -118,7 +117,7 @@ public class AnonymousCanBeMethodReferenceInspection extends BaseJavaBatchLocalI
 
           final PsiParameter[] parameters = methods[0].getParameterList().getParameters();
           final PsiCallExpression callExpression = LambdaCanBeMethodReferenceInspection
-            .canBeMethodReferenceProblem(methods[0].getBody(), parameters, anonymousClass.getBaseClassType());
+            .canBeMethodReferenceProblem(methods[0].getBody(), parameters, anonymousClass.getBaseClassType(), anonymousClass.getParent());
           if (callExpression == null) return;
           final String methodRefText =
             LambdaCanBeMethodReferenceInspection.createMethodReferenceText(callExpression, anonymousClass.getBaseClassType(), parameters);
index 140f672f1a3016177989e529bd9711cd2ce34f44..2abba13e3c347efd33cbadfbb95280eab3afcf2b 100644 (file)
@@ -89,8 +89,16 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
 
   @Nullable
   public static PsiCallExpression canBeMethodReferenceProblem(@Nullable final PsiElement body,
-                                                                 final PsiParameter[] parameters,
-                                                                 PsiType functionalInterfaceType) {
+                                                              final PsiParameter[] parameters,
+                                                              final PsiType functionalInterfaceType) {
+    return canBeMethodReferenceProblem(body, parameters, functionalInterfaceType, null);
+  }
+
+  @Nullable
+  public static PsiCallExpression canBeMethodReferenceProblem(@Nullable final PsiElement body,
+                                                              final PsiParameter[] parameters,
+                                                              PsiType functionalInterfaceType, 
+                                                              @Nullable PsiElement context) {
     final PsiCallExpression callExpression = extractMethodCallFromBlock(body);
     if (callExpression instanceof PsiNewExpression) {
       final PsiNewExpression newExpression = (PsiNewExpression)callExpression;
@@ -104,7 +112,7 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
       LOG.assertTrue(callExpression != null);
       final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(callExpression.getProject());
       final PsiMethodReferenceExpression methodReferenceExpression = 
-        (PsiMethodReferenceExpression)elementFactory.createExpressionFromText(methodReferenceText, callExpression);
+        (PsiMethodReferenceExpression)elementFactory.createExpressionFromText(methodReferenceText, context != null ? context : callExpression);
       final Map<PsiElement, PsiType> map = LambdaUtil.getFunctionalTypeMap();
       try {
         map.put(methodReferenceExpression, functionalInterfaceType);
@@ -367,7 +375,10 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
         return getClassReferenceName(containingClass);
       }
       else {
-        final PsiClass parentContainingClass = PsiTreeUtil.getParentOfType(methodCall, PsiClass.class);
+        PsiClass parentContainingClass = PsiTreeUtil.getParentOfType(methodCall, PsiClass.class);
+        if (parentContainingClass instanceof PsiAnonymousClass) {
+          parentContainingClass = PsiTreeUtil.getParentOfType(parentContainingClass, PsiClass.class, true);
+        }
         PsiClass treeContainingClass = parentContainingClass;
         while (treeContainingClass != null && !InheritanceUtil.isInheritorOrSelf(treeContainingClass, containingClass, true)) {
           treeContainingClass = PsiTreeUtil.getParentOfType(treeContainingClass, PsiClass.class, true);
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2methodReference/afterUnqualifiedThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2methodReference/afterUnqualifiedThis.java
new file mode 100644 (file)
index 0000000..e777691
--- /dev/null
@@ -0,0 +1,12 @@
+// "Replace with method reference" "true"
+class Test {
+
+  private void doTest (){}
+
+  void foo(Runnable r){}
+
+  {
+    foo (this::doTest);
+  }
+
+}
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2methodReference/beforeUnqualifiedThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2methodReference/beforeUnqualifiedThis.java
new file mode 100644 (file)
index 0000000..35b98c0
--- /dev/null
@@ -0,0 +1,17 @@
+// "Replace with method reference" "true"
+class Test {
+
+  private void doTest (){}
+
+  void foo(Runnable r){}
+
+  {
+    foo (new Ru<caret>nnable() {
+      @Override
+      public void run() {
+        doTest();
+      }
+    });
+  }
+
+}