Class TreeWalker

java.lang.Object
org.glassfish.pfl.dynamic.codegen.impl.TreeWalker
All Implemented Interfaces:
Visitor
Direct Known Subclasses:
ASMByteCodeVisitor, ASMSetupVisitor, NopVisitor, SourceExpressionVisitor, SourceStatementVisitor

public abstract class TreeWalker extends Object implements Visitor
This is a general purpose utility that does a complete traversal of a Node tree. A stack of Visitors is maintained. The current Visitor on top of the stack is applied to each node. Pre and post methods are provided for each type. The default implementations of these methods delegate to the pre and post methods of the superclass of the node type, so only the required pre and post methods need be overridden.

This is used as follows:

     TreeWalkerContext context = new TreeWalkerContext() ;
     Visitor visitor = new SubclassOfTreeWalker( context, ... ) ;
     context.push( visitor ) ;
     node.accept( visitor ) ;
 
Note that this allows the temporary changing of the current visitor while traversing the tree. The fact that the context support mark and popMark operations makes it easy to process all of the children of a node in any order: just call context.mark() in a preXXX method, push appropriate visitors in the intermediate xXXBeforeYYY methods, and then call context.popMark in the postXXX method.

Note that all preXXX methods return a boolean which indicates whether or not this node should be traversed. If true is return, any child nodes are traversed, and the postXXX method is called. If false is returned, the visitXXX method completes.

If the preXXX method returns true, the postXXX method is always called. If the preXXX method throws an exception or returns false, the postXXX method is not called.

The more complex node types also include intermediate control methods that can be overridden to affect the traversal.