mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
StreamApiMigrationInspection: fix noneMatch()/allMatch() quick-fix name when continue is used; prettier Optional code generated in some cases
This commit is contained in:
+12
@@ -19,6 +19,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.streamMigration.StreamApiMigrationInspection.Operation;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.siyeh.ig.psiutils.EquivalenceChecker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -103,6 +104,17 @@ class ReplaceWithFindFirstFix extends MigrateToStreamFix {
|
||||
return "(" + castType.getText() + ")" + stream + ".orElse(null)";
|
||||
}
|
||||
}
|
||||
if(StreamApiMigrationInspection.isLiteral(falseExpression, Boolean.FALSE) && PsiType.BOOLEAN.equals(trueExpression.getType())) {
|
||||
return stream + ".filter(" + StreamApiMigrationInspection.createLambda(var, trueExpression) + ").isPresent()";
|
||||
}
|
||||
if(trueExpression instanceof PsiConditionalExpression) {
|
||||
PsiConditionalExpression condition = (PsiConditionalExpression)trueExpression;
|
||||
if(EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(falseExpression, condition.getElseExpression())) {
|
||||
return generateOptionalUnwrap(
|
||||
stream + ".filter(" + StreamApiMigrationInspection.createLambda(var, condition.getCondition()) + ")", tb,
|
||||
condition.getThenExpression(), falseExpression);
|
||||
}
|
||||
}
|
||||
stream += ".map(" + StreamApiMigrationInspection.createLambda(var, trueExpression) + ")";
|
||||
}
|
||||
stream += ".orElse(" + falseExpression.getText() + ")";
|
||||
|
||||
+5
-1
@@ -561,7 +561,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
methodName = "noneMatch";
|
||||
if(!operations.isEmpty()) {
|
||||
Operation lastOp = operations.get(operations.size() - 1);
|
||||
if(lastOp instanceof FilterOp && BoolUtils.isNegation(lastOp.getExpression())) {
|
||||
if(lastOp instanceof FilterOp && (((FilterOp)lastOp).isNegated() ^ BoolUtils.isNegation(lastOp.getExpression()))) {
|
||||
methodName = "allMatch";
|
||||
}
|
||||
}
|
||||
@@ -644,6 +644,10 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
myNegated = negated;
|
||||
}
|
||||
|
||||
public boolean isNegated() {
|
||||
return myNegated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
PsiExpression expression =
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace with allMatch()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
boolean find(String[][] data) {
|
||||
return Arrays.stream(data).flatMap(Arrays::stream).allMatch(str -> str.startsWith("xyz"));
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Replace with findFirst()" "true"
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static boolean find(List<List<String>> list) {
|
||||
/*
|
||||
Block comment
|
||||
*/
|
||||
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().filter(string -> string.substring(3).equals("xyz")).isPresent();
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with findFirst()" "true"
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static String find(List<List<String>> list) {
|
||||
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().filter(string -> string.substring(3).equals("xyz")).map(String::trim).orElse(null);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace with noneMatch()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
boolean find(String[][] data) {
|
||||
return Arrays.stream(data).flatMap(Arrays::stream).noneMatch(str -> str.startsWith("xyz"));
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Replace with allMatch()" "true"
|
||||
|
||||
public class Main {
|
||||
boolean find(String[][] data) {
|
||||
for(String[] arr : da<caret>ta) {
|
||||
for(String str : arr) {
|
||||
if(str.startsWith("xyz")) continue;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace with findFirst()" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static boolean find(List<List<String>> list) {
|
||||
for(List<String> innerList : li<caret>st) {
|
||||
for(String string : innerList) {
|
||||
if(string.startsWith("ABC")) {
|
||||
return string.substring(3).equals("xyz");
|
||||
/*
|
||||
Block comment
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace with findFirst()" "true"
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static String find(List<List<String>> list) {
|
||||
for(List<String> innerList : li<caret>st) {
|
||||
for(String string : innerList) {
|
||||
if(string.startsWith("ABC")) {
|
||||
return string.substring(3).equals("xyz") ? string.trim() : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Replace with noneMatch()" "true"
|
||||
|
||||
public class Main {
|
||||
boolean find(String[][] data) {
|
||||
for(String[] arr : da<caret>ta) {
|
||||
for(String str : arr) {
|
||||
if(!str.startsWith("xyz")) continue;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user