mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
stream api: do not collapse loops when body is not throws compatible (IDEA-125541)
This commit is contained in:
+21
-2
@@ -15,10 +15,12 @@
|
||||
*/
|
||||
package com.intellij.codeInspection;
|
||||
|
||||
import com.intellij.codeInsight.ExceptionUtil;
|
||||
import com.intellij.codeInsight.daemon.GroupNames;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
@@ -27,6 +29,7 @@ import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.IntArrayList;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -160,7 +163,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
if (args.length == 1) {
|
||||
if (args[0] instanceof PsiCallExpression) {
|
||||
final PsiMethod method = ((PsiCallExpression)args[0]).resolveMethod();
|
||||
return method != null && !method.hasTypeParameters();
|
||||
return method != null && !method.hasTypeParameters() && !isThrowsCompatible(method);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -222,7 +225,23 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
return false;
|
||||
}
|
||||
//method reference
|
||||
return LambdaCanBeMethodReferenceInspection.canBeMethodReferenceProblem(body instanceof PsiBlockStatement ? ((PsiBlockStatement)body).getCodeBlock() : body, new PsiParameter[] {parameter}, null) == null;
|
||||
final PsiCallExpression callExpression = LambdaCanBeMethodReferenceInspection
|
||||
.canBeMethodReferenceProblem(body instanceof PsiBlockStatement ? ((PsiBlockStatement)body).getCodeBlock() : body,
|
||||
new PsiParameter[]{parameter}, null);
|
||||
if (callExpression == null) {
|
||||
return true;
|
||||
}
|
||||
final PsiMethod method = callExpression.resolveMethod();
|
||||
return method != null && isThrowsCompatible(method);
|
||||
}
|
||||
|
||||
private static boolean isThrowsCompatible(PsiMethod method) {
|
||||
return ContainerUtil.find(method.getThrowsList().getReferencedTypes(), new Condition<PsiClassType>() {
|
||||
@Override
|
||||
public boolean value(PsiClassType type) {
|
||||
return !ExceptionUtil.isUncheckedException(type);
|
||||
}
|
||||
}) != null;
|
||||
}
|
||||
|
||||
private static class ReplaceWithForeachCallFix implements LocalQuickFix {
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace with collect" "false"
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ForEachTest {
|
||||
|
||||
interface A {
|
||||
String ii() throws IOException;
|
||||
}
|
||||
private List<A> reqs;
|
||||
|
||||
public ForEachTest () throws IOException {
|
||||
List<String> result = new ArrayList<>();
|
||||
for(A val : re<caret>qs) {
|
||||
result.add(val.ii());
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace with forEach" "false"
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
class Test {
|
||||
|
||||
private List<byte[]> reqs;
|
||||
|
||||
public ForEachTest () throws IOException {
|
||||
DataOutputStream req = new DataOutputStream(new ByteArrayOutputStream());
|
||||
for(byte[] val : r<caret>eqs) {
|
||||
req.write(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user