summaryrefslogtreecommitdiff
path: root/java/java-analysis-impl/src/com/intellij/codeInspection/LambdaCanBeMethodReferenceInspection.java
blob: 1e887030dcb4f1beb76657289c9a56eb3b111f49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright 2000-2012 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.codeInspection;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ArrayUtilRt;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * User: anna
 */
public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInspectionTool {
  public static final Logger LOG = Logger.getInstance("#" + LambdaCanBeMethodReferenceInspection.class.getName());

  @Nls
  @NotNull
  @Override
  public String getGroupDisplayName() {
    return GroupNames.LANGUAGE_LEVEL_SPECIFIC_GROUP_NAME;
  }

  @Nls
  @NotNull
  @Override
  public String getDisplayName() {
    return "Lambda can be replaced with method reference";
  }

  @Override
  public boolean isEnabledByDefault() {
    return true;
  }

  @NotNull
  @Override
  public String getShortName() {
    return "Convert2MethodRef";
  }

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitLambdaExpression(PsiLambdaExpression expression) {
        super.visitLambdaExpression(expression);
        if (PsiUtil.getLanguageLevel(expression).isAtLeast(LanguageLevel.JDK_1_8)) {
          final PsiElement body = expression.getBody();
          final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
          if (functionalInterfaceType != null) {
            final PsiCallExpression callExpression = canBeMethodReferenceProblem(body, expression.getParameterList().getParameters(), functionalInterfaceType);
            if (callExpression != null) {
              holder.registerProblem(callExpression,
                                     "Can be replaced with method reference",
                                     ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithMethodRefFix());
            }
          }
        }
      }
    };
  }

  @Nullable
  protected static PsiCallExpression canBeMethodReferenceProblem(@Nullable final PsiElement body,
                                                                 final PsiParameter[] parameters,
                                                                 PsiType functionalInterfaceType) {
    PsiCallExpression methodCall = extractMethodCallFromBlock(body);

    if (methodCall instanceof PsiNewExpression && ((PsiNewExpression)methodCall).getAnonymousClass() != null) return null;

    if (methodCall != null) {
      final PsiExpressionList argumentList = methodCall.getArgumentList();
      if (argumentList != null) {
        final PsiExpression[] expressions = argumentList.getExpressions();

        PsiMethod psiMethod = methodCall.resolveMethod();
        PsiClass containingClass;
        boolean isConstructor;
        if (psiMethod == null) {
          isConstructor = true;
          if (!(methodCall instanceof PsiNewExpression)) return null;
          final PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)methodCall).getClassOrAnonymousClassReference();
          if (classReference == null) return null;
          containingClass = (PsiClass)classReference.resolve();
        }
        else {
          containingClass = psiMethod.getContainingClass();
          isConstructor = psiMethod.isConstructor();
        }
        boolean isReceiverType = PsiMethodReferenceUtil.isReceiverType(functionalInterfaceType, containingClass, psiMethod);
        if (isReceiverType && psiMethod != null) {
          PsiMethod nonAmbiguousMethod = ensureNonAmbiguousMethod(parameters, psiMethod);
          if (nonAmbiguousMethod == null) return null;
          psiMethod = nonAmbiguousMethod;
          containingClass = nonAmbiguousMethod.getContainingClass();
        }
        if (containingClass == null) return null;
        final boolean staticOrValidConstructorRef;
        if (isConstructor) {
          staticOrValidConstructorRef =
            (containingClass.getContainingClass() == null || containingClass.hasModifierProperty(PsiModifier.STATIC));
        }
        else {
          staticOrValidConstructorRef = psiMethod.hasModifierProperty(PsiModifier.STATIC);
        }

        final int offset = isReceiverType && !staticOrValidConstructorRef ? 1 : 0;
        if (parameters.length != expressions.length + offset) return null;

        for (int i = 0; i < expressions.length; i++) {
          PsiExpression psiExpression = expressions[i];
          if (!(psiExpression instanceof PsiReferenceExpression)) return null;
          final PsiElement resolve = ((PsiReferenceExpression)psiExpression).resolve();
          if (resolve == null) return null;
          if (parameters[i + offset] != resolve) return null;
        }

        final PsiExpression qualifierExpression;
        if (methodCall instanceof PsiMethodCallExpression) {
          qualifierExpression = ((PsiMethodCallExpression)methodCall).getMethodExpression().getQualifierExpression();
        }
        else if (methodCall instanceof PsiNewExpression) {
          qualifierExpression = ((PsiNewExpression)methodCall).getQualifier();
        }
        else {
          qualifierExpression = null;
        }
        if (offset > 0) {
          if (!(qualifierExpression instanceof PsiReferenceExpression) ||
              ((PsiReferenceExpression)qualifierExpression).resolve() != parameters[0]) {
            return null;
          }
        }
        else if (qualifierExpression != null) {
          final Ref<Boolean> usedInQualifier = new Ref<Boolean>(false);
          qualifierExpression.accept(new JavaRecursiveElementWalkingVisitor() {
            @Override
            public void visitReferenceExpression(PsiReferenceExpression expression) {
              final PsiElement resolve = expression.resolve();
              if (resolve instanceof PsiParameter && ArrayUtilRt.find(parameters, resolve) > -1) {
                usedInQualifier.set(true);
                return;
              }
              super.visitReferenceExpression(expression);
            }

            @Override
            public void visitNewExpression(PsiNewExpression expression) {
              usedInQualifier.set(true);
              super.visitNewExpression(expression);
            }

            @Override
            public void visitMethodCallExpression(PsiMethodCallExpression expression) {
              usedInQualifier.set(true);
              super.visitMethodCallExpression(expression);
            }
          });
          if (usedInQualifier.get()) return null;
        } else if (containingClass != PsiTreeUtil.getParentOfType(body, PsiClass.class) && containingClass.getName() == null) {
          return null;
        }
        return methodCall;
      } else if (methodCall instanceof PsiNewExpression) {
        final PsiExpression[] dimensions = ((PsiNewExpression)methodCall).getArrayDimensions();
        if (dimensions.length > 0) {
          final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
          if (interfaceMethod != null) {
            final PsiParameter[] psiParameters = interfaceMethod.getParameterList().getParameters();
            if (psiParameters.length == 1 && PsiType.INT.equals(psiParameters[0].getType())) {
              return methodCall;
            }
          }
        }
      }
    }
    return null;
  }

  public static PsiCallExpression extractMethodCallFromBlock(PsiElement body) {
    PsiCallExpression methodCall = null;
    if (body instanceof PsiCallExpression) {
      methodCall = (PsiCallExpression)body;
    }
    else if (body instanceof PsiCodeBlock) {
      final PsiStatement[] statements = ((PsiCodeBlock)body).getStatements();
      if (statements.length == 1) {
        if (statements[0] instanceof PsiReturnStatement) {
          final PsiExpression returnValue = ((PsiReturnStatement)statements[0]).getReturnValue();
          if (returnValue instanceof PsiCallExpression) {
            methodCall = (PsiCallExpression)returnValue;
          }
        }
        else if (statements[0] instanceof PsiExpressionStatement) {
          final PsiExpression expr = ((PsiExpressionStatement)statements[0]).getExpression();
          if (expr instanceof PsiCallExpression) {
            methodCall = (PsiCallExpression)expr;
          }
        }
      }
    }
    else if (body instanceof PsiBlockStatement) {
      return extractMethodCallFromBlock(((PsiBlockStatement)body).getCodeBlock());
    }
    else if (body instanceof PsiExpressionStatement) {
      final PsiExpression expression = ((PsiExpressionStatement)body).getExpression();
      if (expression instanceof PsiCallExpression) {
        methodCall = (PsiCallExpression)expression;
      }
    }
    return methodCall;
  }

  @Nullable
  private static PsiMethod ensureNonAmbiguousMethod(PsiParameter[] parameters, @NotNull PsiMethod psiMethod) {
    String methodName = psiMethod.getName();
    PsiClass containingClass = psiMethod.getContainingClass();
    if (containingClass == null) return null;
    for (PsiMethod method : containingClass.findMethodsByName(methodName, false)) {
      PsiParameter[] candidateParams = method.getParameterList().getParameters();
      if (candidateParams.length == 1) {
        if (TypeConversionUtil.areTypesConvertible(candidateParams[0].getType(), parameters[0].getType())) {
          for (PsiMethod superMethod : psiMethod.findDeepestSuperMethods()) {
            PsiMethod validSuperMethod = ensureNonAmbiguousMethod(parameters, superMethod);
            if (validSuperMethod != null) return validSuperMethod;
          }
          return null;
        }
      }
    }
    return psiMethod;
  }

  @Nullable
  protected static String createMethodReferenceText(final PsiElement element,
                                                    final PsiType functionalInterfaceType,
                                                    final PsiParameter[] parameters) {
    String methodRefText = null;
    if (element instanceof PsiMethodCallExpression) {
      final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element;
      final PsiMethod psiMethod = methodCall.resolveMethod();
      if (psiMethod == null) return null;
      PsiClass containingClass = psiMethod.getContainingClass();
      LOG.assertTrue(containingClass != null);
      final PsiReferenceExpression methodExpression = methodCall.getMethodExpression();
      final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
      final String methodReferenceName = methodExpression.getReferenceName();
      if (qualifierExpression != null) {
        boolean isReceiverType = PsiMethodReferenceUtil.isReceiverType(functionalInterfaceType, containingClass, psiMethod);
        final String qualifier = isReceiverType ? composeReceiverQualifierText(parameters, psiMethod, containingClass, qualifierExpression) 
                                                : qualifierExpression.getText();
        methodRefText = qualifier + "::" + ((PsiMethodCallExpression)element).getTypeArgumentList().getText() + methodReferenceName;
      }
      else {
        if (psiMethod.hasModifierProperty(PsiModifier.STATIC)) {
          methodRefText = getClassReferenceName(containingClass);
        } else {
          if (containingClass != PsiTreeUtil.getParentOfType(element, PsiClass.class) ) {
            methodRefText = containingClass.getName() + ".this";
          } else {
            methodRefText = "this";
          }
        }
        methodRefText += "::" + methodReferenceName;
      }
    }
    else if (element instanceof PsiNewExpression) {
      final PsiMethod constructor = ((PsiNewExpression)element).resolveConstructor();
      PsiClass containingClass = null;
      if (constructor != null) {
        containingClass = constructor.getContainingClass();
        LOG.assertTrue(containingClass != null);
      }
      else {
        final PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)element).getClassOrAnonymousClassReference();
        if (classReference != null) {
          final JavaResolveResult resolve = classReference.advancedResolve(false);
          final PsiElement resolveElement = resolve.getElement();
          if (resolveElement instanceof PsiClass) {
            containingClass = (PsiClass)resolveElement;
          }
        }
      }
      final PsiType newExprType = ((PsiNewExpression)element).getType();
      if (containingClass != null) {
        methodRefText = getClassReferenceName(containingClass);
      } else if (newExprType instanceof PsiArrayType){
        final PsiType deepComponentType = newExprType.getDeepComponentType();
        if (deepComponentType instanceof PsiPrimitiveType) {
          methodRefText = deepComponentType.getCanonicalText();
        }
      }

      if (methodRefText != null) {
        if (newExprType != null) {
          int dim = newExprType.getArrayDimensions();
          while (dim-- > 0) {
            methodRefText += "[]";
          }
        }
        methodRefText += "::new";
      }
    }
    return methodRefText;
  }

  private static String composeReceiverQualifierText(PsiParameter[] parameters,
                                                     PsiMethod psiMethod,
                                                     PsiClass containingClass,
                                                     @NotNull PsiExpression qualifierExpression) {
    final PsiMethod nonAmbiguousMethod = ensureNonAmbiguousMethod(parameters, psiMethod);
    LOG.assertTrue(nonAmbiguousMethod != null);
    final PsiClass nonAmbiguousContainingClass = nonAmbiguousMethod.getContainingClass();
    if (!containingClass.equals(nonAmbiguousContainingClass)) {
      return getClassReferenceName(nonAmbiguousContainingClass);
    }

    if (nonAmbiguousContainingClass.isPhysical() && qualifierExpression instanceof PsiReferenceExpression) {
      final PsiElement resolve = ((PsiReferenceExpression)qualifierExpression).resolve();
      if (resolve instanceof PsiParameter && ArrayUtil.find(parameters, resolve) > -1 && ((PsiParameter)resolve).getTypeElement() == null) {
        return getClassReferenceName(nonAmbiguousContainingClass);
      }
    }

    final PsiType qualifierExpressionType = qualifierExpression.getType();
    return qualifierExpressionType != null ? qualifierExpressionType.getCanonicalText() : getClassReferenceName(nonAmbiguousContainingClass);
  }

  private static String getClassReferenceName(PsiClass containingClass) {
    final String qualifiedName = containingClass.getQualifiedName();
    if (qualifiedName != null) {
      return qualifiedName;
    }
    else {
      final String containingClassName = containingClass.getName();
      return containingClassName != null ? containingClassName : "";
    }
  }

  private static class ReplaceWithMethodRefFix implements LocalQuickFix {
    @NotNull
    @Override
    public String getName() {
      return "Replace lambda with method reference";
    }

    @NotNull
    @Override
    public String getFamilyName() {
      return getName();
    }

    @Override
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      final PsiElement element = descriptor.getPsiElement();
      final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
      if (lambdaExpression == null) return;
      final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
      if (functionalInterfaceType == null || !functionalInterfaceType.isValid()) return;
      final String methodRefText = createMethodReferenceText(element, functionalInterfaceType,
                                                             lambdaExpression.getParameterList().getParameters());

      if (methodRefText != null) {
        final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
        final PsiExpression psiExpression =
          factory.createExpressionFromText(methodRefText, lambdaExpression);
        PsiElement replace = lambdaExpression.replace(psiExpression);
        if (((PsiMethodReferenceExpression)replace).getFunctionalInterfaceType() == null) { //ambiguity
          final PsiTypeCastExpression cast = (PsiTypeCastExpression)factory.createExpressionFromText("(A)a", replace);
          cast.getCastType().replace(factory.createTypeElement(functionalInterfaceType));
          cast.getOperand().replace(replace);
          replace = replace.replace(cast);
        }
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(replace);
      }
    }
  }
}