convert to streams: convert lambda to constructor reference; don't accept list when arrayList expected

This commit is contained in:
Anna Kozlova
2015-06-09 20:31:02 +03:00
parent 46173543d5
commit 155f283f0e
4 changed files with 42 additions and 6 deletions
@@ -387,7 +387,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
final PsiExpressionList argumentList = ((PsiNewExpression)initializer).getArgumentList();
if (argumentList != null && argumentList.getExpressions().length == 0) {
restoreComments(foreachStatement, body);
final String callText = builder.toString() + createInitializerReplacementText(initializer) + ")";
final String callText = builder.toString() + createInitializerReplacementText(((PsiVariable)resolve).getType(), initializer) + ")";
result = initializer.replace(elementFactory.createExpressionFromText(callText, null));
simplifyRedundantCast(result);
foreachStatement.delete();
@@ -412,14 +412,24 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
}
}
private static String createInitializerReplacementText(PsiExpression initializer) {
private static String createInitializerReplacementText(PsiType varType, PsiExpression initializer) {
final PsiType initializerType = initializer.getType();
final PsiClassType rawType = initializerType instanceof PsiClassType ? ((PsiClassType)initializerType).rawType() : null;
if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST)) {
final PsiClassType rawVarType = varType instanceof PsiClassType ? ((PsiClassType)varType).rawType() : null;
if (rawType != null && rawVarType != null &&
rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST) &&
rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_LIST)) {
return "toList()";
} else if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET)) {
}
else if (rawType != null && rawVarType != null &&
rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET) &&
rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_SET)) {
return "toSet()";
} else {
}
else if (rawType != null) {
return "toCollection(" + rawType.getClassName() + "::new)";
}
else {
return "toCollection(() -> " + initializer.getText() +")";
}
}
@@ -0,0 +1,12 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
class A {
public static void main(List<String> args) {
ArrayList<String> uniqNames = args.stream().map(name -> name.substring(1)).collect(Collectors.toCollection(ArrayList::new));
uniqNames.forEach(System.out::println);
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
Set<String> names = persons.stream().map(Person::getName).collect(Collectors.toCollection(() -> new LinkedHashSet<>()));
Set<String> names = persons.stream().map(Person::getName).collect(Collectors.toCollection(LinkedHashSet::new));
}
}
@@ -0,0 +1,14 @@
// "Replace with collect" "true"
import java.util.*;
class A {
public static void main(List<String> args) {
ArrayList<String> uniqNames = new ArrayList<>();
for (String name : ar<caret>gs){
uniqNames.add(name.substring(1));
}
uniqNames.forEach(System.out::println);
}
}