mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
StreamApiMigrationInspection: correctly handle anonymous classes/lambdas (IDEA-CR-14138)
This commit is contained in:
+4
-1
@@ -409,12 +409,15 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
}
|
||||
|
||||
static InitializerUsageStatus getInitializerUsageStatus(PsiVariable var, PsiStatement nextStatement) {
|
||||
if(var.getInitializer() == null) return UNKNOWN;
|
||||
if(!(var instanceof PsiLocalVariable) || var.getInitializer() == null) return UNKNOWN;
|
||||
if(isDeclarationJustBefore(var, nextStatement)) return DECLARED_JUST_BEFORE;
|
||||
PsiElement declaration = var.getParent();
|
||||
// Check if variable is not referenced in the same declaration like "int a = 0, b = a;"
|
||||
if(!PsiTreeUtil.processElements(declaration, e -> !(e instanceof PsiReferenceExpression) ||
|
||||
((PsiReferenceExpression)e).resolve() != var)) return UNKNOWN;
|
||||
// Check that variable is declared in the same method or the same lambda expression
|
||||
if(PsiTreeUtil.getParentOfType(var, PsiLambdaExpression.class, PsiMethod.class) !=
|
||||
PsiTreeUtil.getParentOfType(nextStatement, PsiLambdaExpression.class, PsiMethod.class)) return UNKNOWN;
|
||||
PsiElement block = PsiUtil.getVariableCodeBlock(var, null);
|
||||
if(block == null) return UNKNOWN;
|
||||
final ControlFlow controlFlow;
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Collect {
|
||||
public static void collectWithLambda(List<String> test) {
|
||||
Runnable r = () -> {
|
||||
List<String> result;
|
||||
System.out.println("We're inside the lambda");
|
||||
result = test.stream().map(String::trim).collect(Collectors.toList());
|
||||
System.out.println(result);
|
||||
};
|
||||
r.run();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// "Replace with collect" "false"
|
||||
import java.util.*;
|
||||
|
||||
public class Collect {
|
||||
public static void collectWithAnonymous(List<String> test) {
|
||||
List<String> result = new ArrayList<>();
|
||||
Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (String str : te<caret>st) {
|
||||
result.add(str.trim());
|
||||
}
|
||||
}
|
||||
};
|
||||
r.run();
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace with collect" "false"
|
||||
import java.util.*;
|
||||
|
||||
public class Collect {
|
||||
public static void collectWithLambda(List<String> test) {
|
||||
List<String> result = new ArrayList<>();
|
||||
Runnable r = () -> {
|
||||
for(String str : te<caret>st) {
|
||||
result.add(str.trim());
|
||||
}
|
||||
};
|
||||
r.run();
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Collect {
|
||||
public static void collectWithLambda(List<String> test) {
|
||||
Runnable r = () -> {
|
||||
List<String> result = new ArrayList<>();
|
||||
System.out.println("We're inside the lambda");
|
||||
for(String str : te<caret>st) {
|
||||
result.add(str.trim());
|
||||
}
|
||||
System.out.println(result);
|
||||
};
|
||||
r.run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user