FuseStreamOperationsInspection: fixes according to review IDEA-CR-24873

1. Code sample in description
2. Supported toCollection with parameterized method-reference
This commit is contained in:
Tagir Valeev
2017-09-27 10:50:03 +07:00
parent f065fa4778
commit 1779a8a871
4 changed files with 37 additions and 4 deletions

View File

@@ -69,9 +69,9 @@ public class FuseStreamOperationsInspection extends BaseJavaBatchLocalInspection
private static PsiClass resolveClassCreatedByFunction(PsiExpression function) {
function = PsiUtil.skipParenthesizedExprDown(function);
if (function instanceof PsiMethodReferenceExpression && ((PsiMethodReferenceExpression)function).isConstructor()) {
PsiExpression qualifier = ((PsiMethodReferenceExpression)function).getQualifierExpression();
if (qualifier instanceof PsiReferenceExpression) {
return tryCast(((PsiReferenceExpression)qualifier).resolve(), PsiClass.class);
PsiMethod constructor = tryCast(((PsiMethodReferenceExpression)function).resolve(), PsiMethod.class);
if (constructor != null) {
return constructor.getContainingClass();
}
}
if (function instanceof PsiLambdaExpression) {

View File

@@ -0,0 +1,12 @@
// "Fuse ArrayList into the Stream API chain" "true"
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
void test(Stream<String> stream) {
List<Object> objects = stream.distinct().collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,12 @@
// "Fuse ArrayList into the Stream API chain" "true"
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
void test(Stream<String> stream) {
List<Object> objects = new ArrayList<>(stream.co<caret>llect(Collectors.toCollection(LinkedHashSet<Object>::new)));
}
}

View File

@@ -1,6 +1,15 @@
<html>
<body>
Detects when some transformations are performed on Stream API result which could be incorporated into the Stream API call chain directly.
Detects when some transformations are performed on Stream API result which could be incorporated into the Stream API call chain directly. E.g.:
<pre>
List&lt;String&gt; list = stream.collect(Collectors.toList());
list.sort(null);
return list.toArray(new String[list.size()]);
</pre>
Could be converted to
<pre>
return stream.sorted().toArray(String[]::new);
</pre>
<!-- tooltip end -->
<p><small>New in 2017.3</small></p>
</body>