diff --git a/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java b/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java index 1c0a0e070655..5c93f3cddcf6 100644 --- a/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2017 JetBrains s.r.o. + * 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. @@ -24,9 +24,9 @@ import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.refactoring.util.LambdaRefactoringUtil; import com.intellij.util.ArrayUtil; +import com.intellij.util.ObjectUtils; import com.siyeh.ig.psiutils.ParenthesesUtils; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; /** * @author Danila Ponomarenko @@ -40,8 +40,8 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix { return InspectionsBundle.message("inspection.replace.ternary.quickfix", myText); } - public ReplaceWithTernaryOperatorFix(@Nullable PsiExpression expressionToAssert) { - myText = expressionToAssert == null ? "" : ParenthesesUtils.getText(expressionToAssert, ParenthesesUtils.BINARY_AND_PRECEDENCE); + public ReplaceWithTernaryOperatorFix(@NotNull PsiExpression expressionToAssert) { + myText = ParenthesesUtils.getText(expressionToAssert, ParenthesesUtils.BINARY_AND_PRECEDENCE); } @NotNull @@ -53,46 +53,42 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix { @Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); - final PsiExpression expression; - String text; - if(element instanceof PsiMethodReferenceExpression) { - PsiLambdaExpression lambda = - LambdaRefactoringUtil.convertMethodReferenceToLambda((PsiMethodReferenceExpression)element, false, true); - if (lambda == null) return; - expression = LambdaUtil.extractSingleExpressionFromBody(lambda.getBody()); - if (expression == null) return; - PsiParameter parameter = ArrayUtil.getFirstElement(lambda.getParameterList().getParameters()); - if (parameter == null) return; - text = parameter.getName(); - } else { - while (true) { - PsiElement parent = element.getParent(); - if (parent instanceof PsiReferenceExpression || parent instanceof PsiMethodCallExpression) { - element = parent; - } - else { - break; - } + while (true) { + PsiElement parent = element.getParent(); + if (parent instanceof PsiReferenceExpression || parent instanceof PsiMethodCallExpression) { + element = parent; + } + else { + break; } - if (!(element instanceof PsiExpression)) return; - expression = (PsiExpression)element; - text = myText; } - final PsiFile file = expression.getContainingFile(); - PsiConditionalExpression conditionalExpression = replaceWthConditionalExpression(project, text + "!=null", expression, suggestDefaultValue(expression)); + if (!(element instanceof PsiExpression)) { + return; + } + final PsiExpression expression = (PsiExpression)element; + final PsiFile file = expression.getContainingFile(); + PsiConditionalExpression conditionalExpression = + replaceWithConditionalExpression(project, myText + "!=null", expression, suggestDefaultValue(expression)); + + selectElseBranch(file, conditionalExpression); + } + + static void selectElseBranch(PsiFile file, PsiConditionalExpression conditionalExpression) { PsiExpression elseExpression = conditionalExpression.getElseExpression(); if (elseExpression != null) { ((Navigatable)elseExpression).navigate(true); - SelectInEditorManager.getInstance(project).selectInEditor(file.getVirtualFile(), elseExpression.getTextRange().getStartOffset(), elseExpression.getTextRange().getEndOffset(), false, true); + SelectInEditorManager.getInstance(file.getProject()) + .selectInEditor(file.getVirtualFile(), elseExpression.getTextRange().getStartOffset(), elseExpression.getTextRange().getEndOffset(), + false, true); } } @NotNull - private static PsiConditionalExpression replaceWthConditionalExpression(@NotNull Project project, - @NotNull String condition, - @NotNull PsiExpression expression, - @NotNull String defaultValue) { + private static PsiConditionalExpression replaceWithConditionalExpression(@NotNull Project project, + @NotNull String condition, + @NotNull PsiExpression expression, + @NotNull String defaultValue) { final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiElement parent = expression.getParent(); @@ -117,4 +113,31 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix { PsiType type = expression.getType(); return PsiTypesUtil.getDefaultValueOfType(type); } + + public static class ReplaceMethodRefWithTernaryOperatorFix implements LocalQuickFix { + @NotNull + @Override + public String getFamilyName() { + return InspectionsBundle.message("inspection.replace.methodref.ternary.quickfix"); + } + + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + PsiMethodReferenceExpression element = ObjectUtils.tryCast(descriptor.getPsiElement(), PsiMethodReferenceExpression.class); + if (element == null) return; + PsiLambdaExpression lambda = + LambdaRefactoringUtil.convertMethodReferenceToLambda(element, false, true); + if (lambda == null) return; + PsiExpression expression = LambdaUtil.extractSingleExpressionFromBody(lambda.getBody()); + if (expression == null) return; + PsiParameter parameter = ArrayUtil.getFirstElement(lambda.getParameterList().getParameters()); + if (parameter == null) return; + String text = parameter.getName(); + final PsiFile file = expression.getContainingFile(); + PsiConditionalExpression conditionalExpression = replaceWithConditionalExpression(project, text + "!=null", expression, + suggestDefaultValue(expression)); + + selectElseBranch(file, conditionalExpression); + } + } } diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java index 6a80a2efa3e9..4c29ded22283 100644 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java @@ -81,7 +81,7 @@ public class DataFlowInspection extends DataFlowInspectionBase { protected List createMethodReferenceNPEFixes(PsiMethodReferenceExpression methodRef) { List fixes = new ArrayList<>(); ContainerUtil.addIfNotNull(fixes, StreamFilterNotNullFix.makeFix(methodRef)); - fixes.add(new ReplaceWithTernaryOperatorFix(null)); + fixes.add(new ReplaceWithTernaryOperatorFix.ReplaceMethodRefWithTernaryOperatorFix()); return fixes; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefArgument.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefArgument.java index 1194566628e4..0caf074707c4 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefArgument.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefArgument.java @@ -1,4 +1,4 @@ -// "Replace with ' != null ?:'" "true" +// "Replace with null-checking lambda" "true" import java.util.Arrays; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefQualifier.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefQualifier.java index 8bcb3614207e..dcfcaea12b37 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefQualifier.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/afterMethodRefQualifier.java @@ -1,4 +1,4 @@ -// "Replace with ' != null ?:'" "true" +// "Replace with null-checking lambda" "true" import java.util.Arrays; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefArgument.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefArgument.java index 8198f757ef97..3501b235642d 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefArgument.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefArgument.java @@ -1,4 +1,4 @@ -// "Replace with ' != null ?:'" "true" +// "Replace with null-checking lambda" "true" import java.util.Arrays; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefQualifier.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefQualifier.java index f83a2dbc087c..0cf8473585d3 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefQualifier.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithTernaryOperator/beforeMethodRefQualifier.java @@ -1,4 +1,4 @@ -// "Replace with ' != null ?:'" "true" +// "Replace with null-checking lambda" "true" import java.util.Arrays; diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 793322c920ea..af3a77881a63 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -324,6 +324,7 @@ inspection.quickfix.assert.family=Assert inspection.assert.quickfix=Assert ''{0}'' inspection.surround.if.quickfix=Surround with ''if ({0} != null)'' inspection.replace.ternary.quickfix=Replace with ''{0} != null ?:'' +inspection.replace.methodref.ternary.quickfix=Replace with null-checking lambda inspection.surround.if.family=Surround with if inspection.dependency.configure.button.text=Configure dependency rules inspection.surround.requirenonnull.quickfix=Replace with ''Objects.requireNonNull({0})''