diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/MatchMigration.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/MatchMigration.java index 22bbcfbe230e..a650df7e5ca6 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/MatchMigration.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/MatchMigration.java @@ -18,6 +18,8 @@ package com.intellij.codeInspection.streamMigration; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.psi.*; +import com.intellij.psi.util.PsiUtil; +import com.intellij.util.ObjectUtils; import com.siyeh.ig.psiutils.*; import com.siyeh.ig.psiutils.ControlFlowUtils.InitializerUsageStatus; import org.jetbrains.annotations.NotNull; @@ -35,9 +37,10 @@ class MatchMigration extends BaseStreamApiMigration { CommentTracker ct = new CommentTracker(); if(tb.getSingleStatement() instanceof PsiReturnStatement) { PsiReturnStatement returnStatement = (PsiReturnStatement)tb.getSingleStatement(); - PsiExpression value = returnStatement.getReturnValue(); - if (ExpressionUtils.isLiteral(value, Boolean.TRUE) || ExpressionUtils.isLiteral(value, Boolean.FALSE)) { - boolean foundResult = (boolean)((PsiLiteralExpression)value).getValue(); + PsiLiteralExpression literal = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(returnStatement.getReturnValue()), + PsiLiteralExpression.class); + if (literal != null && literal.getValue() instanceof Boolean) { + boolean foundResult = (Boolean)literal.getValue(); PsiReturnStatement nextReturnStatement = ControlFlowUtils.getNextReturnStatement(sourceStatement); if (nextReturnStatement != null) { PsiExpression returnValue = nextReturnStatement.getReturnValue(); @@ -98,7 +101,7 @@ class MatchMigration extends BaseStreamApiMigration { } } } - String replacement = "if(" + streamText + "){" + ct.text(statement) + "}"; + String replacement = "if(" + streamText + "){" + ct.text(statement) + "\n}"; return ct.replaceAndRestoreComments(sourceStatement, replacement); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/anyMatch/afterEndComment.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/anyMatch/afterEndComment.java new file mode 100644 index 000000000000..1d93e0bd235f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/anyMatch/afterEndComment.java @@ -0,0 +1,12 @@ +// "Replace with anyMatch()" "true" +import java.util.List; + +class X { + String test(List list) { + if (list.stream().map(String::trim).anyMatch(String::isEmpty)) { + return null; // Comment + } + System.out.println("hello"); + return "foo"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/anyMatch/beforeEndComment.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/anyMatch/beforeEndComment.java new file mode 100644 index 000000000000..ea3b68f10375 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/anyMatch/beforeEndComment.java @@ -0,0 +1,15 @@ +// "Replace with anyMatch()" "true" +import java.util.List; + +class X { + String test(List list) { + for (String s : list) { + String s1 = s.trim(); + if (s1.isEmpty()) { + return null; // Comment + } + } + System.out.println("hello"); + return "foo"; + } +} \ No newline at end of file