mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 09:19:13 +07:00
Java inspection: Simplify stream API call chains - error handling added, inspection description improved, tests added (IDEA-140724, IDEA-156324)
This commit is contained in:
+14
-6
@@ -22,6 +22,7 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -31,12 +32,12 @@ import org.jetbrains.annotations.Nullable;
|
||||
* @author Pavel.Dolgov
|
||||
*/
|
||||
public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
public static final Logger LOG = Logger.getInstance("#" + SimplifyStreamApiCallChainsInspection.class.getName());
|
||||
private static final Logger LOG = Logger.getInstance("#" + SimplifyStreamApiCallChainsInspection.class.getName());
|
||||
|
||||
public static final String FOR_EACH_METHOD = "forEach";
|
||||
public static final String STREAM_METHOD = "stream";
|
||||
public static final String AS_LIST_METHOD = "asList";
|
||||
public static final String OF_METHOD = "of";
|
||||
private static final String FOR_EACH_METHOD = "forEach";
|
||||
private static final String STREAM_METHOD = "stream";
|
||||
private static final String AS_LIST_METHOD = "asList";
|
||||
private static final String OF_METHOD = "of";
|
||||
|
||||
@Override
|
||||
public boolean isEnabledByDefault() {
|
||||
@@ -134,7 +135,14 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns
|
||||
|
||||
if (qualifierExpression != null) {
|
||||
final String text = createExpressionText(expression, previousExpression, qualifierExpression);
|
||||
final PsiExpression newElement = JavaPsiFacade.getElementFactory(project).createExpressionFromText(text, null);
|
||||
final PsiExpression newElement;
|
||||
try {
|
||||
newElement = JavaPsiFacade.getElementFactory(project).createExpressionFromText(text, null);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.info("Failed to parse expression '" + text + "'", e);
|
||||
return;
|
||||
}
|
||||
final PsiElement shortenedElement = JavaCodeStyleManager.getInstance(project).shortenClassReferences(newElement);
|
||||
element.replace(shortenedElement);
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace Arrays.asList().stream() with Stream.of()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class AsListIncompleteArgsStream {
|
||||
Stream<String> abc() {
|
||||
return Stream.of("a", , );
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace Arrays.asList().stream() with Stream.of()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class AsListIncompleteArgsStream {
|
||||
Stream<String> abc() {
|
||||
return Ar<caret>rays.asList("a", , ).stream();
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace Arrays.asList().stream() with Arrays.stream()" "false"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
class AsListParallelStream {
|
||||
String max(String[] args) {
|
||||
return Arrays.asL<caret>ist(args).parallelStream().max(String::compareTo);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports redundant stream API call chains.
|
||||
This inspection reports stream API call chains which can be simplified.
|
||||
It allows to avoid creating redundant temporary objects without changing semantics.
|
||||
<br>
|
||||
For example, Collection.stream().forEach() can be replaced with Collection.forEach()
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user