From b2df7a760a3399b7a6ac8372c4c186c212a55f4c Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 26 Sep 2017 09:44:21 +0700 Subject: [PATCH] IDEA-179490 "Can be replaced with single expression in functional style" in Java fails when field name matches class name --- .../OptionalIsPresentInspection.java | 8 ++++---- .../psi/codeStyle/JavaCodeStyleManager.java | 8 +++++--- .../afterFieldNameConflict.java | 18 +++++++++++++++++ .../beforeFieldNameConflict.java | 20 +++++++++++++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterFieldNameConflict.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeFieldNameConflict.java diff --git a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java index 4470656d7122..162d7b4a005c 100644 --- a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java @@ -32,6 +32,8 @@ import com.intellij.psi.impl.PsiDiamondTypeUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.PsiUtil; +import com.intellij.util.ArrayUtil; +import com.intellij.util.ObjectUtils; import com.siyeh.ig.psiutils.BoolUtils; import com.siyeh.ig.psiutils.CommentTracker; import com.siyeh.ig.psiutils.ControlFlowUtils; @@ -206,10 +208,8 @@ public class OptionalIsPresentInspection extends BaseJavaBatchLocalInspectionToo PsiType type = optionalVariable.getType(); JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(trueValue.getProject()); SuggestedNameInfo info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, type); - if (info.names.length == 0) { - info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, "value", null, type); - } - String paramName = javaCodeStyleManager.suggestUniqueVariableName(info, trueValue, true).names[0]; + String baseName = ObjectUtils.coalesce(ArrayUtil.getFirstElement(info.names), "value"); + String paramName = javaCodeStyleManager.suggestUniqueVariableName(baseName, trueValue, true); if(trueValue instanceof PsiExpressionStatement) { trueValue = ((PsiExpressionStatement)trueValue).getExpression(); } diff --git a/java/java-psi-api/src/com/intellij/psi/codeStyle/JavaCodeStyleManager.java b/java/java-psi-api/src/com/intellij/psi/codeStyle/JavaCodeStyleManager.java index 8f9d1b9664b7..bf4230e1facb 100644 --- a/java/java-psi-api/src/com/intellij/psi/codeStyle/JavaCodeStyleManager.java +++ b/java/java-psi-api/src/com/intellij/psi/codeStyle/JavaCodeStyleManager.java @@ -179,7 +179,8 @@ public abstract class JavaCodeStyleManager { public abstract String propertyNameToVariableName(@NonNls @NotNull String propertyName, @NotNull VariableKind variableKind); /** - * Suggests a unique name for the variable used at the specified location. + * Suggests a unique name for the variable used at the specified location. The returned name is guaranteed to not shadow + * the existing name. * * @param baseName the base name for the variable. * @param place the location where the variable will be used. @@ -190,12 +191,13 @@ public abstract class JavaCodeStyleManager { public abstract String suggestUniqueVariableName(@NonNls @NotNull String baseName, PsiElement place, boolean lookForward); /** - * Suggests a unique name for the variable used at the specified location. + * Suggests a unique names for the variable used at the specified location. The resulting name info may contain names which + * shadow existing names. * * @param baseNameInfo the base name info for the variable. * @param place the location where the variable will be used. * @param lookForward if true, the existing variables are searched in both directions; if false - only backward - * @return the generated unique name + * @return the generated unique name info. */ @NotNull public SuggestedNameInfo suggestUniqueVariableName(@NotNull SuggestedNameInfo baseNameInfo, diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterFieldNameConflict.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterFieldNameConflict.java new file mode 100644 index 000000000000..96e3df9d8e66 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterFieldNameConflict.java @@ -0,0 +1,18 @@ +// "Replace Optional.isPresent() condition with functional style expression" "true" + +import java.util.Optional; + +public class Test { + private Power power; + + interface Power { + static Optional parseValue(String value) { + return Optional.empty(); + } + } + + void testOpt(String powerValue) { + Optional optPower = Power.parseValue(powerValue); + optPower.ifPresent(power1 -> power = power1); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeFieldNameConflict.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeFieldNameConflict.java new file mode 100644 index 000000000000..bcb55b146a54 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeFieldNameConflict.java @@ -0,0 +1,20 @@ +// "Replace Optional.isPresent() condition with functional style expression" "true" + +import java.util.Optional; + +public class Test { + private Power power; + + interface Power { + static Optional parseValue(String value) { + return Optional.empty(); + } + } + + void testOpt(String powerValue) { + Optional optPower = Power.parseValue(powerValue); + if (optPower.isPresent()) { + power = optPower.get(); + } + } +} \ No newline at end of file