change signature: move declarations out of try/catch block (IDEA-169213)

This commit is contained in:
Anna.Kozlova
2017-03-07 14:57:35 +01:00
parent 2769981fb1
commit af59ff1487
5 changed files with 39 additions and 5 deletions
@@ -26,7 +26,6 @@ import com.intellij.psi.impl.source.tree.JavaJspElementType;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.IncorrectOperationException;
@@ -42,7 +41,7 @@ public class SurroundWithUtil {
private SurroundWithUtil() {
}
static PsiElement[] moveDeclarationsOut(PsiElement block, PsiElement[] statements, boolean generateInitializers) {
public static PsiElement[] moveDeclarationsOut(PsiElement block, PsiElement[] statements, boolean generateInitializers) {
try{
PsiManager psiManager = block.getManager();
PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.ExceptionUtil;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInsight.daemon.impl.quickfix.RemoveUnusedVariableUtil;
import com.intellij.codeInsight.generation.surroundWith.SurroundWithUtil;
import com.intellij.codeInspection.dataFlow.ControlFlowAnalyzer;
import com.intellij.lang.StdLanguages;
import com.intellij.lang.java.JavaLanguage;
@@ -359,11 +360,15 @@ public class JavaChangeSignatureUsageProcessor implements ChangeSignatureUsagePr
anchor = PsiTreeUtil.getParentOfType(ref, PsiStatement.class);
}
LOG.assertTrue(anchor != null);
tryStatement.getTryBlock().add(anchor);
tryStatement = (PsiTryStatement)anchor.getParent().addAfter(tryStatement, anchor);
PsiElement container = anchor.getParent();
PsiElement[] elements = SurroundWithUtil.moveDeclarationsOut(container, new PsiElement[]{anchor}, true);
tryStatement = (PsiTryStatement)container.addAfter(tryStatement, elements[elements.length - 1]);
PsiCodeBlock tryBlock = tryStatement.getTryBlock();
LOG.assertTrue(tryBlock != null);
tryBlock.addRange(elements[0], elements[elements.length - 1]);
addExceptions(newExceptions, tryStatement);
anchor.delete();
container.deleteChildRange(elements[0], elements[elements.length - 1]);
tryStatement.getCatchSections()[0].delete(); //Delete dummy catch section
}
}
@@ -0,0 +1,8 @@
class Test {
int <caret>foo() throws IllegalArgumentException { return 1;}
void fooBar() throws IllegalArgumentException{
int a = foo();
System.out.println(a);
}
}
@@ -0,0 +1,13 @@
class Test {
int foo() throws Exception { return 1;}
void fooBar() throws IllegalArgumentException{
int a = 0;
try {
a = foo();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(a);
}
}
@@ -328,6 +328,15 @@ public class ChangeSignatureTest extends ChangeSignatureBaseTest {
);
}
public void testLessSpecificException() {
doTest(null, null, null, new SimpleParameterGen(new ParameterInfoImpl[0]),
method -> new ThrownExceptionInfo[]{
new JavaThrownExceptionInfo(0, myFactory.createTypeByFQClassName("java.lang.Exception", method.getResolveScope()))
},
false
);
}
public void testReorderWithVarargs() { // IDEADEV-26977
doTest(null, new ParameterInfoImpl[]{
new ParameterInfoImpl(1),