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);