mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
stream migration inspection: collapse only primitive new initializers (IDEA-121745)
This commit is contained in:
+12
-6
@@ -294,13 +294,19 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
iteration += ").collect(java.util.stream.Collectors.";
|
||||
|
||||
String variableName = null;
|
||||
PsiExpression initializer = null;
|
||||
PsiExpression primitiveInitializer = null;
|
||||
final PsiExpression qualifierExpression = methodCallExpression.getMethodExpression().getQualifierExpression();
|
||||
if (qualifierExpression instanceof PsiReferenceExpression) {
|
||||
final PsiElement resolve = ((PsiReferenceExpression)qualifierExpression).resolve();
|
||||
if (resolve instanceof PsiVariable) {
|
||||
if (resolve instanceof PsiLocalVariable && foreachStatement.equals(PsiTreeUtil.skipSiblingsForward(resolve.getParent(), PsiWhiteSpace.class))) {
|
||||
initializer = ((PsiVariable)resolve).getInitializer();
|
||||
final PsiExpression initializer = ((PsiVariable)resolve).getInitializer();
|
||||
if (initializer instanceof PsiNewExpression) {
|
||||
final PsiExpressionList argumentList = ((PsiNewExpression)initializer).getArgumentList();
|
||||
if (argumentList != null && argumentList.getExpressions().length == 0) {
|
||||
primitiveInitializer = initializer;
|
||||
}
|
||||
}
|
||||
}
|
||||
variableName = ((PsiVariable)resolve).getName() + ".";
|
||||
}
|
||||
@@ -309,18 +315,18 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
}
|
||||
|
||||
PsiElement result = null;
|
||||
if (initializer != null) {
|
||||
final PsiType initializerType = initializer.getType();
|
||||
if (primitiveInitializer != null) {
|
||||
final PsiType initializerType = primitiveInitializer.getType();
|
||||
final PsiClassType rawType = initializerType instanceof PsiClassType ? ((PsiClassType)initializerType).rawType() : null;
|
||||
if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST)) {
|
||||
iteration += "toList()";
|
||||
} else if (rawType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET)) {
|
||||
iteration += "toSet()";
|
||||
} else {
|
||||
iteration += "toCollection(() -> " + initializer.getText() +")";
|
||||
iteration += "toCollection(() -> " + primitiveInitializer.getText() +")";
|
||||
}
|
||||
iteration += ")";
|
||||
result = initializer.replace(JavaPsiFacade.getElementFactory(project).createExpressionFromText(iteration, foreachStatement));
|
||||
result = primitiveInitializer.replace(JavaPsiFacade.getElementFactory(project).createExpressionFromText(iteration, foreachStatement));
|
||||
foreachStatement.delete();
|
||||
} else if (variableName != null){
|
||||
iteration += "toList())";
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Collect {
|
||||
class Person {
|
||||
String getName() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<String> foo() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<String> names = foo();
|
||||
names.addAll(persons.stream().map(Person::getName).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Collect {
|
||||
class Person {
|
||||
String getName() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<String> foo() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<String> names = foo();
|
||||
for (Person person : pers<caret>ons) {
|
||||
names.add(person.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user