diff --git a/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java b/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java index c40226d55c1f..721b908192f2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/ExceptionUtil.java @@ -284,16 +284,24 @@ public class ExceptionUtil { @NotNull public static List getUnhandledExceptions(PsiElement[] elements) { final List array = new ArrayList(); - PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() { - @Override public void visitCallExpression(PsiCallExpression expression) { + final PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() { + @Override + public void visitCallExpression(PsiCallExpression expression) { addExceptions(array, getUnhandledExceptions(expression, null)); visitElement(expression); } - @Override public void visitThrowStatement(PsiThrowStatement statement) { + @Override + public void visitThrowStatement(PsiThrowStatement statement) { addException(array, getUnhandledException(statement, null)); visitElement(statement); } + + @Override + public void visitResourceVariable(PsiResourceVariable resourceVariable) { + addExceptions(array, getUnhandledCloserExceptions(resourceVariable, null)); + visitElement(resourceVariable); + } }; for (PsiElement element : elements) { diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java index 545d1182fd19..9233df64c05c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SurroundWithTryCatchFix.java @@ -13,15 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: mike - * Date: Aug 19, 2002 - * Time: 8:28:43 PM - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.daemon.QuickFixBundle; @@ -40,8 +31,13 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +/** + * @author mike + * Date: Aug 19, 2002 + */ public class SurroundWithTryCatchFix implements IntentionAction { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.SurroundWithTryCatchFix"); + private PsiStatement myStatement; public SurroundWithTryCatchFix(PsiElement element) { @@ -59,11 +55,10 @@ public class SurroundWithTryCatchFix implements IntentionAction { } public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - if (myStatement == null || !myStatement.isValid()) { - return false; - } - return !(myStatement instanceof PsiExpressionStatement) || - !HighlightUtil.isSuperOrThisMethodCall(((PsiExpressionStatement)myStatement).getExpression()); + return myStatement != null && + myStatement.isValid() && + (!(myStatement instanceof PsiExpressionStatement) || + !HighlightUtil.isSuperOrThisMethodCall(((PsiExpressionStatement)myStatement).getExpression())); } public void invoke(@NotNull Project project, Editor editor, PsiFile file) { @@ -101,5 +96,4 @@ public class SurroundWithTryCatchFix implements IntentionAction { public boolean startInWriteAction() { return true; } - } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/after1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/after1.java new file mode 100644 index 000000000000..b4463038a988 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/after1.java @@ -0,0 +1,17 @@ +// "Surround with try/catch" "true" +class C { + static class E1 extends Exception { } + + static class MyResource implements AutoCloseable { + public void close() throws E1 { } + } + + void m() { + try { + try (MyResource r = new MyResource()) { + } + } catch (E1 e1) { + e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/before1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/before1.java new file mode 100644 index 000000000000..50f6b43d1be2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/before1.java @@ -0,0 +1,13 @@ +// "Surround with try/catch" "true" +class C { + static class E1 extends Exception { } + + static class MyResource implements AutoCloseable { + public void close() throws E1 { } + } + + void m() { + try (MyResource r = new MyResource()) { + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/SurroundWithTryCatchTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/SurroundWithTryCatchTest.java new file mode 100644 index 000000000000..207dcf388099 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/SurroundWithTryCatchTest.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.quickFix; + +public class SurroundWithTryCatchTest extends LightQuickFixTestCase { + public void test() throws Exception { + doAllTests(); + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry"; + } +} \ No newline at end of file