diff --git a/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/PseudoLambdaReplaceTemplate.java b/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/PseudoLambdaReplaceTemplate.java index cff890eb515c..ca1ee7d2474b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/PseudoLambdaReplaceTemplate.java +++ b/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/PseudoLambdaReplaceTemplate.java @@ -22,7 +22,6 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.impl.PsiDiamondTypeUtil; -import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.*; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; @@ -156,16 +155,17 @@ class PseudoLambdaReplaceTemplate { return null; } - final PsiMethod method = expression.resolveMethod(); - if (method == null) { + final JavaResolveResult result = expression.getMethodExpression().advancedResolve(false); + final PsiElement element = result.getElement(); + if (!(element instanceof PsiMethod)) { return null; } - final PsiParameter[] expectedParameters = method.getParameterList().getParameters(); + final PsiParameter[] expectedParameters = ((PsiMethod)element).getParameterList().getParameters(); if (argumentTypes.length != expectedParameters.length) { return null; } - final JavaResolveResult result = expression.getMethodExpression().advancedResolve(false); + final PsiSubstitutor methodSubstitutor = result.getSubstitutor(); return validate(argumentTypes, methodReturnType, methodSubstitutor, expression); } @@ -357,8 +357,7 @@ class PseudoLambdaReplaceTemplate { } @NotNull - public PsiExpression convertToStream(final PsiMethodCallExpression expression, PsiMethod method, boolean force) { - LOG.assertTrue(expression != null); + public PsiExpression convertToStream(@NotNull final PsiMethodCallExpression expression, @Nullable PsiMethod method, boolean force) { if (method == null) { method = expression.resolveMethod(); if (method == null) { @@ -556,15 +555,9 @@ class PseudoLambdaReplaceTemplate { private static boolean isIterableOrArray(final PsiType type) { if (type instanceof PsiClassType) { - final PsiClass resolvedClass = ((PsiClassType)type).resolve(); - if (resolvedClass != null) { - return InheritanceUtil.isInheritor(resolvedClass, CommonClassNames.JAVA_LANG_ITERABLE); - } + return InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE); } - else if (type instanceof PsiArrayType) { - return true; - } - return false; + return type instanceof PsiArrayType; } @Override diff --git a/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/StaticPseudoFunctionalStyleMethodInspection.java b/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/StaticPseudoFunctionalStyleMethodInspection.java index 398aabc0ff20..58959f11cc3d 100644 --- a/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/StaticPseudoFunctionalStyleMethodInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/java18StreamApi/StaticPseudoFunctionalStyleMethodInspection.java @@ -61,47 +61,46 @@ public class StaticPseudoFunctionalStyleMethodInspection extends BaseJavaBatchLo if (!PsiUtil.isLanguageLevel8OrHigher(holder.getFile())) { return PsiElementVisitor.EMPTY_VISITOR; } - return new PsiElementVisitor() { + return new JavaElementVisitor() { @Override - public void visitElement(PsiElement element) { - if (element instanceof PsiMethodCallExpression) { - final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)element; - String qName = methodCallExpression.getMethodExpression().getQualifiedName(); - if (qName == null) { - return; - } - qName = StringUtil.getShortName(qName); - final Collection handlerInfos = myOptions.findElementsByMethodName(qName); - if (handlerInfos.isEmpty()) { - return; - } - final PsiMethod method = methodCallExpression.resolveMethod(); - if (method == null) { - return; - } - final PsiClass aClass = method.getContainingClass(); - if (aClass == null) { - return; - } - final String classQualifiedName = aClass.getQualifiedName(); - if (classQualifiedName == null) { - return; - } - StaticPseudoFunctionalStyleMethodOptions.PipelineElement suitableHandler = null; - for (StaticPseudoFunctionalStyleMethodOptions.PipelineElement h : handlerInfos) { - if (h.getHandlerClass().equals(classQualifiedName)) { - suitableHandler = h; - break; - } - } - if (suitableHandler == null) { - return; - } - final PseudoLambdaReplaceTemplate.ValidationInfo validationInfo = suitableHandler.getTemplate().validate(methodCallExpression); - if (validationInfo != null) { - holder.registerProblem(methodCallExpression.getMethodExpression(), "Pseudo functional style code", new ReplacePseudoLambdaWithLambda(suitableHandler)); + public void visitMethodCallExpression(PsiMethodCallExpression methodCallExpression) { + String qName = methodCallExpression.getMethodExpression().getQualifiedName(); + if (qName == null) { + return; + } + qName = StringUtil.getShortName(qName); + final Collection handlerInfos = myOptions.findElementsByMethodName(qName); + if (handlerInfos.isEmpty()) { + return; + } + final PsiMethod method = methodCallExpression.resolveMethod(); + if (method == null) { + return; + } + final PsiClass aClass = method.getContainingClass(); + if (aClass == null) { + return; + } + final String classQualifiedName = aClass.getQualifiedName(); + if (classQualifiedName == null) { + return; + } + StaticPseudoFunctionalStyleMethodOptions.PipelineElement suitableHandler = null; + for (StaticPseudoFunctionalStyleMethodOptions.PipelineElement h : handlerInfos) { + if (h.getHandlerClass().equals(classQualifiedName)) { + suitableHandler = h; + break; } } + if (suitableHandler == null) { + return; + } + final PseudoLambdaReplaceTemplate.ValidationInfo validationInfo = suitableHandler.getTemplate().validate(methodCallExpression); + if (validationInfo != null) { + holder.registerProblem(methodCallExpression.getMethodExpression(), + "Pseudo functional style code", + new ReplacePseudoLambdaWithLambda(suitableHandler)); + } } }; } @@ -128,9 +127,13 @@ public class StaticPseudoFunctionalStyleMethodInspection extends BaseJavaBatchLo @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { - PsiMethodCallExpression expression = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiMethodCallExpression.class); - LOG.assertTrue(expression != null); - myHandler.getTemplate().convertToStream(expression, null, false); + PsiElement psiElement = descriptor.getPsiElement(); + if (psiElement instanceof PsiReferenceExpression) { + PsiElement parent = psiElement.getParent(); + if (parent instanceof PsiMethodCallExpression) { + myHandler.getTemplate().convertToStream((PsiMethodCallExpression)parent, null, false); + } + } } } } \ No newline at end of file