From 55466aeef1657324f5b7b7a31cf016110ac997da Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 16 Mar 2017 11:22:15 +0700 Subject: [PATCH] IDEA-169678 "Replace Optional.isPresent() condition with functional style expression" breaks code in case of 'Collection' return type --- .../codeInspection/util/OptionalUtil.java | 11 ++++++++-- .../afterDifferentTrueFalseTypes.java | 18 ++++++++++++++++ .../beforeDifferentTrueFalseTypes.java | 21 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterDifferentTrueFalseTypes.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentTrueFalseTypes.java diff --git a/java/java-impl/src/com/intellij/codeInspection/util/OptionalUtil.java b/java/java-impl/src/com/intellij/codeInspection/util/OptionalUtil.java index 0aaf900e0f71..ba613719b7a0 100644 --- a/java/java-impl/src/com/intellij/codeInspection/util/OptionalUtil.java +++ b/java/java-impl/src/com/intellij/codeInspection/util/OptionalUtil.java @@ -136,7 +136,7 @@ public class OptionalUtil { } trueExpression = targetType == null ? trueExpression : RefactoringUtil.convertInitializerToNormalExpression(trueExpression, targetType); - String typeArg = getMapTypeArgument(trueExpression, targetType); + String typeArg = getMapTypeArgument(trueExpression, targetType, falseExpression); qualifier += "." + typeArg + "map(" + LambdaUtil.createLambda(var, trueExpression) + ")"; } if (useOrElseGet && !ExpressionUtils.isSimpleExpression(falseExpression)) { @@ -154,6 +154,11 @@ public class OptionalUtil { @NotNull public static String getMapTypeArgument(PsiExpression expression, PsiType type) { + return getMapTypeArgument(expression, type, null); + } + + @NotNull + private static String getMapTypeArgument(PsiExpression expression, PsiType type, PsiExpression falseExpression) { if (!(type instanceof PsiClassType)) return ""; PsiExpression copy = JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(expression.getText(), expression); @@ -162,7 +167,9 @@ public class OptionalUtil { !exprType.equals(PsiType.NULL) && !LambdaUtil.notInferredType(exprType) && TypeConversionUtil.isAssignable(type, exprType)) { - return ""; + if (falseExpression == null) return ""; + PsiType falseType = falseExpression.getType(); + if (falseType != null && falseType.isAssignableFrom(exprType)) return ""; } return "<" + type.getCanonicalText() + ">"; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterDifferentTrueFalseTypes.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterDifferentTrueFalseTypes.java new file mode 100644 index 000000000000..2383e93f0d3f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterDifferentTrueFalseTypes.java @@ -0,0 +1,18 @@ +// "Replace Optional.isPresent() condition with functional style expression" "true" + +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; + +public class TestFile { + + public static Collection example() { + final Optional root = Optional.empty(); + return root.>map(TestFile::foo).orElseGet(Collections::emptyList); + } + + private static Set foo(String s) { + return Collections.emptySet(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentTrueFalseTypes.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentTrueFalseTypes.java new file mode 100644 index 000000000000..787cb3bf43de --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentTrueFalseTypes.java @@ -0,0 +1,21 @@ +// "Replace Optional.isPresent() condition with functional style expression" "true" + +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; +import java.util.Set; + +public class TestFile { + + public static Collection example() { + final Optional root = Optional.empty(); + if (root.isPresent()) { + return foo(root.get()); + } + return Collections.emptyList(); + } + + private static Set foo(String s) { + return Collections.emptySet(); + } +} \ No newline at end of file