diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java index 87daff2cbf90..d82f7586307c 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/StreamApiMigrationInspection.java @@ -932,7 +932,32 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo @Override String createReplacement() { - return "java.util.Arrays.stream("+myExpression.getText() + ")"; + if (myExpression instanceof PsiNewExpression) { + PsiArrayInitializerExpression initializer = ((PsiNewExpression)myExpression).getArrayInitializer(); + if (initializer != null) { + PsiElement[] children = initializer.getChildren(); + if (children.length > 2) { + String initializerText = StreamEx.of(children, 1, children.length - 1).map(PsiElement::getText).joining(); + PsiType type = myExpression.getType(); + if (type instanceof PsiArrayType) { + PsiType componentType = ((PsiArrayType)type).getComponentType(); + if (componentType.equals(PsiType.INT)) { + return CommonClassNames.JAVA_UTIL_STREAM_INT_STREAM + ".of(" + initializerText + ")"; + } + else if (componentType.equals(PsiType.LONG)) { + return CommonClassNames.JAVA_UTIL_STREAM_LONG_STREAM + ".of(" + initializerText + ")"; + } + else if (componentType.equals(PsiType.DOUBLE)) { + return CommonClassNames.JAVA_UTIL_STREAM_DOUBLE_STREAM + ".of(" + initializerText + ")"; + } + else if (componentType instanceof PsiClassType) { + return CommonClassNames.JAVA_UTIL_STREAM_STREAM + ".<" + componentType.getCanonicalText() + ">of(" + initializerText + ")"; + } + } + } + } + } + return CommonClassNames.JAVA_UTIL_ARRAYS + ".stream(" + myExpression.getText() + ")"; } @Nullable diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterExplicitArray.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterExplicitArray.java new file mode 100644 index 000000000000..e371093a8fa0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/afterExplicitArray.java @@ -0,0 +1,18 @@ +// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true" + +import java.util.stream.IntStream; +import java.util.stream.Stream; + +public class Test { + public String test(String other) { + return Stream.of("aaa", "bbb", "ccc", "ddd").filter(other::startsWith).findFirst().orElse(null); + } + + public CharSequence test2(String other) { + return Stream.of("aaa", "bbb", "ccc", "ddd").filter(s -> other.startsWith(s.toString())).findFirst().orElse(null); + } + + public int test(int other) { + return IntStream.of(2, 4, 8, 16, 32, 64, 128, 256, 512, 1024).filter(i -> i > other).findFirst().orElse(-1); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeExplicitArray.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeExplicitArray.java new file mode 100644 index 000000000000..d29ad11d82e7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/beforeExplicitArray.java @@ -0,0 +1,30 @@ +// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true" + +public class Test { + public String test(String other) { + for(String s : new String[] {"aaa", "bbb", "ccc", "ddd"}) { + if(other.startsWith(s)) { + return s; + } + } + return null; + } + + public CharSequence test2(String other) { + for(CharSequence s : new CharSequence[] {"aaa", "bbb", "ccc", "ddd"}) { + if(other.startsWith(s.toString())) { + return s; + } + } + return null; + } + + public int test(int other) { + for(int i : new int[] {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}) { + if(i > other) { + return i; + } + } + return -1; + } +}