From fb2b6ed995b522402c5310ee0362dab9e89f0cf1 Mon Sep 17 00:00:00 2001 From: Anton Makeev Date: Fri, 10 Sep 2010 17:08:21 +0400 Subject: [PATCH] psi util: @nullables --- .../com/intellij/psi/util/PsiTreeUtil.java | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java b/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java index ee06af7ba7fd..35fa915d585e 100644 --- a/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java +++ b/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java @@ -40,7 +40,7 @@ public class PsiTreeUtil { private static final Logger LOG = Logger.getInstance("#com.intellij.psi.util.PsiTreeUtil"); private static final Key INDEX = Key.create("PsiTreeUtil.copyElements.INDEX"); - private static final Key MARKER = Key.create("PsiTreeUtil.copyElements.INDEX"); + private static final Key MARKER = Key.create("PsiTreeUtil.copyElements.MARKER"); private PsiTreeUtil() { } @@ -195,19 +195,27 @@ public class PsiTreeUtil { return parents; } - @Nullable public static T findChildOfType(@Nullable final PsiElement element, @NotNull final Class aClass) { + @Nullable + public static T findChildOfType(@Nullable final PsiElement element, @NotNull final Class aClass) { return findChildOfType(element, aClass, true); } - @Nullable public static T findChildOfType(@Nullable final PsiElement element, @NotNull final Class aClass, final boolean strict) { + @Nullable + public static T findChildOfType(@Nullable final PsiElement element, + @NotNull final Class aClass, + final boolean strict) { return findChildOfAnyType(element, strict, aClass); } - @Nullable public static T findChildOfAnyType(@Nullable final PsiElement element, @NotNull final Class... classes) { + @Nullable + public static T findChildOfAnyType(@Nullable final PsiElement element, @NotNull final Class... classes) { return findChildOfAnyType(element, true, classes); } - @Nullable public static T findChildOfAnyType(@Nullable final PsiElement element, final boolean strict, @NotNull final Class... classes) { + @Nullable + public static T findChildOfAnyType(@Nullable final PsiElement element, + final boolean strict, + @NotNull final Class... classes) { PsiElementProcessor.FindElement processor = new PsiElementProcessor.FindElement() { @Override public boolean execute(PsiElement each) { @@ -225,9 +233,10 @@ public class PsiTreeUtil { return (T)processor.getFoundElement(); } - @Nullable public static T getChildOfType(@Nullable PsiElement element, @NotNull Class aClass) { + @Nullable + public static T getChildOfType(@Nullable PsiElement element, @NotNull Class aClass) { if (element == null) return null; - for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){ + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (instanceOf(aClass, child)) return (T)child; } return null; @@ -240,7 +249,8 @@ public class PsiTreeUtil { return child; } - @Nullable public static T[] getChildrenOfType(@Nullable PsiElement element, @NotNull Class aClass) { + @Nullable + public static T[] getChildrenOfType(@Nullable PsiElement element, @NotNull Class aClass) { if (element == null) return null; List result = null; @@ -253,7 +263,8 @@ public class PsiTreeUtil { return result == null ? null : ArrayUtil.toObjectArray(result, aClass); } - @NotNull public static List getChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class aClass) { + @NotNull + public static List getChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class aClass) { if (element == null) return Collections.emptyList(); List result = new SmartList(); @@ -291,28 +302,31 @@ public class PsiTreeUtil { * @return the element, or null if none was found. * @since 5.1 */ - @Nullable public static T getChildOfAnyType(@Nullable PsiElement element, @NotNull Class... classes) { + @Nullable + public static T getChildOfAnyType(@Nullable PsiElement element, @NotNull Class... classes) { if (element == null) return null; - for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){ - for(Class aClass : classes) { + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { + for (Class aClass : classes) { if (instanceOf(aClass, child)) return (T)child; } } return null; } - @Nullable public static T getNextSiblingOfType(@Nullable PsiElement sibling, @NotNull Class aClass) { + @Nullable + public static T getNextSiblingOfType(@Nullable PsiElement sibling, @NotNull Class aClass) { if (sibling == null) return null; - for(PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()){ + for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) { if (instanceOf(aClass, child)) return (T)child; } return null; } - @Nullable public static T getPrevSiblingOfType(@Nullable PsiElement sibling, @NotNull Class aClass) { + @Nullable + public static T getPrevSiblingOfType(@Nullable PsiElement sibling, @NotNull Class aClass) { if (sibling == null) return null; - for(PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()){ - if (instanceOf(aClass, child)) return (T)child; + for (PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()) { + if (instanceOf(aClass, child)) return (T)child; } return null; } @@ -342,7 +356,7 @@ public class PsiTreeUtil { } @Nullable - public static E getStubOrPsiParentOfType(@Nullable PsiElement element, final Class parentClass) { + public static E getStubOrPsiParentOfType(@Nullable PsiElement element, @NotNull Class parentClass) { if (element instanceof StubBasedPsiElement) { StubBase stub = (StubBase)((StubBasedPsiElement)element).getStub(); if (stub != null) {