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 e00e282f2b42..35da669d3eda 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/StreamApiMigrationInspection.java @@ -261,7 +261,17 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo iteration += ".filter(" + parameter.getName() + " -> " + condition.getText() +")"; } } - iteration +=".map(" + parameter.getName() + " -> " + methodCallExpression.getArgumentList().getExpressions()[0].getText() + ").collect(java.util.stream.Collectors."; + iteration +=".map("; + + final PsiExpression mapperCall = methodCallExpression.getArgumentList().getExpressions()[0]; + + final String methodReferenceText = LambdaCanBeMethodReferenceInspection.createMethodReferenceText(mapperCall, null, new PsiParameter[]{parameter}); + if (methodReferenceText != null) { + iteration += methodReferenceText; + } else { + iteration += parameter.getName() + " -> " + mapperCall.getText(); + } + iteration += ").collect(java.util.stream.Collectors."; String variableName = null; PsiExpression initializer = null; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayList.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayList.java index a9274fd2cbc4..9fbe5abd7edc 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayList.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayList.java @@ -10,6 +10,6 @@ public class Collect { } void collectNames(List persons){ - List names = persons.stream().map(person -> person.getName()).collect(Collectors.toList()); + List names = persons.stream().map(person::getName).collect(Collectors.toList()); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListAndFilter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListAndFilter.java index 4f625cd9427c..b7848359956b 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListAndFilter.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListAndFilter.java @@ -10,6 +10,6 @@ public class Collect { } void collectNames(List persons){ - List names = persons.stream().filter(person -> person != null).map(person -> person.getName()).collect(Collectors.toList()); + List names = persons.stream().filter(person -> person != null).map(person::getName).collect(Collectors.toList()); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListLambda.java new file mode 100644 index 000000000000..e1517740be28 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectArrayListLambda.java @@ -0,0 +1,15 @@ +// "Replace with collect" "true" +import java.util.*; +import java.util.stream.Collectors; + +public class Collect { + class Person { + String getName() { + return ""; + } + } + + void collectNames(List persons){ + List names = persons.stream().map(person -> "name: " + person.getName()).collect(Collectors.toList()); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSet.java index 13f274c4cf08..b65dffeb206f 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSet.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSet.java @@ -10,6 +10,6 @@ public class Collect { } void collectNames(List persons){ - Set names = persons.stream().map(person -> person.getName()).collect(Collectors.toSet()); + Set names = persons.stream().map(person::getName).collect(Collectors.toSet()); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSetFieldInitializer.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSetFieldInitializer.java index 43e84a057c66..ed9239d7620c 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSetFieldInitializer.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectHashSetFieldInitializer.java @@ -11,6 +11,6 @@ public class Collect { Set names = new HashSet<>(); void collectNames(List persons){ - names.addAll(persons.stream().map(person -> person.getName()).collect(Collectors.toList())); + names.addAll(persons.stream().map(person::getName).collect(Collectors.toList())); } } 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 ce7539d68ecd..3acd04ed3416 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 -> person.getName()).collect(Collectors.toCollection(() -> new LinkedHashSet<>())); + Set names = persons.stream().map(person::getName).collect(Collectors.toCollection(() -> new LinkedHashSet<>())); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSelfCollection.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSelfCollection.java index ac40fed1b89c..9e8adaab776b 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSelfCollection.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSelfCollection.java @@ -10,6 +10,6 @@ public abstract class Collect implements Collection{ } void collectNames(List persons){ - addAll(persons.stream().map(person -> person.getName()).collect(Collectors.toList())); + addAll(persons.stream().map(person::getName).collect(Collectors.toList())); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSetParameter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSetParameter.java index da15d53fb48d..9820b8f9be5e 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSetParameter.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterCollectSetParameter.java @@ -10,6 +10,6 @@ public class Collect { } void collectNames(List persons, Set names){ - names.addAll(persons.stream().map(person -> person.getName()).collect(Collectors.toList())); + names.addAll(persons.stream().map(person::getName).collect(Collectors.toList())); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectArrayListLambda.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectArrayListLambda.java new file mode 100644 index 000000000000..434c2c5a1bec --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeCollectArrayListLambda.java @@ -0,0 +1,17 @@ +// "Replace with collect" "true" +import java.util.*; + +public class Collect { + class Person { + String getName() { + return ""; + } + } + + void collectNames(List persons){ + List names = new ArrayList<>(); + for (Person person : persons) { + names.add("name: " + person.getName()); + } + } +}