cleanup try statement on resources list deletion (IDEA-109543)

This commit is contained in:
Anna Kozlova
2013-06-25 20:15:29 +04:00
parent 95ae5e69db
commit b8638b7b46
7 changed files with 38 additions and 23 deletions
@@ -250,7 +250,6 @@ public class RemoveUnusedVariableUtil {
if (element instanceof PsiField) {
((PsiField)element).normalizeDeclaration();
}
if (processResourceVariable(element)) break;
element.delete();
}
return !sideEffectsFound;
@@ -260,24 +259,4 @@ public class RemoveUnusedVariableUtil {
}
return true;
}
private static boolean processResourceVariable(PsiElement element) {
final PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
if (element instanceof PsiResourceVariable && tryStatement != null) {
final PsiResourceList resourceList = tryStatement.getResourceList();
if (resourceList != null &&
resourceList.getResourceVariables().contains(element) &&
resourceList.getResourceVariablesCount() == 1) {
final PsiCodeBlock tryBlock = tryStatement.getTryBlock();
if (tryBlock != null) {
tryBlock.deleteChildRange(tryBlock.getFirstChild(), tryBlock.getFirstBodyElement());
tryBlock.deleteChildRange(tryBlock.getLastBodyElement(), tryBlock.getLastChild());
tryStatement.replace(tryBlock);
return true;
}
}
}
return false;
}
}
@@ -68,6 +68,7 @@ public class PsiResourceListImpl extends CompositePsiElement implements PsiResou
public void deleteChildInternal(@NotNull final ASTNode child) {
if (child.getPsi() instanceof PsiResourceVariable && getResourceVariablesCount() == 1) {
getTreeParent().deleteChildInternal(this);
return;
}
super.deleteChildInternal(child);
@@ -181,6 +181,26 @@ public class PsiTryStatementImpl extends CompositePsiElement implements PsiTrySt
return true;
}
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
if (child.getPsi() instanceof PsiResourceList && getCatchBlocks().length == 0 && getFinallyBlock() == null) {
final PsiCodeBlock tryBlock = getTryBlock();
if (tryBlock != null) {
final PsiCodeBlock block = (PsiCodeBlock)replace(tryBlock);
final PsiJavaToken lBrace = block.getLBrace();
final PsiJavaToken rBrace = block.getRBrace();
if (lBrace != null) {
lBrace.delete();
}
if (rBrace != null) {
rBrace.delete();
}
return;
}
}
super.deleteChildInternal(child);
}
public String toString() {
return "PsiTryStatement";
}
@@ -0,0 +1,6 @@
class ARM {
void f() {
try (AutoCloseable <caret>r = null) {
} finally {}
}
}
@@ -0,0 +1,6 @@
class ARM {
void f() {
try {
} finally {}
}
}
@@ -1,6 +1,4 @@
class ARM {
void f() {
try {
}
}
}
@@ -149,6 +149,11 @@ public class SafeDeleteTest extends MultiFileTestCase {
doSingleFileTest();
}
public void testLastResourceVariableWithFinallyBlock() throws Exception {
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
doSingleFileTest();
}
public void testLastTypeParam() throws Exception {
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
doSingleFileTest();