[java-intentions] IDEA-299327 'Merge filter chain' quick-fix produces uncompilable code when lambda contains return

GitOrigin-RevId: 1e8d17520a03629891549385727b62e0bd342500
This commit is contained in:
Tagir Valeev
2022-08-04 17:47:30 +02:00
committed by intellij-monorepo-bot
parent c01bbb5413
commit 41d70528b5
3 changed files with 44 additions and 11 deletions

View File

@@ -147,21 +147,19 @@ public class MergeFilterChainAction extends PsiElementBaseIntentionAction {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
PsiElement nameElement = callToStay.getMethodExpression().getReferenceNameElement();
LOG.assertTrue(nameElement != null);
if(!resultingOperation.equals(nameElement.getText())) {
if (!resultingOperation.equals(nameElement.getText())) {
nameElement.replace(factory.createIdentifier(resultingOperation));
}
PsiElement targetBody = targetLambda.getBody();
LOG.assertTrue(targetBody instanceof PsiExpression);
final PsiElement sourceLambdaBody = sourceLambda.getBody();
PsiExpression targetBody = LambdaUtil.extractSingleExpressionFromBody(targetLambda.getBody());
LOG.assertTrue(targetBody != null);
final PsiExpression sourceLambdaBody = LambdaUtil.extractSingleExpressionFromBody(sourceLambda.getBody());
LOG.assertTrue(sourceLambdaBody != null);
LOG.assertTrue(sourceLambdaBody instanceof PsiExpression);
final PsiExpression compoundExpression = factory
.createExpressionFromText(
ParenthesesUtils.getText((PsiExpression)targetBody, ParenthesesUtils.OR_PRECEDENCE) + " && " +
ParenthesesUtils.getText((PsiExpression)sourceLambdaBody, ParenthesesUtils.OR_PRECEDENCE), sourceLambda);
targetBody = targetBody.replace(compoundExpression);
String newFilter = ParenthesesUtils.getText(targetBody, ParenthesesUtils.OR_PRECEDENCE) + " && " +
ParenthesesUtils.getText(sourceLambdaBody, ParenthesesUtils.OR_PRECEDENCE);
final PsiExpression compoundExpression = factory.createExpressionFromText(newFilter, sourceLambda);
targetBody = (PsiExpression)targetBody.replace(compoundExpression);
CodeStyleManager.getInstance(project).reformat(targetBody);
final PsiExpression qualifierExpression = callToEliminate.getMethodExpression().getQualifierExpression();

View File

@@ -0,0 +1,16 @@
// "Merge filter chain" "true-preview"
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class StreamFilter {
List<Integer> calc(Stream<Integer> stream) {
return stream
.filter(chain -> chain.doubleValue() > 12 && shouldReport(chain))
.collect(Collectors.toList());
}
private boolean shouldReport(Integer chain1) {
return false;
}
}

View File

@@ -0,0 +1,19 @@
// "Merge filter chain" "true-preview"
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class StreamFilter {
List<Integer> calc(Stream<Integer> stream) {
return stream
.<caret>filter(chain -> chain.doubleValue() > 12)
.filter(chain1 -> {
return shouldReport(chain1);
})
.collect(Collectors.toList());
}
private boolean shouldReport(Integer chain1) {
return false;
}
}