IPP: fix convert catch to throws inside a lambda

This commit is contained in:
Bas Leijdekkers
2014-12-03 15:30:49 +01:00
parent f972926905
commit 5763ba435b
5 changed files with 54 additions and 7 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2012 Bas Leijdekkers
* Copyright 2007-2014 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
*/
package com.siyeh.ipp.exceptions;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
@@ -36,8 +37,18 @@ public class ConvertCatchToThrowsIntention extends Intention {
@Override
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
final PsiCatchSection catchSection = (PsiCatchSection)element.getParent();
final PsiMethod method = PsiTreeUtil.getParentOfType(catchSection, PsiMethod.class);
if (method == null) {
final NavigatablePsiElement owner = PsiTreeUtil.getParentOfType(catchSection, PsiMethod.class, PsiLambdaExpression.class);
final PsiMethod method;
if (owner instanceof PsiMethod) {
method = (PsiMethod)owner;
}
else if (owner instanceof PsiLambdaExpression) {
method = LambdaUtil.getFunctionalInterfaceMethod(owner);
if (method == null || !FileModificationService.getInstance().preparePsiElementsForWrite(method)) {
return;
}
}
else {
return;
}
// todo warn if method implements or overrides some base method
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2013 Bas Leijdekkers
* Copyright 2007-2014 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ class ConvertCatchToThrowsPredicate implements PsiElementPredicate {
if (element instanceof PsiCodeBlock) {
return false;
}
final PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class, true, PsiClass.class);
return method != null;
final PsiElement owner = PsiTreeUtil.getParentOfType(parent, PsiMethod.class, PsiClass.class, PsiLambdaExpression.class);
return owner instanceof PsiMethod || owner instanceof PsiLambdaExpression;
}
}
@@ -0,0 +1,19 @@
class Lambda {
void bar() throws E {}
void foo() {
U u = () -> {
try {
} <caret>catch (E e) {
e.printStackTrace();
}
};
}
interface U {
void f();
}
class E extends Exception {}
}
@@ -0,0 +1,16 @@
class Lambda {
void bar() throws E {}
void foo() {
U u = () -> {
};
}
interface U {
void f() throws E;
}
class E extends Exception {}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ public class ConvertCatchToThrowsTest extends IPPTestCase {
public void testArmWithPluralCatches() { doTest(); }
public void testArmWithSingleCatch() { doTest(); }
public void testExistingThrows() { doTest(); }
public void testLambda() { doTest(); }
@Override
protected String getIntentionName() {