lambda: unhandled exceptions inside method body should be treated as those inside anonymous classes (IDEA-121741)

This commit is contained in:
Anna Kozlova
2014-03-06 15:30:24 +01:00
parent a042720e74
commit 1a931f9871
5 changed files with 74 additions and 8 deletions
@@ -57,8 +57,21 @@ public class AddExceptionToThrowsFix extends BaseIntentionAction {
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiMethod targetMethod = PsiTreeUtil.getParentOfType(myWrongElement, PsiMethod.class);
List<PsiClassType> exceptions = getUnhandledExceptions(myWrongElement, targetMethod);
PsiElement targetElement = null;
PsiMethod targetMethod = null;
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(myWrongElement, PsiLambdaExpression.class);
if (lambdaExpression != null) {
targetMethod = LambdaUtil.getFunctionalInterfaceMethod(lambdaExpression);
targetElement = lambdaExpression.getBody();
}
if (targetElement == null && targetMethod == null) {
targetMethod = PsiTreeUtil.getParentOfType(myWrongElement, PsiMethod.class);
targetElement = targetMethod;
}
List<PsiClassType> exceptions = getUnhandledExceptions(myWrongElement, targetElement, targetMethod);
if (exceptions == null || targetMethod == null) return;
Set<PsiClassType> unhandledExceptions = new THashSet<PsiClassType>(exceptions);
@@ -168,9 +181,21 @@ public class AddExceptionToThrowsFix extends BaseIntentionAction {
if (!(file instanceof PsiJavaFile)) return false;
if (myWrongElement == null || !myWrongElement.isValid()) return false;
PsiMethod method = PsiTreeUtil.getParentOfType(myWrongElement, PsiMethod.class);
if (method == null || !method.getThrowsList().isPhysical()) return false;
List<PsiClassType> unhandled = getUnhandledExceptions(myWrongElement, method);
PsiElement targetElement = null;
PsiMethod targetMethod = null;
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(myWrongElement, PsiLambdaExpression.class);
if (lambdaExpression != null) {
targetMethod = LambdaUtil.getFunctionalInterfaceMethod(lambdaExpression);
targetElement = lambdaExpression.getBody();
}
if (targetElement == null && targetMethod == null) {
targetMethod = PsiTreeUtil.getParentOfType(myWrongElement, PsiMethod.class);
targetElement = targetMethod;
}
if (targetElement == null || targetMethod == null || !targetMethod.getThrowsList().isPhysical()) return false;
List<PsiClassType> unhandled = getUnhandledExceptions(myWrongElement, targetElement, targetMethod);
if (unhandled == null || unhandled.isEmpty()) return false;
setText(QuickFixBundle.message("add.exception.to.throws.text", unhandled.size()));
@@ -184,13 +209,13 @@ public class AddExceptionToThrowsFix extends BaseIntentionAction {
}
@Nullable
private static List<PsiClassType> getUnhandledExceptions(@Nullable PsiElement element, PsiMethod topElement) {
private static List<PsiClassType> getUnhandledExceptions(@Nullable PsiElement element, PsiElement topElement, PsiMethod targetMethod) {
if (element == null || element == topElement) return null;
List<PsiClassType> unhandledExceptions = ExceptionUtil.getUnhandledExceptions(element);
if (!filterInProjectExceptions(topElement, unhandledExceptions).isEmpty()) {
if (!filterInProjectExceptions(targetMethod, unhandledExceptions).isEmpty()) {
return unhandledExceptions;
}
return getUnhandledExceptions(element.getParent(), topElement);
return getUnhandledExceptions(element.getParent(), topElement, targetMethod);
}
@NotNull
@@ -0,0 +1,12 @@
// "Add Exception to Method Signature" "true"
class C {
interface I {
void a() throws InterruptedException;
}
{
I i = () -> {
Thread.sleep(2000);
};
}
}
@@ -0,0 +1,9 @@
// "Add Exception to Method Signature" "false"
class C {
public static void main(String[] args) throws InterruptedException {
new Thread(( ) -> {
Thread.sl<caret>eep(2000);
}).start();
}
}
@@ -0,0 +1,12 @@
// "Add Exception to Method Signature" "true"
class C {
interface I {
void a();
}
{
I i = () -> {
Thread.sl<caret>eep(2000);
};
}
}
@@ -1,5 +1,8 @@
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.testFramework.IdeaTestUtil;
public class AddExceptionToThrowsTest extends LightQuickFixParameterizedTestCase {
public void test() throws Exception {
doAllTests();
@@ -9,4 +12,9 @@ public class AddExceptionToThrowsTest extends LightQuickFixParameterizedTestCase
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows";
}
@Override
protected Sdk getProjectJDK() {
return IdeaTestUtil.getMockJdk18();
}
}