surround with try/catch: ignore nested functional expressions from unchecked analyzes

This commit is contained in:
Anna Kozlova
2014-12-18 14:13:22 +01:00
parent 6336e973c9
commit 6e761e661f
5 changed files with 43 additions and 7 deletions
@@ -41,12 +41,10 @@ public class SurroundWithTryCatchFix implements IntentionAction {
private PsiStatement myStatement = null;
public SurroundWithTryCatchFix(@NotNull PsiElement element) {
final PsiMethodReferenceExpression methodReferenceExpression = PsiTreeUtil.getParentOfType(element, PsiMethodReferenceExpression.class, false);
if (methodReferenceExpression == null) {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
if (lambdaExpression == null || lambdaExpression.getBody() instanceof PsiCodeBlock) {
myStatement = PsiTreeUtil.getNonStrictParentOfType(element, PsiStatement.class);
}
final PsiFunctionalExpression functionalExpression = PsiTreeUtil.getParentOfType(element, PsiFunctionalExpression.class, false);
if (functionalExpression == null ||
(functionalExpression instanceof PsiLambdaExpression && ((PsiLambdaExpression)functionalExpression).getBody() instanceof PsiCodeBlock)) {
myStatement = PsiTreeUtil.getNonStrictParentOfType(element, PsiStatement.class);
}
}
@@ -29,6 +29,7 @@ import com.intellij.psi.scope.MethodProcessorSetupFailedException;
import com.intellij.psi.scope.processor.MethodResolverProcessor;
import com.intellij.psi.scope.util.PsiScopesUtil;
import com.intellij.psi.util.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.NullableFunction;
import com.intellij.util.SmartList;
@@ -346,7 +347,7 @@ public class ExceptionUtil {
}
@NotNull
public static List<PsiClassType> getUnhandledExceptions(@NotNull PsiElement[] elements) {
public static List<PsiClassType> getUnhandledExceptions(final @NotNull PsiElement[] elements) {
final List<PsiClassType> array = ContainerUtil.newArrayList();
final PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() {
@Override
@@ -363,6 +364,7 @@ public class ExceptionUtil {
@Override
public void visitMethodReferenceExpression(@NotNull PsiMethodReferenceExpression expression) {
if (ArrayUtil.find(elements, expression) < 0) return;
addExceptions(array, getUnhandledExceptions(expression, null));
visitElement(expression);
}
@@ -0,0 +1,14 @@
class Test {
public static void main(String[] args){
<selection>I i = ExceptionTest::foo;</selection>
}
class Ex extends Exception {}
static void foo() throws Ex {}
interface I {
void f();
}
}
@@ -0,0 +1,18 @@
class Test {
public static void main(String[] args){
try {
I i = ExceptionTest::foo;
} catch (Exception e) {
e.printStackTrace();
}
}
class Ex extends Exception {}
static void foo() throws Ex {}
interface I {
void f();
}
}
@@ -170,6 +170,10 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
}
}
public void testSurroundWithTryCatchFunctionalExpression() {
doTest(getTestName(false), new JavaWithTryCatchSurrounder());
}
private void doTest(@NotNull String fileName, final Surrounder surrounder) {
configureByFile(BASE_PATH + fileName + ".java");