convert lambda to method reference: expand the acceptance range when not visible

This commit is contained in:
Anna.Kozlova
2016-10-17 19:00:10 +02:00
parent 2a8fc8cbc6
commit 75ac3842ff
3 changed files with 18 additions and 3 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil;
@@ -102,7 +103,7 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
final PsiExpression candidate =
canBeMethodReferenceProblem(body, expression.getParameterList().getParameters(), functionalInterfaceType, null);
if (candidate != null) {
holder.registerProblem(candidate,
holder.registerProblem(InspectionProjectProfileManager.isInformationLevel(getShortName(), expression) ? expression : candidate,
"Can be replaced with method reference",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithMethodRefFix());
}
@@ -586,8 +587,11 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
PsiElement element = descriptor.getPsiElement();
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
if (element instanceof PsiLambdaExpression) {
element = LambdaUtil.extractSingleExpressionFromBody(((PsiLambdaExpression)element).getBody());
}
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
if (lambdaExpression == null) return;
tryConvertToMethodReference(lambdaExpression, element);
@@ -15,6 +15,8 @@
*/
package com.intellij.profile.codeInspection;
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.InspectionProfile;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
@@ -40,4 +42,13 @@ public abstract class InspectionProjectProfileManager implements InspectionProfi
public InspectionProfile getInspectionProfile(PsiElement element){
return getCurrentProfile();
}
public static boolean isInformationLevel(String shortName, @NotNull PsiElement element) {
final HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
if (key != null) {
final HighlightDisplayLevel errorLevel = getInstance(element.getProject()).getCurrentProfile().getErrorLevel(key, element);
return HighlightDisplayLevel.DO_NOT_SHOW.equals(errorLevel);
}
return false;
}
}
@@ -146,7 +146,7 @@ public abstract class ControlFlowStatementVisitorBase extends BaseInspectionVisi
}
if (myKey != null) {
final Project project = element.getProject();
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getCurrentProfile();
final HighlightDisplayLevel errorLevel = profile.getErrorLevel(myKey, element);
return !HighlightDisplayLevel.DO_NOT_SHOW.equals(errorLevel);
}