IDEA-66686 (wrong lookup method used)

Distinction in PsiTreeUtil's get/find methods documented
This commit is contained in:
Roman Shevchenko
2011-03-17 15:43:09 +01:00
parent 68e4ecbeb6
commit 09eb2ae423
2 changed files with 25 additions and 4 deletions
@@ -94,7 +94,7 @@ public class PsiTryStatementImpl extends CompositePsiElement implements PsiTrySt
@Override
public PsiResourceList getResourceList() {
return PsiTreeUtil.findChildOfType(this, PsiResourceList.class);
return PsiTreeUtil.getChildOfType(this, PsiResourceList.class);
}
public ASTNode findChildByRole(int role) {
@@ -200,13 +200,15 @@ public class PsiTreeUtil {
@Nullable
public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element, @NotNull final Class<T> aClass) {
return findChildOfType(element, aClass, true);
//noinspection unchecked
return findChildOfAnyType(element, true, aClass);
}
@Nullable
public static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element,
@NotNull final Class<T> aClass,
final boolean strict) {
//noinspection unchecked
return findChildOfAnyType(element, strict, aClass);
}
@@ -215,6 +217,15 @@ public class PsiTreeUtil {
return findChildOfAnyType(element, true, classes);
}
/**
* Recursive (depth first) search for first element of any of given {@code classes}.
*
* @param element a PSI element to start search from.
* @param strict if false the {@code element} is also included in the search.
* @param classes element types to search for.
* @param <T> type to cast found element to.
* @return first found element, or null if nothing found.
*/
@Nullable
public static <T extends PsiElement> T findChildOfAnyType(@Nullable final PsiElement element,
final boolean strict,
@@ -235,6 +246,14 @@ public class PsiTreeUtil {
return (T)processor.getFoundElement();
}
/**
* Non-recursive search for element of type T amongst given {@code element} children.
*
* @param element a PSI element to start search from.
* @param aClass element type to search for.
* @param <T> element type to search for.
* @return first found element, or null if nothing found.
*/
@Nullable
public static <T extends PsiElement> T getChildOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
if (element == null) return null;
@@ -284,8 +303,10 @@ public class PsiTreeUtil {
}
private static boolean instanceOf(final PsiElement child, final Class<?>... classes) {
for (final Class<?> each : classes) {
if (each.isInstance(child)) return true;
if (classes != null) {
for (final Class<?> each : classes) {
if (each.isInstance(child)) return true;
}
}
return false;
}