Top Description Inners Constructors Methods
org.hibernate.hql.internal.ast.util

public final Class ASTUtil

extends Object
Class Inheritance
Imports
java.lang.reflect.Field, .Modifier, java.util.ArrayList, .HashMap, .List, .Map, org.hibernate.HibernateError, antlr.ASTFactory, antlr.collections.AST, antlr.collections.impl.ASTArray

Provides utility methods for AST traversal and manipulation.
Authors
Joshua Davis, Steve Ebersole

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static interface
ASTUtil.FilterPredicate

Filters nodes out of a tree.

public abstract static class
ASTUtil.IncludePredicate

A predicate that uses inclusion, rather than exclusion semantics.

Constructor Summary

AccessConstructor and Description
private
ASTUtil()

Deprecated (tellclovertoignorethis)
Disallow instantiation.

Method Summary

Modifier and TypeMethod and Description
public static void
appendChild(AST parent, AST child)

public static void
public static List<E>
public static AST

Returns:

AST - A single node tree.
create
(ASTFactory
The factory.
astFactory
,
int
The node type.
type
,
String
The node text.
text
)

Deprecated silly
Creates a single node AST.
private static ASTArray
createAstArray(ASTFactory factory, int size, int parentType, String parentText, AST child1)

public static AST

Returns:

AST - A new sub-tree of the form "(parent child1 child2)"
createBinarySubtree
(ASTFactory
The AST factory.
factory
,
int
The type of the parent node.
parentType
,
String
The text of the parent node.
parentText
,
AST
The first child.
child1
,
AST
The second child.
child2
)

Creates a 'binary operator' subtree, given the information about the parent and the two child nodes.

public static AST

Returns:

AST - A new sub-tree of the form "(parent child)"
createParent
(ASTFactory
The AST factory.
factory
,
int
The type of the parent node.
parentType
,
String
The text of the parent node.
parentText
,
AST
The child.
child
)

Creates a single parent of the specified child (i.e. a 'unary operator' subtree).

public static AST

Returns:

The created AST node.
createSibling
(ASTFactory
The factory.
astFactory
,
int
The node type.
type
,
String
The node text.
text
,
AST
The previous sibling.
prevSibling
)

Creates a single node AST as a sibling of the passed prevSibling, taking care to reorganize the tree correctly to account for this newly created node.

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

private static Integer
public static AST

Returns:

the previous sibling of the child
findPreviousSibling
(AST
the parent node
parent
,
AST
the child to find the previous sibling of
child
)

Find the previous sibling in the parent for the given child.

public static AST

Returns:

The first node of the specified type, or null if not found.
findTypeInChildren
(AST
The parent
parent
,
int
The type to find.
type
)

Finds the first node of the specified type in the chain of children.

public static String[]

Returns:

A compact map int -> tokenName in array format
generateTokenNameCache
(Class<T>
The *TokenTypes interface (or implementor of said interface).
tokenTypeInterface
)

Method to generate a map of token type names, keyed by their token type values.

public static String

Returns:

The token type name, *or* the integer value if the name could not be found.
getConstantName
(Class<T>
The class which defines the constant
owner
,
int
The value of the constant.
value
)

Deprecated Use #getTokenTypeName instead
Get the name of a constant defined on the given class which has the given value.
public static String

Returns:

The list representation of the tree.
getDebugString
(AST
The tree.
n
)

Returns the 'list' representation with some brackets around it for debugging.

public static AST

Returns:

The last direct child of 'n'.
getLastChild
(AST
The parent
n
)

Returns the last direct child of 'n'.

private static AST

Returns:

The last sibling of 'a'.
getLastSibling
(AST
The sibling.
a
)

Returns the last sibling of 'a'.

public static String
private static void
public static String

Returns:

The corresponding name.
getTokenTypeName
(Class<T>
The *TokenTypes interface (or one of its implementors).
tokenTypeInterface
,
int
The token type value.
tokenType
)

Intended to retrieve the name of an AST token type based on the token type interface.

public static boolean
public static void
insertChild(AST
the parent
parent
,
AST
the new first child
child
)

Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.

public static AST

Returns:

The return is the same as the node parameter passed in.
insertSibling
(AST
The node to insert
node
,
AST
The previous node at the sibling position where we want this node inserted.
prevSibling
)

Inserts a node into a child subtree as a particularly positioned sibling taking care to properly reorganize the tree to account for this new addition.

public static boolean

Returns:

True if child is contained in the parent's collection of children.
isSubtreeChild
(AST
The node against which to test to be checked for children.
fixture
,
AST
The node to be tested as being a subtree child of the parent.
test
)

Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).

public static void
makeSiblingOfParent(AST
the parent
parent
,
AST
the child
child
)

Makes the child node a sibling of the parent, reconnecting all siblings.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ASTUtilback to summary
private ASTUtil()

Deprecated

(tellclovertoignorethis)

Disallow instantiation.

Annotations
@Deprecated

Method Detail

appendChildback to summary
public static void appendChild(AST parent, AST child)
appendSiblingback to summary
public static void appendSibling(AST n, AST s)
collectChildrenback to summary
public static List<E> collectChildren(AST root, ASTUtil.FilterPredicate predicate)
createback to summary
public static AST create(ASTFactory astFactory, int type, String text)

Deprecated

silly

Creates a single node AST.

TODO : this is silly, remove it...

Parameters
astFactory:ASTFactory

The factory.

type:int

The node type.

text:String

The node text.

Returns:AST

AST - A single node tree.

Annotations
@Deprecated
createAstArrayback to summary
private static ASTArray createAstArray(ASTFactory factory, int size, int parentType, String parentText, AST child1)
createBinarySubtreeback to summary
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 nodes.

Parameters
factory:ASTFactory

The AST factory.

parentType:int

The type of the parent node.

parentText:String

The text of the parent node.

child1:AST

The first child.

child2:AST

The second child.

Returns:AST

AST - A new sub-tree of the form "(parent child1 child2)"

createParentback to summary
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:ASTFactory

The AST factory.

parentType:int

The type of the parent node.

parentText:String

The text of the parent node.

child:AST

The child.

Returns:AST

AST - A new sub-tree of the form "(parent child)"

createSiblingback to summary
public static AST createSibling(ASTFactory astFactory, int type, String text, AST prevSibling)

Creates a single node AST as a sibling of the passed prevSibling, taking care to reorganize the tree correctly to account for this newly created node.

Parameters
astFactory:ASTFactory

The factory.

type:int

The node type.

text:String

The node text.

prevSibling:AST

The previous sibling.

Returns:AST

The created AST node.

createTreeback to summary
public static AST createTree(ASTFactory factory, AST[] nestedChildren)
extractIntegerValueback to summary
private static Integer extractIntegerValue(Field field)
findPreviousSiblingback to summary
public static AST findPreviousSibling(AST parent, AST child)

Find the previous sibling in the parent for the given child.

Parameters
parent:AST

the parent node

child:AST

the child to find the previous sibling of

Returns:AST

the previous sibling of the child

findTypeInChildrenback to summary
public static AST findTypeInChildren(AST parent, int type)

Finds the first node of the specified type in the chain of children.

Parameters
parent:AST

The parent

type:int

The type to find.

Returns:AST

The first node of the specified type, or null if not found.

generateTokenNameCacheback to summary
public static String[] generateTokenNameCache(Class<T> tokenTypeInterface)

Method to generate a map of token type names, keyed by their token type values.

Parameters
tokenTypeInterface:Class<T>

The *TokenTypes interface (or implementor of said interface).

Returns:String[]

A compact map int -> tokenName in array format

getConstantNameback to summary
public static String getConstantName(Class<T> owner, int value)

Deprecated

Use #getTokenTypeName instead

Get the name of a constant defined on the given class which has the given value.

Note, if multiple constants have this value, the first will be returned which is known to be different on different JVM implementations.

Parameters
owner:Class<T>

The class which defines the constant

value:int

The value of the constant.

Returns:String

The token type name, *or* the integer value if the name could not be found.

Annotations
@Deprecated
getDebugStringback to summary
public static String getDebugString(AST n)

Returns the 'list' representation with some brackets around it for debugging.

Parameters
n:AST

The tree.

Returns:String

The list representation of the tree.

getLastChildback to summary
public static AST getLastChild(AST n)

Returns the last direct child of 'n'.

Parameters
n:AST

The parent

Returns:AST

The last direct child of 'n'.

getLastSiblingback to summary
private static AST getLastSibling(AST a)

Returns the last sibling of 'a'.

Parameters
a:AST

The sibling.

Returns:AST

The last sibling of 'a'.

getPathTextback to summary
public static String getPathText(AST n)
getPathTextback to summary
private static void getPathText(StringBuilder buf, AST n)
getTokenTypeNameback to summary
public static String getTokenTypeName(Class<T> tokenTypeInterface, int tokenType)

Intended to retrieve the name of an AST token type based on the token type interface. However, this method can be used to look up the name of any constant defined on a class/interface based on the constant value. Note that if multiple constants have this value, the first will be returned which is known to be different on different JVM implementations.

Parameters
tokenTypeInterface:Class<T>

The *TokenTypes interface (or one of its implementors).

tokenType:int

The token type value.

Returns:String

The corresponding name.

hasExactlyOneChildback to summary
public static boolean hasExactlyOneChild(AST n)
insertChildback to summary
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:AST

the parent

child:AST

the new first child

insertSiblingback to summary
public static AST insertSibling(AST node, AST prevSibling)

Inserts a node into a child subtree as a particularly positioned sibling taking care to properly reorganize the tree to account for this new addition.

Parameters
node:AST

The node to insert

prevSibling:AST

The previous node at the sibling position where we want this node inserted.

Returns:AST

The return is the same as the node parameter passed in.

isSubtreeChildback to summary
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:AST

The node against which to test to be checked for children.

test:AST

The node to be tested as being a subtree child of the parent.

Returns:boolean

True if child is contained in the parent's collection of children.

makeSiblingOfParentback to summary
public static void makeSiblingOfParent(AST parent, AST child)

Makes the child node a sibling of the parent, reconnecting all siblings.

Parameters
parent:AST

the parent

child:AST

the child

org.hibernate.hql.internal.ast.util back to summary

private Class ASTUtil.CollectingNodeVisitor

extends Object
implements VisitationStrategy
Class Inheritance
  • java.lang.Object
  • org.hibernate.hql.internal.ast.util.ASTUtil.CollectingNodeVisitor
All Implemented Interfaces
org.hibernate.hql.internal.ast.util.NodeTraverser.VisitationStrategy

Field Summary

Modifier and TypeField and Description
private final List<E>
private final ASTUtil.FilterPredicate

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public List<E>
collect(AST root)

public List<E>
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

collectedNodesback to summary
private final List<E> collectedNodes
predicateback to summary
private final ASTUtil.FilterPredicate predicate

Constructor Detail

CollectingNodeVisitorback to summary
public CollectingNodeVisitor(ASTUtil.FilterPredicate predicate)

Method Detail

collectback to summary
public List<E> collect(AST root)
getCollectedNodesback to summary
public List<E> getCollectedNodes()
visitback to summary
public void visit(AST node)

Implements org.hibernate.hql.internal.ast.util.NodeTraverser.VisitationStrategy.visit.

Annotations
@Override
org.hibernate.hql.internal.ast.util back to summary

public Interface ASTUtil.FilterPredicate

Known Direct Implementers
org.hibernate.hql.internal.ast.util.ASTUtil.IncludePredicate

Filters nodes out of a tree.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the node should be filtered out, false to keep the node.
exclude
(AST
The node.
n
)

Returns true if the node should be filtered out.

Method Detail

excludeback to summary
public boolean exclude(AST n)

Returns true if the node should be filtered out.

Parameters
n:AST

The node.

Returns:boolean

true if the node should be filtered out, false to keep the node.

org.hibernate.hql.internal.ast.util back to summary

public abstract Class ASTUtil.IncludePredicate

extends Object
implements FilterPredicate
Class Inheritance
All Implemented Interfaces
org.hibernate.hql.internal.ast.util.ASTUtil.FilterPredicate

A predicate that uses inclusion, rather than exclusion semantics.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final boolean
exclude(AST
The node.
node
)

Implements org.hibernate.hql.internal.ast.util.ASTUtil.FilterPredicate.exclude.

Returns true if the node should be filtered out.
public abstract boolean
include(AST node)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

IncludePredicateback to summary
public IncludePredicate()

Method Detail

excludeback to summary
public final boolean exclude(AST node)

Implements org.hibernate.hql.internal.ast.util.ASTUtil.FilterPredicate.exclude.

Doc from org.hibernate.hql.internal.ast.util.ASTUtil.FilterPredicate.exclude.

Returns true if the node should be filtered out.

Parameters
node:AST

The node.

Returns:boolean

true if the node should be filtered out, false to keep the node.

Annotations
@Override
includeback to summary
public abstract boolean include(AST node)