org.hibernate.hql.ast.util

Class ASTUtil


public final class ASTUtil
extends java.lang.Object

Provides utility methods for AST traversal and manipulation.
Author:
Joshua Davis

Nested Class Summary

static interface
ASTUtil.FilterPredicate
Filters nodes out of a tree.
static class
ASTUtil.IncludePredicate
A predicate that uses inclusion, rather than exclusion semantics.

Method Summary

static void
appendSibling(AST n, AST s)
static List
collectChildren(AST root, ASTUtil.FilterPredicate predicate)
static AST
create(ASTFactory astFactory, int type, String text)
Creates a single node AST.
static AST
createBinarySubtree(ASTFactory factory, int parentType, String parentText, AST child1, AST child2)
Creates a 'binary operator' subtree, given the information about the parent and the two child nodex.
static AST
createParent(ASTFactory factory, int parentType, String parentText, AST child)
Creates a single parent of the specified child (i.e.
static AST
createSibling(ASTFactory astFactory, int type, String text, AST prevSibling)
Creates a single node AST as a sibling.
static AST
createTree(ASTFactory factory, AST[] nestedChildren)
static AST
findPreviousSibling(AST parent, AST child)
Find the previous sibling in the parent for the given child.
static AST
findTypeInChildren(AST parent, int type)
Finds the first node of the specified type in the chain of children.
static String
getDebugString(AST n)
Returns the 'list' representation with some brackets around it for debugging.
static AST
getLastChild(AST n)
Returns the last direct child of 'n'.
static String
getPathText(AST n)
static boolean
hasExactlyOneChild(AST n)
static void
insertChild(AST parent, AST child)
Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
static AST
insertSibling(AST node, AST prevSibling)
static boolean
isDirectChild(AST fixture, AST test)
Determine if a given node (test) is a direct (throtle to one level down) child of another given node (fixture).
static boolean
isSubtreeChild(AST fixture, AST test)
Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).
static void
makeSiblingOfParent(AST parent, AST child)
Makes the child node a sibling of the parent, reconnecting all siblings.

Method Details

appendSibling

public static void appendSibling(AST n,
                                 AST s)

collectChildren

public static List collectChildren(AST root,
                                   ASTUtil.FilterPredicate predicate)

create

public static AST create(ASTFactory astFactory,
                         int type,
                         String text)
Creates a single node AST.
Parameters:
astFactory - The factory.
type - The node type.
text - The node text.
Returns:
AST - A single node tree.

createBinarySubtree

public static AST createBinarySubtree(ASTFactory factory,
                                      int parentType,
                                      String parentText,
                                      AST child1,
                                      AST child2)
Creates a 'binary operator' subtree, given the information about the parent and the two child nodex.
Parameters:
factory - The AST factory.
parentType - The type of the parent node.
parentText - The text of the parent node.
child1 - The first child.
child2 - The second child.
Returns:
AST - A new sub-tree of the form "(parent child1 child2)"

createParent

public static AST createParent(ASTFactory factory,
                               int parentType,
                               String parentText,
                               AST child)
Creates a single parent of the specified child (i.e. a 'unary operator' subtree).
Parameters:
factory - The AST factory.
parentType - The type of the parent node.
parentText - The text of the parent node.
child - The child.
Returns:
AST - A new sub-tree of the form "(parent child)"

createSibling

public static AST createSibling(ASTFactory astFactory,
                                int type,
                                String text,
                                AST prevSibling)
Creates a single node AST as a sibling.
Parameters:
astFactory - The factory.
type - The node type.
text - The node text.
prevSibling - The previous sibling.
Returns:
AST - A single node tree.

createTree

public static AST createTree(ASTFactory factory,
                             AST[] nestedChildren)

findPreviousSibling

public static AST findPreviousSibling(AST parent,
                                      AST child)
Find the previous sibling in the parent for the given child.
Parameters:
parent - the parent node
child - the child to find the previous sibling of
Returns:
the previous sibling of the child

findTypeInChildren

public static AST findTypeInChildren(AST parent,
                                     int type)
Finds the first node of the specified type in the chain of children.
Parameters:
parent - The parent
type - The type to find.
Returns:
The first node of the specified type, or null if not found.

getDebugString

public static String getDebugString(AST n)
Returns the 'list' representation with some brackets around it for debugging.
Parameters:
n - The tree.
Returns:
The list representation of the tree.

getLastChild

public static AST getLastChild(AST n)
Returns the last direct child of 'n'.
Parameters:
n - The parent
Returns:
The last direct child of 'n'.

getPathText

public static String getPathText(AST n)

hasExactlyOneChild

public static boolean hasExactlyOneChild(AST n)

insertChild

public static void insertChild(AST parent,
                               AST child)
Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
Parameters:
parent - the parent
child - the new first child

insertSibling

public static AST insertSibling(AST node,
                                AST prevSibling)

isDirectChild

public static boolean isDirectChild(AST fixture,
                                    AST test)
Determine if a given node (test) is a direct (throtle to one level down) child of another given node (fixture).
Parameters:
fixture - The node against which to testto be checked for children.
test - The node to be tested as being a child of the parent.
Returns:
True if test is contained in the fixtures's direct children; false otherwise.

isSubtreeChild

public static boolean isSubtreeChild(AST fixture,
                                     AST test)
Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).
Parameters:
fixture - The node against which to testto be checked for children.
test - The node to be tested as being a subtree child of the parent.
Returns:
True if child is contained in the parent's collection of children.

makeSiblingOfParent

public static void makeSiblingOfParent(AST parent,
                                       AST child)
Makes the child node a sibling of the parent, reconnecting all siblings.
Parameters:
parent - the parent
child - the child