From f3cbeaaaf396f02cd4f2471f58d37eca43f0c52b Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 11 Mar 2011 18:02:59 +0100 Subject: [PATCH] Try-with-resources unwrap test; cleanup --- .../unwrap/JavaAnonymousUnwrapper.java | 12 ++++++++- .../unwrap/JavaElseUnwrapperBase.java | 3 ++- .../codeInsight/unwrap/JavaIfUnwrapper.java | 3 ++- .../codeInsight/unwrap/JavaTryUnwrapper.java | 6 +++-- .../codeInsight/unwrap/JavaUnwrapper.java | 26 +++++-------------- .../src/com/intellij/psi/util/PsiUtil.java | 10 ++++--- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaAnonymousUnwrapper.java b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaAnonymousUnwrapper.java index 50c643588cb0..0d5827438a57 100644 --- a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaAnonymousUnwrapper.java +++ b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaAnonymousUnwrapper.java @@ -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 clazz) { + while (true) { + @SuppressWarnings({"unchecked"}) + PsiElement temp = PsiTreeUtil.getParentOfType(el, clazz, true, PsiAnonymousClass.class); + if (temp == null || temp instanceof PsiFile) return el; + el = temp; + } + } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaElseUnwrapperBase.java b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaElseUnwrapperBase.java index bbfbde94b4e3..8d18735e5e0e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaElseUnwrapperBase.java +++ b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaElseUnwrapperBase.java @@ -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) { diff --git a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaIfUnwrapper.java b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaIfUnwrapper.java index 26c304e4ebe3..5ae056cdf9c8 100644 --- a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaIfUnwrapper.java +++ b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaIfUnwrapper.java @@ -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 diff --git a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaTryUnwrapper.java b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaTryUnwrapper.java index 28cf4d8e9f26..85cf8089611f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaTryUnwrapper.java +++ b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaTryUnwrapper.java @@ -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); diff --git a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaUnwrapper.java b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaUnwrapper.java index 47ee17a8f156..dbec98c6a72d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaUnwrapper.java +++ b/java/java-impl/src/com/intellij/codeInsight/unwrap/JavaUnwrapper.java @@ -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 myElementsToExtract = new ArrayList(); 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; - } - } } diff --git a/java/openapi/src/com/intellij/psi/util/PsiUtil.java b/java/openapi/src/com/intellij/psi/util/PsiUtil.java index 25fef64b0ffd..97d14f4c5fd9 100644 --- a/java/openapi/src/com/intellij/psi/util/PsiUtil.java +++ b/java/openapi/src/com/intellij/psi/util/PsiUtil.java @@ -890,9 +890,13 @@ public final class PsiUtil extends PsiUtilBase { } public static boolean isTryBlock(final PsiElement element) { - return element instanceof PsiCodeBlock && - element.getParent() instanceof PsiTryStatement && - ((PsiTryStatement)element.getParent()).getTryBlock() == element; + final PsiElement parent = element.getParent(); + return parent instanceof PsiTryStatement && element == ((PsiTryStatement)parent).getTryBlock(); + } + + public static boolean isElseBlock(final PsiElement element) { + final PsiElement parent = element.getParent(); + return parent instanceof PsiIfStatement && element == ((PsiIfStatement)parent).getElseBranch(); } public static boolean isJavaToken(@Nullable final PsiElement element, final IElementType type) {