diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java index b4ca43f4cc0b..265e1b6d9946 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java @@ -387,7 +387,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo final PsiExpressionList argumentList = ((PsiNewExpression)initializer).getArgumentList(); if (argumentList != null && argumentList.getExpressions().length == 0) { restoreComments(foreachStatement, body); - final String callText = builder.toString() + createInitializerReplacementText(initializer) + ")"; + final String callText = builder.toString() + createInitializerReplacementText(((PsiVariable)resolve).getType(), initializer) + ")"; result = initializer.replace(elementFactory.createExpressionFromText(callText, null)); simplifyRedundantCast(result); foreachStatement.delete(); @@ -412,14 +412,24 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo } } - private static String createInitializerReplacementText(PsiExpression initializer) { + private static String createInitializerReplacementText(PsiType varType, PsiExpression initializer) { final PsiType initializerType = initializer.getType(); final PsiClassType rawType = initializerType instanceof PsiClassType ? ((PsiClassType)initializerType).rawType() : null; - if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST)) { + final PsiClassType rawVarType = varType instanceof PsiClassType ? ((PsiClassType)varType).rawType() : null; + if (rawType != null && rawVarType != null && + rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST) && + rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_LIST)) { return "toList()"; - } else if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET)) { + } + else if (rawType != null && rawVarType != null && + rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET) && + rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_SET)) { return "toSet()"; - } else { + } + else if (rawType != null) { + return "toCollection(" + rawType.getClassName() + "::new)"; + } + else { return "toCollection(() -> " + initializer.getText() +")"; } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterArrayListVariableType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterArrayListVariableType.java new file mode 100644 index 000000000000..6d59beba0bfe --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterArrayListVariableType.java @@ -0,0 +1,12 @@ +// "Replace with collect" "true" +import java.util.*; +import java.util.stream.Collectors; + +class A { + public static void main(List args) { + ArrayList uniqNames = args.stream().map(name -> name.substring(1)).collect(Collectors.toCollection(ArrayList::new)); + uniqNames.forEach(System.out::println); + } + + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectLinkedHashSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectLinkedHashSet.java index 6bb6839b0cea..031edc64f14d 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectLinkedHashSet.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectLinkedHashSet.java @@ -10,6 +10,6 @@ public class Collect { } void collectNames(List persons){ - Set names = persons.stream().map(Person::getName).collect(Collectors.toCollection(() -> new LinkedHashSet<>())); + Set names = persons.stream().map(Person::getName).collect(Collectors.toCollection(LinkedHashSet::new)); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeArrayListVariableType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeArrayListVariableType.java new file mode 100644 index 000000000000..b03af38ce844 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeArrayListVariableType.java @@ -0,0 +1,14 @@ +// "Replace with collect" "true" +import java.util.*; + +class A { + public static void main(List args) { + ArrayList uniqNames = new ArrayList<>(); + for (String name : args){ + uniqNames.add(name.substring(1)); + } + uniqNames.forEach(System.out::println); + } + + +} \ No newline at end of file