mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
StreamApiMigrationInspection: suggest to use takeWhile() in Java-9
This commit is contained in:
+18
-1
@@ -764,7 +764,12 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
PsiExpression intermediate = makeIntermediateExpression(factory);
|
||||
PsiExpression expression =
|
||||
myNegated ? factory.createExpressionFromText(BoolUtils.getNegatedExpressionText(intermediate), myExpression) : intermediate;
|
||||
return ".filter(" + LambdaUtil.createLambda(myVariable, expression) + ")";
|
||||
return "." + getOpName() + "(" + LambdaUtil.createLambda(myVariable, expression) + ")";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
String getOpName() {
|
||||
return "filter";
|
||||
}
|
||||
|
||||
PsiExpression makeIntermediateExpression(PsiElementFactory factory) {
|
||||
@@ -772,6 +777,18 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
}
|
||||
}
|
||||
|
||||
static class TakeWhileOp extends FilterOp {
|
||||
TakeWhileOp(PsiExpression condition, PsiVariable variable, boolean negated) {
|
||||
super(condition, variable, negated);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
String getOpName() {
|
||||
return "takeWhile";
|
||||
}
|
||||
}
|
||||
|
||||
static class CompoundFilterOp extends FilterOp {
|
||||
private final FlatMapOp myFlatMapOp;
|
||||
private final PsiVariable myMatchVariable;
|
||||
|
||||
@@ -186,6 +186,18 @@ class TerminalBlock {
|
||||
}
|
||||
if(myStatements.length >= 1) {
|
||||
PsiStatement first = myStatements[0];
|
||||
if(PsiUtil.isLanguageLevel9OrHigher(first.getContainingFile()) && first instanceof PsiIfStatement) {
|
||||
PsiIfStatement ifStatement = (PsiIfStatement)first;
|
||||
PsiExpression condition = ifStatement.getCondition();
|
||||
if(ifStatement.getElseBranch() == null && condition != null) {
|
||||
PsiStatement thenStatement = ControlFlowUtils.stripBraces(ifStatement.getThenBranch());
|
||||
if(ControlFlowUtils.statementBreaksLoop(thenStatement, getMainLoop())) {
|
||||
TakeWhileOp op = new TakeWhileOp(condition, myVariable, true);
|
||||
PsiStatement[] leftOver = Arrays.copyOfRange(myStatements, 1, myStatements.length);
|
||||
return new TerminalBlock(this, op, myVariable, leftOver);
|
||||
}
|
||||
}
|
||||
}
|
||||
// extract map
|
||||
if(first instanceof PsiDeclarationStatement) {
|
||||
PsiDeclarationStatement decl = (PsiDeclarationStatement)first;
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Replace with collect" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
public static void test(List<String> data) {
|
||||
List<String> result = data.stream().takeWhile(s -> !s.isEmpty()).collect(Collectors.toList());
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace with collect" "false"
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Test {
|
||||
public static void test(List<String> data) {
|
||||
List<String> result = new ArrayList<>();
|
||||
for(String s : da<caret>ta) {
|
||||
if(s.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
result.add(s);
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace with collect" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Test {
|
||||
public static void test(List<String> data) {
|
||||
List<String> result = new ArrayList<>();
|
||||
for(String s : da<caret>ta) {
|
||||
if(s.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
result.add(s);
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.streamMigration.StreamApiMigrationInspection;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
@@ -31,6 +32,11 @@ public class StreamApiMigrationInspectionTest extends LightQuickFixParameterized
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LanguageLevel getLanguageLevel() {
|
||||
return getTestName(false).contains("Java9") ? LanguageLevel.JDK_1_9 : LanguageLevel.JDK_1_8;
|
||||
}
|
||||
|
||||
public void test() throws Exception { doAllTests(); }
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user