From ac8ad2c8228fc83c76942b8f1a97eafce351c1bf Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 6 Dec 2017 17:45:39 +0700 Subject: [PATCH] IDEA-183326 Comment disappears in stream to loop --- .../streamMigration/CollectMigration.java | 8 ++++---- .../collect/afterCollectCustomConstructor.java | 10 +++++----- .../collect/afterCollectHashSet.java | 1 + .../collect/beforeCollectCustomConstructor.java | 10 +++++----- .../collect/beforeCollectHashSet.java | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java index 22fde684fabf..6fd38ca2a205 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java @@ -279,7 +279,7 @@ class CollectMigration extends BaseStreamApiMigration { } public String generateCollector(CommentTracker ct) { - return getCollectionCollector(ct.markUnchanged(myInitializer), myTargetType); + return getCollectionCollector(ct, myInitializer, myTargetType); } @Override @@ -313,7 +313,7 @@ class CollectMigration extends BaseStreamApiMigration { } @NotNull - private static String getCollectionCollector(PsiExpression initializer, PsiType type) { + private static String getCollectionCollector(CommentTracker ct, PsiExpression initializer, PsiType type) { String collector; PsiType initializerType = initializer.getType(); PsiClassType rawType = initializerType instanceof PsiClassType ? ((PsiClassType)initializerType).rawType() : null; @@ -332,7 +332,7 @@ class CollectMigration extends BaseStreamApiMigration { } else { PsiExpression copy = JavaPsiFacade.getElementFactory(initializer.getProject()) - .createExpressionFromText(initializer.getText(), initializer); + .createExpressionFromText(ct.text(initializer), initializer); if (copy instanceof PsiNewExpression) { PsiExpressionList argumentList = ((PsiNewExpression)copy).getArgumentList(); if (argumentList != null) { @@ -865,7 +865,7 @@ class CollectMigration extends BaseStreamApiMigration { @Override public String generateTerminal(CommentTracker ct) { - return ".collect(" + getCollectionCollector(ct.markUnchanged(myCreateExpression), myResultType) + ")"; + return ".collect(" + getCollectionCollector(ct, ct.markUnchanged(myCreateExpression), myResultType) + ")"; } @Override diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectCustomConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectCustomConstructor.java index 52e7fcaef38d..5d9f8907386b 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectCustomConstructor.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectCustomConstructor.java @@ -6,10 +6,10 @@ import java.util.stream.Collectors; public class Test { void testList(List input) { - List result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new ArrayList<>(10))); + List result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new ArrayList<>(/*initial size*/10))); System.out.println(result); - ArrayList result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new ArrayList<>(20))); + ArrayList result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new ArrayList<>/*valuable comment*/(20))); System.out.println(result2); // Non-empty @@ -26,10 +26,10 @@ public class Test { } void testSet(List input) { - Set result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new HashSet<>(10))); + Set result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new HashSet/*comment*/<>(/*size*/10))); System.out.println(result); - Collection result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new LinkedHashSet<>(20, 0.8f))); + Collection result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new LinkedHashSet<>(20, /*load factor*/ 0.8f))); System.out.println(result2); // Non-empty @@ -37,7 +37,7 @@ public class Test { input.stream().filter(s -> !s.isEmpty()).forEach(result3::add); System.out.println(result3); - Collection result4 = input.stream().filter(s -> !s.isEmpty()).map(TimeUnit::valueOf).collect(Collectors.toCollection(() -> EnumSet.noneOf(TimeUnit.class))); + Collection result4 = input.stream().filter(s -> !s.isEmpty()).map(TimeUnit::valueOf).collect(Collectors.toCollection(() -> EnumSet.noneOf(/*timeunits*/TimeUnit.class))); System.out.println(result4); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectHashSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectHashSet.java index 5d6cab563c56..eea9f78d4568 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectHashSet.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/afterCollectHashSet.java @@ -11,5 +11,6 @@ public class Collect { void collectNames(List persons){ Set names = persons.stream().map(Person::getName).collect(Collectors.toSet()); + /*valuable comment*/ } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectCustomConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectCustomConstructor.java index e1e4c3cd7de0..38688378ff5d 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectCustomConstructor.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectCustomConstructor.java @@ -5,7 +5,7 @@ import java.util.concurrent.TimeUnit; public class Test { void testList(List input) { - List result = new ArrayList<>(10); + List result = new ArrayList<>(/*initial size*/10); for (String s : input) { if (!s.isEmpty()) { result.add(s); @@ -13,7 +13,7 @@ public class Test { } System.out.println(result); - ArrayList result2 = new ArrayList<>(20); + ArrayList result2 = new ArrayList<>/*valuable comment*/(20); for (String s : input) { if (!s.isEmpty()) { result2.add(s); @@ -43,7 +43,7 @@ public class Test { } void testSet(List input) { - Set result = new HashSet<>(10); + Set result = new HashSet/*comment*/<>(/*size*/10); for (String s : input) { if (!s.isEmpty()) { result.add(s); @@ -51,7 +51,7 @@ public class Test { } System.out.println(result); - Collection result2 = new LinkedHashSet<>(20, 0.8f); + Collection result2 = new LinkedHashSet<>(20, /*load factor*/ 0.8f); for (String s : input) { if (!s.isEmpty()) { result2.add(s); @@ -68,7 +68,7 @@ public class Test { } System.out.println(result3); - Collection result4 = EnumSet.noneOf(TimeUnit.class); + Collection result4 = EnumSet.noneOf(/*timeunits*/TimeUnit.class); for (String s : input) { if (!s.isEmpty()) { result4.add(TimeUnit.valueOf(s)); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectHashSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectHashSet.java index 9e1e0e52659d..6101a7d9bd9f 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectHashSet.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/collect/beforeCollectHashSet.java @@ -9,7 +9,7 @@ public class Collect { } void collectNames(List persons){ - Set names = new HashSet<>(); + Set names = new HashSet/*valuable comment*/<>(); for (Person person : persons) { names.add(person.getName()); }