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 bbf8da8de745..86a854e47089 100644 --- a/java/java-impl/src/com/intellij/codeInspection/util/OptionalUtil.java +++ b/java/java-impl/src/com/intellij/codeInspection/util/OptionalUtil.java @@ -141,11 +141,14 @@ public class OptionalUtil { @NotNull public static String getMapTypeArgument(PsiExpression expression, PsiType type) { if (!(type instanceof PsiClassType)) return ""; - if (((PsiClassType)type).getParameterCount() == 0) { - PsiExpression copy = - JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(expression.getText(), expression); - PsiType exprType = copy.getType(); - if (exprType != null && !LambdaUtil.notInferredType(exprType) && TypeConversionUtil.isAssignable(type, exprType)) return ""; + PsiExpression copy = + JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(expression.getText(), expression); + PsiType exprType = copy.getType(); + if (exprType != null && + !exprType.equals(PsiType.NULL) && + !LambdaUtil.notInferredType(exprType) && + TypeConversionUtil.isAssignable(type, exprType)) { + return ""; } return "<" + type.getCanonicalText() + ">"; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCastExpected.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCastExpected.java index 06440af2b9fb..cbf887b56104 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCastExpected.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCastExpected.java @@ -5,7 +5,7 @@ import java.util.stream.Collectors; class Test { public static List> fromString(final T src, Function> extractor) { - final List> result = extractor.apply(src).stream().>map(TokenFilter::new).collect(Collectors.toList()); + final List> result = extractor.apply(src).stream().map((Function>) TokenFilter::new).collect(Collectors.toList()); return result; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectContravariant.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectContravariant.java new file mode 100644 index 000000000000..3aaf86530da0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectContravariant.java @@ -0,0 +1,14 @@ +// "Replace with collect" "true" + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Main { + public static List> test(List list) { + List> strings = list.stream().>map(Collections::singletonList).collect(Collectors.toList()); + return strings; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectNulls.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectNulls.java new file mode 100644 index 000000000000..fabf0f2c51f3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectNulls.java @@ -0,0 +1,13 @@ +// "Replace with collect" "true" + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Main { + public static List test() { + List strings = IntStream.range(0, 10).mapToObj(x -> null).collect(Collectors.toList()); + return strings; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSupertype.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSupertype.java new file mode 100644 index 000000000000..f242758b3d7c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSupertype.java @@ -0,0 +1,14 @@ +// "Replace with collect" "true" + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class Main { + public static List> test(List list) { + List> strings = list.stream().map(Collections::singletonList).collect(Collectors.toList()); + return strings; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectContravariant.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectContravariant.java new file mode 100644 index 000000000000..bbf8206bda7e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectContravariant.java @@ -0,0 +1,17 @@ +// "Replace with collect" "true" + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class Main { + public static List> test(List list) { + List> strings = new ArrayList<>(); + for (String s : list) { + List e = Collections.singletonList(s); + strings.add(e); + } + return strings; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectNulls.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectNulls.java new file mode 100644 index 000000000000..476977704b11 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectNulls.java @@ -0,0 +1,14 @@ +// "Replace with collect" "true" + +import java.util.ArrayList; +import java.util.List; + +public class Main { + public static List test() { + List strings = new ArrayList<>(); + for(int x = 0; x < 10; x++) { + strings.add(null); + } + return strings; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectSupertype.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectSupertype.java new file mode 100644 index 000000000000..09ba97e24cef --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectSupertype.java @@ -0,0 +1,17 @@ +// "Replace with collect" "true" + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class Main { + public static List> test(List list) { + List> strings = new ArrayList<>(); + for (String s : list) { + List e = Collections.singletonList(s); + strings.add(e); + } + return strings; + } +} \ No newline at end of file