Add catch block fix made aware of try-with-resources

This commit is contained in:
Roman Shevchenko
2011-03-01 16:24:18 +01:00
parent f627677fdd
commit a5a7e6846a
6 changed files with 85 additions and 13 deletions

View File

@@ -32,6 +32,7 @@ import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@@ -123,19 +124,30 @@ public class AddExceptionToCatchFix extends BaseIntentionAction {
return true;
}
private static PsiElement findElement(PsiFile file, int offset) {
@Nullable
private static PsiElement findElement(final PsiFile file, final int offset) {
PsiElement element = file.findElementAt(offset);
if (element instanceof PsiWhiteSpace) element = file.findElementAt(offset - 1);
if (element == null) return null;
PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class, PsiMethod.class);
final PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class, PsiMethod.class);
if (parent == null || parent instanceof PsiMethod) return null;
PsiTryStatement statement = (PsiTryStatement) parent;
PsiCodeBlock tryBlock = statement.getTryBlock();
if (tryBlock.getTextRange().getStartOffset() <= offset && tryBlock.getTextRange().getEndOffset() > offset) {
final PsiTryStatement statement = (PsiTryStatement) parent;
final PsiCodeBlock tryBlock = statement.getTryBlock();
if (tryBlock != null && tryBlock.getTextRange().contains(offset)) {
if (!ExceptionUtil.collectUnhandledExceptions(tryBlock, statement.getParent()).isEmpty()) {
return tryBlock;
}
}
final PsiResourceList resourceList = statement.getResourceList();
if (resourceList != null && resourceList.getTextRange().contains(offset)) {
if (!ExceptionUtil.collectUnhandledExceptions(resourceList, statement.getParent()).isEmpty()) {
return resourceList;
}
}
return null;
}

View File

@@ -0,0 +1,15 @@
// "Add Catch Clause(s)" "true"
import java.io.IOException;
class Test {
static class MyResource implements AutoCloseable {
public void close() throws IOException { }
}
void m() {
try (MyResource r = new MyResource()) {
} catch (IOException e) {
<selection>e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.</selection>
}
}
}

View File

@@ -0,0 +1,19 @@
// "Add Catch Clause(s)" "true"
class Test {
static class E1 extends Exception { }
static class E2 extends Exception { }
static class MyResource implements AutoCloseable {
public void doSomething() throws E1 { }
public void close() throws E2 { }
}
void m() {
try (MyResource r = new MyResource()) {
r.doSomething();
} catch (E1 ignore) {
} catch (E2 e2) {
<selection>e2.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.</selection>
}
}
}

View File

@@ -0,0 +1,13 @@
// "Add Catch Clause(s)" "true"
import java.io.IOException;
class Test {
static class MyResource implements AutoCloseable {
public void close() throws IOException { }
}
void m() {
try (<caret>MyResource r = new MyResource()) {
}
}
}

View File

@@ -0,0 +1,17 @@
// "Add Catch Clause(s)" "true"
class Test {
static class E1 extends Exception { }
static class E2 extends Exception { }
static class MyResource implements AutoCloseable {
public void doSomething() throws E1 { }
public void close() throws E2 { }
}
void m() {
try (<caret>MyResource r = new MyResource()) {
r.doSomething();
} catch (E1 ignore) {
}
}
}

View File

@@ -1,16 +1,12 @@
package com.intellij.codeInsight.daemon.quickFix;
public class AddCatchBlockTest extends LightQuickFixTestCase {
public void test() throws Exception { doAllTests(); }
public class AddExceptionToCatchTest extends LightQuickFixTestCase {
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock";
}
}