diff --git a/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java b/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java index e4fcd0881ae7..f4a3fca563de 100644 --- a/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java @@ -180,7 +180,13 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal }; } - public static PsiElement simplifyStreamExpressions(PsiElement element) { + /** + * Simplify any stream expressions encountered within given element + * @param element element to process + * @param keepStream if true, no simplification which changes stream to non-stream will be performed + * @return the resulting element (may differ from the passed one if it was completely replaced) + */ + public static PsiElement simplifyStreamExpressions(PsiElement element, boolean keepStream) { boolean replaced = true; while(replaced) { replaced = false; @@ -189,6 +195,7 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal .select(PsiMethodCallExpression.class) .mapToEntry(CALL_TO_FIX_MAPPER::mapFirst) .nonNullValues() + .chain(s -> keepStream ? s.filterValues(CallChainSimplification::keepsStream) : s) .toCustomMap(LinkedHashMap::new); for (Map.Entry entry : callToSimplification.entrySet()) { if(entry.getKey().isValid()) { @@ -227,6 +234,10 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal interface CallChainSimplification extends CallChainFix { String getMessage(); + default boolean keepsStream() { + return true; + } + default void applyFix(@NotNull Project project, PsiElement element) { PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class, false); if (call != null) { @@ -422,6 +433,11 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal myChangeSemantics = changeSemantics; } + @Override + public boolean keepsStream() { + return false; + } + @Nls @NotNull @Override @@ -805,6 +821,11 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal OPTIONAL, FUNCTION, NEGATED_FUNCTION } + @Override + public boolean keepsStream() { + return false; + } + public SimpleStreamOfFix(ReplacementMode mode) { myMode = mode; } @@ -969,6 +990,11 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal return "Replace 'stream().toArray()' with 'toArray()'"; } + @Override + public boolean keepsStream() { + return false; + } + @Override public String getMessage() { return "Can be replaced with collection.toArray()"; @@ -1606,6 +1632,11 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal return "Can be replaced with Arrays.asList().contains()"; } + @Override + public boolean keepsStream() { + return false; + } + @Override public PsiElement simplify(PsiMethodCallExpression call) { PsiExpression value = myValuePointer.getElement(); @@ -1669,6 +1700,11 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal return "Replace with 'containsAll'"; } + @Override + public boolean keepsStream() { + return false; + } + @Override public PsiElement simplify(PsiMethodCallExpression call) { PsiExpression left = extractLeft(call); @@ -1736,6 +1772,11 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal return "Replace with 'String.join'"; } + @Override + public boolean keepsStream() { + return false; + } + @Override public String getMessage() { return "Can be replaced with 'String.join'"; diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/FoldExpressionIntoStreamInspection.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/FoldExpressionIntoStreamInspection.java index b7d5276a7094..7dab9e0d2f29 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/FoldExpressionIntoStreamInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/FoldExpressionIntoStreamInspection.java @@ -220,7 +220,7 @@ public class FoldExpressionIntoStreamInspection extends AbstractBaseJavaLocalIns private static void cleanup(PsiElement result) { JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(result.getProject()); - result = SimplifyStreamApiCallChainsInspection.simplifyStreamExpressions(result); + result = SimplifyStreamApiCallChainsInspection.simplifyStreamExpressions(result, false); LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); result = codeStyleManager.shortenClassReferences(result); PsiDiamondTypeUtil.removeRedundantTypeArguments(result); diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/MigrateToStreamFix.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/MigrateToStreamFix.java index d26b60dacf91..9ee92fa4c51e 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/MigrateToStreamFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/MigrateToStreamFix.java @@ -71,7 +71,7 @@ class MigrateToStreamFix implements LocalQuickFix { if (result == null) return; LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); PsiDiamondTypeUtil.removeRedundantTypeArguments(result); - result = SimplifyStreamApiCallChainsInspection.simplifyStreamExpressions(result); + result = SimplifyStreamApiCallChainsInspection.simplifyStreamExpressions(result, true); CodeStyleManager.getInstance(project).reformat(JavaCodeStyleManager.getInstance(project).shortenClassReferences(result)); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/simplifyForEach/afterForEachExpressionLambdaToArray.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/simplifyForEach/afterForEachExpressionLambdaToArray.java index 4b0ef9561e0c..c399defdbc64 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/simplifyForEach/afterForEachExpressionLambdaToArray.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/simplifyForEach/afterForEachExpressionLambdaToArray.java @@ -4,6 +4,6 @@ import java.util.*; public class Main { private void test(List strs) { - String[] arr = strs.toArray(); + String[] arr = strs.stream().toArray(); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAllActions/afterToArrayCountedLengthGeneric.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAllActions/afterToArrayCountedLengthGeneric.java index 86be38554ca3..f139523334bd 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAllActions/afterToArrayCountedLengthGeneric.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterAllActions/afterToArrayCountedLengthGeneric.java @@ -5,7 +5,7 @@ import java.util.List; public class Test { public void test(List> list) { - List[] arr = list.toArray(new List[0]); + List[] arr = list.stream().toArray(List[]::new); System.out.println(Arrays.toString(arr)); } }