Stream API Migration: fix cases when boxed collection is iterated with primitive parameter (inspired by PR https://github.com/JetBrains/intellij-community/pull/455 by FHannes)

This commit is contained in:
Tagir Valeev
2016-10-24 17:44:45 +07:00
parent 79184bf1f4
commit d51f09d0d5
10 changed files with 137 additions and 2 deletions
@@ -522,6 +522,19 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
return false;
}
static String tryUnbox(PsiVariable variable) {
PsiType type = variable.getType();
String mapOp = null;
if(type.equals(PsiType.INT)) {
mapOp = "mapToInt";
} else if(type.equals(PsiType.LONG)) {
mapOp = "mapToLong";
} else if(type.equals(PsiType.DOUBLE)) {
mapOp = "mapToDouble";
}
return mapOp == null ? "" : "."+mapOp+"("+variable.getName()+" -> "+variable.getName()+")";
}
private class StreamApiMigrationVisitor extends JavaElementVisitor {
private final ProblemsHolder myHolder;
private final boolean myIsOnTheFly;
@@ -1155,7 +1168,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
@Override
String createReplacement() {
return ParenthesesUtils.getText(myExpression, ParenthesesUtils.POSTFIX_PRECEDENCE) + ".stream()";
return ParenthesesUtils.getText(myExpression, ParenthesesUtils.POSTFIX_PRECEDENCE) + ".stream()" + tryUnbox(myVariable);
}
@Contract("null, _ -> false")
@@ -1175,7 +1188,8 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
PsiClass iteratorClass = PsiUtil.resolveClassInClassTypeOnly(iteratedValueType);
if (collectionClass == null ||
!InheritanceUtil.isInheritorOrSelf(iteratorClass, collectionClass, true) ||
isRawSubstitution(iteratedValueType, collectionClass)) {
isRawSubstitution(iteratedValueType, collectionClass) ||
!isSupported(statement.getIterationParameter().getType())) {
return null;
}
return new CollectionStream(statement.getIterationParameter(), iteratedValue);
@@ -0,0 +1,9 @@
import java.util.Arrays;
// "Replace with sum()" "true"
public class Main {
public int sum(int[] array) {
int sum = Arrays.stream(array).sum();
return sum;
}
}
@@ -0,0 +1,22 @@
// "Replace with sum()" "true"
import java.util.Arrays;
import java.util.List;
public class Main {
public boolean check(Integer x) {
return x % 3 == 0;
}
public boolean check(int x) {
return x % 2 == 0;
}
public int sum(List<Integer> list) {
int sum = list.stream().mapToInt(x -> x).filter(this::check).sum();
return sum;
}
public static void main(String[] args) {
System.out.println(new Main().sum(Arrays.asList(1,2,3,4,5,6)));
}
}
@@ -0,0 +1,9 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public int sum(List<Integer> list) {
int sum = list.stream().mapToInt(x -> x).sum();
return sum;
}
}
@@ -0,0 +1,9 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public int sum(List<Integer> list) {
int sum = list.stream().mapToInt(x -> x).sum();
return sum;
}
}
@@ -0,0 +1,12 @@
// "Replace with sum()" "false"
import java.util.List;
public class Main {
public int sum(List<Float> list) {
int sum = 0;
for(float x : li<caret>st) {
sum += x;
}
return sum;
}
}
@@ -0,0 +1,10 @@
// "Replace with sum()" "true"
public class Main {
public int sum(int[] array) {
int sum = 0;
for(int x : arr<caret>ay) {
sum += x;
}
return sum;
}
}
@@ -0,0 +1,26 @@
// "Replace with sum()" "true"
import java.util.Arrays;
import java.util.List;
public class Main {
public boolean check(Integer x) {
return x % 3 == 0;
}
public boolean check(int x) {
return x % 2 == 0;
}
public int sum(List<Integer> list) {
int sum = 0;
for(int x : li<caret>st) {
if(check(x))
sum += x;
}
return sum;
}
public static void main(String[] args) {
System.out.println(new Main().sum(Arrays.asList(1,2,3,4,5,6)));
}
}
@@ -0,0 +1,12 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public int sum(List<Integer> list) {
int sum = 0;
for(int x : li<caret>st) {
sum += x;
}
return sum;
}
}
@@ -0,0 +1,12 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public int sum(List<Integer> list) {
int sum = 0;
for(Integer x : li<caret>st) {
sum += x;
}
return sum;
}
}