StreamApiMigrationInspection: use toList()/toSet() collector if target variable type is Collection

This commit is contained in:
Tagir Valeev
2016-09-06 11:56:13 +07:00
parent 3a3c613446
commit f0276b724d
3 changed files with 34 additions and 2 deletions
@@ -531,12 +531,12 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
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)) {
(rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_LIST) || rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_COLLECTION))) {
return "toList()";
}
else if (rawType != null && rawVarType != null &&
rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET) &&
rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_SET)) {
(rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_SET) || rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_COLLECTION))) {
return "toSet()";
}
else if (rawType != null) {
@@ -0,0 +1,15 @@
// "Replace with collect" "true"
import java.util.*;
import java.util.stream.Collectors;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Collection<String> names = persons.stream().map(Person::getName).collect(Collectors.toList());
}
}
@@ -0,0 +1,17 @@
// "Replace with collect" "true"
import java.util.*;
public class Collect {
class Person {
String getName() {
return "";
}
}
void collectNames(List<Person> persons){
Collection<String> names = new ArrayList<>();
for (Person person : pers<caret>ons) {
names.add(person.getName());
}
}
}