mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
stream migration: collapse identity matching (IDEA-122706)
This commit is contained in:
+19
-16
@@ -331,24 +331,23 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
iteration += ".filter(" + parameter.getName() + " -> " + condition.getText() +")";
|
||||
}
|
||||
}
|
||||
iteration +=".map(";
|
||||
|
||||
final PsiExpression mapperCall = methodCallExpression.getArgumentList().getExpressions()[0];
|
||||
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
final PsiClass functionClass = psiFacade.findClass("java.util.function.Function", GlobalSearchScope.allScope(project));
|
||||
final PsiClassType functionalInterfaceType = functionClass != null ? psiFacade.getElementFactory().createType(functionClass, parameter.getType(), mapperCall.getType()) : null;
|
||||
final PsiCallExpression toConvertCall = LambdaCanBeMethodReferenceInspection.canBeMethodReferenceProblem(mapperCall,
|
||||
new PsiParameter[]{
|
||||
parameter},
|
||||
functionalInterfaceType);
|
||||
final String methodReferenceText = LambdaCanBeMethodReferenceInspection.createMethodReferenceText(toConvertCall, functionalInterfaceType, new PsiParameter[]{parameter});
|
||||
if (methodReferenceText != null) {
|
||||
iteration += methodReferenceText;
|
||||
} else {
|
||||
iteration += parameter.getName() + " -> " + mapperCall.getText();
|
||||
if (!isIdentityMapping(parameter, mapperCall)) {
|
||||
iteration +=".map(";
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
final PsiClass functionClass = psiFacade.findClass("java.util.function.Function", GlobalSearchScope.allScope(project));
|
||||
final PsiClassType functionalInterfaceType = functionClass != null ? psiFacade.getElementFactory().createType(functionClass, parameter.getType(), mapperCall.getType()) : null;
|
||||
final PsiCallExpression toConvertCall = LambdaCanBeMethodReferenceInspection.canBeMethodReferenceProblem(mapperCall, new PsiParameter[]{parameter}, functionalInterfaceType);
|
||||
final String methodReferenceText = LambdaCanBeMethodReferenceInspection.createMethodReferenceText(toConvertCall, functionalInterfaceType, new PsiParameter[]{parameter});
|
||||
if (methodReferenceText != null) {
|
||||
iteration += methodReferenceText;
|
||||
} else {
|
||||
iteration += parameter.getName() + " -> " + mapperCall.getText();
|
||||
}
|
||||
iteration += ")";
|
||||
}
|
||||
iteration += ").collect(java.util.stream.Collectors.";
|
||||
|
||||
iteration += ".collect(java.util.stream.Collectors.";
|
||||
|
||||
String variableName = null;
|
||||
PsiExpression primitiveInitializer = null;
|
||||
@@ -403,6 +402,10 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isIdentityMapping(PsiParameter parameter, PsiExpression mapperCall) {
|
||||
return mapperCall instanceof PsiReferenceExpression && ((PsiReferenceExpression)mapperCall).resolve() == parameter;
|
||||
}
|
||||
}
|
||||
|
||||
public static PsiIfStatement extractIfStatement(PsiStatement body) {
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Collect {
|
||||
class Person {}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<Person> names = persons.stream().collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Collect {
|
||||
class Person {}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<Person> names = new ArrayList<>();
|
||||
for (Person person : pers<caret>ons) {
|
||||
names.add(person);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user