Try-with-resources unwrap test; cleanup

This commit is contained in:
Roman Shevchenko
2011-03-11 21:12:43 +01:00
parent a8dcf9e9c8
commit f3cbeaaaf3
6 changed files with 32 additions and 28 deletions
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.unwrap;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import java.util.List;
@@ -52,7 +53,7 @@ public class JavaAnonymousUnwrapper extends JavaUnwrapper {
context.deleteExactly(from);
}
private PsiElement findElementToExtractFrom(PsiElement el) {
private static PsiElement findElementToExtractFrom(PsiElement el) {
if (el.getParent() instanceof PsiNewExpression) el = el.getParent();
el = findTopmostParentOfType(el, PsiMethodCallExpression.class);
el = findTopmostParentOfType(el, PsiAssignmentExpression.class);
@@ -64,4 +65,13 @@ public class JavaAnonymousUnwrapper extends JavaUnwrapper {
return el;
}
private static PsiElement findTopmostParentOfType(PsiElement el, Class<? extends PsiElement> clazz) {
while (true) {
@SuppressWarnings({"unchecked"})
PsiElement temp = PsiTreeUtil.getParentOfType(el, clazz, true, PsiAnonymousClass.class);
if (temp == null || temp instanceof PsiFile) return el;
el = temp;
}
}
}
@@ -18,6 +18,7 @@ package com.intellij.codeInsight.unwrap;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIfStatement;
import com.intellij.psi.PsiStatement;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import java.util.Set;
@@ -28,7 +29,7 @@ public abstract class JavaElseUnwrapperBase extends JavaUnwrapper {
}
public boolean isApplicableTo(PsiElement e) {
return (isElseBlock(e) || isElseKeyword(e)) && isValidConstruct(e);
return (PsiUtil.isElseBlock(e) || isElseKeyword(e)) && isValidConstruct(e);
}
private boolean isElseKeyword(PsiElement e) {
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIfStatement;
import com.intellij.psi.PsiStatement;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
public class JavaIfUnwrapper extends JavaUnwrapper {
@@ -27,7 +28,7 @@ public class JavaIfUnwrapper extends JavaUnwrapper {
}
public boolean isApplicableTo(PsiElement e) {
return e instanceof PsiIfStatement && !isElseBlock(e);
return e instanceof PsiIfStatement && !PsiUtil.isElseBlock(e);
}
@Override
@@ -17,6 +17,8 @@ package com.intellij.codeInsight.unwrap;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiResourceList;
import com.intellij.psi.PsiResourceVariable;
import com.intellij.psi.PsiTryStatement;
import com.intellij.util.IncorrectOperationException;
@@ -30,8 +32,8 @@ public class JavaTryUnwrapper extends JavaUnwrapper {
}
@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
PsiTryStatement trySt = (PsiTryStatement)element;
protected void doUnwrap(final PsiElement element, final Context context) throws IncorrectOperationException {
final PsiTryStatement trySt = (PsiTryStatement)element;
context.extractFromCodeBlock(trySt.getTryBlock(), trySt);
context.extractFromCodeBlock(trySt.getFinallyBlock(), trySt);
@@ -17,7 +17,6 @@ package com.intellij.codeInsight.unwrap;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import java.util.ArrayList;
@@ -60,11 +59,6 @@ public abstract class JavaUnwrapper implements Unwrapper {
protected abstract void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException;
protected boolean isElseBlock(PsiElement e) {
PsiElement p = e.getParent();
return p instanceof PsiIfStatement && e == ((PsiIfStatement)p).getElseBranch();
}
protected static class Context {
private final List<PsiElement> myElementsToExtract = new ArrayList<PsiElement>();
private final boolean myIsEffective;
@@ -96,8 +90,6 @@ public abstract class JavaUnwrapper implements Unwrapper {
}
private void extract(PsiElement first, PsiElement last, PsiElement from) throws IncorrectOperationException {
if (first == null) return;
// trim leading empty spaces
while (first != last && first instanceof PsiWhiteSpace) {
first = first.getNextSibling();
@@ -109,7 +101,7 @@ public abstract class JavaUnwrapper implements Unwrapper {
}
// nothing to extract
if (first == last && last instanceof PsiWhiteSpace) return;
if (first == null || last == null || first == last && last instanceof PsiWhiteSpace) return;
PsiElement toExtract = first;
if (myIsEffective) {
@@ -117,8 +109,10 @@ public abstract class JavaUnwrapper implements Unwrapper {
}
do {
addElementToExtract(toExtract);
toExtract = toExtract.getNextSibling();
if (toExtract != null) {
addElementToExtract(toExtract);
toExtract = toExtract.getNextSibling();
}
first = first.getNextSibling();
}
while (first != null && first.getPrevSibling() != last);
@@ -145,19 +139,11 @@ public abstract class JavaUnwrapper implements Unwrapper {
addElementToExtract(toExtract);
}
private PsiStatement copyElement(PsiStatement e) throws IncorrectOperationException {
private static PsiStatement copyElement(PsiStatement e) throws IncorrectOperationException {
// We cannot call el.copy() for 'else' since it sets context to parent 'if'.
// This causes copy to be invalidated after parent 'if' is removed by setElseBranch method.
PsiElementFactory factory = JavaPsiFacade.getInstance(e.getProject()).getElementFactory();
return factory.createStatementFromText(e.getText(), null);
}
}
protected PsiElement findTopmostParentOfType(PsiElement el, Class clazz) {
while (true) {
PsiElement temp = PsiTreeUtil.getParentOfType(el, clazz, true, PsiAnonymousClass.class);
if (temp == null || temp instanceof PsiFile) return el;
el = temp;
}
}
}