psi util: @nullables

This commit is contained in:
Anton Makeev
2010-09-10 17:08:21 +04:00
parent fedc22bdba
commit fb2b6ed995
@@ -40,7 +40,7 @@ public class PsiTreeUtil {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.util.PsiTreeUtil");
private static final Key<Integer> INDEX = Key.create("PsiTreeUtil.copyElements.INDEX");
private static final Key<Object> MARKER = Key.create("PsiTreeUtil.copyElements.INDEX");
private static final Key<Object> MARKER = Key.create("PsiTreeUtil.copyElements.MARKER");
private PsiTreeUtil() {
}
@@ -195,19 +195,27 @@ public class PsiTreeUtil {
return parents;
}
@Nullable public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element, @NotNull final Class<T> aClass) {
@Nullable
public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element, @NotNull final Class<T> aClass) {
return findChildOfType(element, aClass, true);
}
@Nullable public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element, @NotNull final Class<T> aClass, final boolean strict) {
@Nullable
public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element,
@NotNull final Class<T> aClass,
final boolean strict) {
return findChildOfAnyType(element, strict, aClass);
}
@Nullable public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element, @NotNull final Class<T>... classes) {
@Nullable
public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element, @NotNull final Class<T>... classes) {
return findChildOfAnyType(element, true, classes);
}
@Nullable public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element, final boolean strict, @NotNull final Class<T>... classes) {
@Nullable
public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element,
final boolean strict,
@NotNull final Class<T>... classes) {
PsiElementProcessor.FindElement<PsiElement> processor = new PsiElementProcessor.FindElement<PsiElement>() {
@Override
public boolean execute(PsiElement each) {
@@ -225,9 +233,10 @@ public class PsiTreeUtil {
return (T)processor.getFoundElement();
}
@Nullable public static <T extends PsiElement> T getChildOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
@Nullable
public static <T extends PsiElement> T getChildOfType(@Nullable PsiElement element, @NotNull Class<T> 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 extends PsiElement> T[] getChildrenOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
@Nullable
public static <T extends PsiElement> T[] getChildrenOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
if (element == null) return null;
List<T> result = null;
@@ -253,7 +263,8 @@ public class PsiTreeUtil {
return result == null ? null : ArrayUtil.toObjectArray(result, aClass);
}
@NotNull public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class<T> aClass) {
@NotNull
public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class<T> aClass) {
if (element == null) return Collections.emptyList();
List<T> result = new SmartList<T>();
@@ -291,28 +302,31 @@ public class PsiTreeUtil {
* @return the element, or null if none was found.
* @since 5.1
*/
@Nullable public static <T extends PsiElement> T getChildOfAnyType(@Nullable PsiElement element, @NotNull Class<? extends T>... classes) {
@Nullable
public static <T extends PsiElement> T getChildOfAnyType(@Nullable PsiElement element, @NotNull Class<? extends T>... classes) {
if (element == null) return null;
for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){
for(Class<? extends T> aClass : classes) {
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
for (Class<? extends T> aClass : classes) {
if (instanceOf(aClass, child)) return (T)child;
}
}
return null;
}
@Nullable public static <T extends PsiElement> T getNextSiblingOfType(@Nullable PsiElement sibling, @NotNull Class<T> aClass) {
@Nullable
public static <T extends PsiElement> T getNextSiblingOfType(@Nullable PsiElement sibling, @NotNull Class<T> 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 extends PsiElement> T getPrevSiblingOfType(@Nullable PsiElement sibling, @NotNull Class<T> aClass) {
@Nullable
public static <T extends PsiElement> T getPrevSiblingOfType(@Nullable PsiElement sibling, @NotNull Class<T> 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 extends PsiElement> E getStubOrPsiParentOfType(@Nullable PsiElement element, final Class<E> parentClass) {
public static <E extends PsiElement> E getStubOrPsiParentOfType(@Nullable PsiElement element, @NotNull Class<E> parentClass) {
if (element instanceof StubBasedPsiElement) {
StubBase stub = (StubBase)((StubBasedPsiElement)element).getStub();
if (stub != null) {