inline: discard inline of variable with initializer in try/catch (IDEA-90390)

This commit is contained in:
anna
2012-09-25 09:38:33 +02:00
parent d61a204d41
commit 2b84533a1f
5 changed files with 65 additions and 0 deletions
@@ -16,6 +16,7 @@
*/
package com.intellij.refactoring.inline;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.codeInsight.highlighting.HighlightManager;
import com.intellij.codeInsight.intention.QuickFixFactory;
@@ -153,6 +154,12 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
return;
}
PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(defToInline, PsiTryStatement.class);
if (tryStatement != null) {
if (ExceptionUtil.getThrownExceptions(defToInline).isEmpty()) {
tryStatement = null;
}
}
PsiFile workingFile = local.getContainingFile();
for (PsiElement ref : refsToInline) {
final PsiFile otherFile = ref.getContainingFile();
@@ -161,6 +168,10 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.INLINE_VARIABLE);
return;
}
if (tryStatement != null && !PsiTreeUtil.isAncestor(tryStatement, ref, false)) {
CommonRefactoringUtil.showErrorHint(project, editor, "Unable to inline outside try/catch statement", REFACTORING_NAME, HelpID.INLINE_VARIABLE);
return;
}
}
for (final PsiElement ref : refsToInline) {
@@ -0,0 +1,18 @@
import java.io.*;
class Test {
private static InputStream getInputUnchecked() throws IOException {
InputStream in;
try {
in = ff();
}
catch (IOException e) {
throw new IOException();
}
return i<caret>n;
}
static InputStream ff() throws IOException {
return null;
}
}
@@ -0,0 +1,15 @@
import java.io.*;
class Test {
private static InputStream getInputUnchecked(InputStream inputSupplier) throws IOException {
InputStream in;
try {
in = null;
}
catch (IOException e) {
throw new IOException();
}
return i<caret>n;
}
}
@@ -0,0 +1,13 @@
import java.io.*;
class Test {
private static InputStream getInputUnchecked(InputStream inputSupplier) throws IOException {
try {
}
catch (IOException e) {
throw new IOException();
}
return null;
}
}
@@ -147,6 +147,14 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
doTest(false, "Cannot perform refactoring.\n" +
"Variable 'x' is accessed for writing.");
}
public void testInlineFromTryCatch() throws Exception {
doTest(true, "Unable to inline outside try/catch statement");
}
public void testInlineFromTryCatchAvailable() throws Exception {
doTest(true);
}
public void testConditionExpr() throws Exception {
doTest(true);