From 6466419b1a200001dd3ae6d6c239e96c22cf7070 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Tue, 30 Sep 2003 14:04:09 +0000 Subject: [PATCH] Added Jeff Anderson's node finding utilities git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2286 51baf565-9d33-0410-a72c-fc3788e3496d --- .../net/sourceforge/pmd/ast/SimpleNode.java | 35 +++++++++++++++++++ pmd/xdocs/credits.xml | 1 + 2 files changed, 36 insertions(+) diff --git a/pmd/src/net/sourceforge/pmd/ast/SimpleNode.java b/pmd/src/net/sourceforge/pmd/ast/SimpleNode.java index f4853d4af6..3483510418 100644 --- a/pmd/src/net/sourceforge/pmd/ast/SimpleNode.java +++ b/pmd/src/net/sourceforge/pmd/ast/SimpleNode.java @@ -96,6 +96,41 @@ public class SimpleNode implements Node { return endColumn; } + /** + * Traverses up the tree to find the first parent instance of type parentType + * @param parentType class which you want to find. + * @return Node of type parentType. Returns null if none found. + */ + public Node getFirstParentOfType(Class parentType) { + Node parentNode = jjtGetParent(); + + while (parentNode != null && parentNode.getClass() != parentType) { + parentNode = parentNode.jjtGetParent(); + } + + return parentNode; + } + + /** + * Traverses up the tree to find all of the parent instances of type parentType + * @param parentType classes which you want to find. + * @return List of parentType instances found. + */ + public List getParentsOfType(Class parentType) { + List parents = new ArrayList(); + Node parentNode = jjtGetParent(); + + while (parentNode != null) { + if (parentNode.getClass() == parentType) { + parents.add(parentNode); + } + + parentNode = parentNode.jjtGetParent(); + } + + return parents; + } + public List findChildrenOfType(Class targetType) { List list = new ArrayList(); findChildrenOfType(targetType, list); diff --git a/pmd/xdocs/credits.xml b/pmd/xdocs/credits.xml index 22e0359ee3..31d1ca7d2a 100644 --- a/pmd/xdocs/credits.xml +++ b/pmd/xdocs/credits.xml @@ -38,6 +38,7 @@