abstractpublic class HierarchicalASTVisitor extends ASTVisitor(Code)
This class provides a convenient behaviour-only
extension mechanism for the ASTNode hierarchy.
If you feel like you would like to add a method to
the ASTNode hierarchy (or a subtree of the hierarchy),
and you want to have different implementations
of it at different points in the hierarchy,
simply create a HierarchicalASTVisitor representing
the new method and all its implementations,
locating each implementation within the right
visit(XX) method. If you wanted to add a method implementation to abstract
class Foo, an ASTNode descendant, put your implementation in visit(Foo).
This class will provide appropriate dispatch, just as if the method
implementations had been added to the ASTNode hierarchy.
Details:
This class has a visit(XX node) method for every for every
class (concrete or abstract) XX in the ASTNode hierarchy. In this class'
default implementations of these methods, the method corresponding to a given
ASTNode descendant class will call (and return the return value of) the
visit(YY) method for it's superclass YY, with the exception of the
visit(ASTNode) method which simply returns true, since ASTNode doesn't have a
superclass that is within the ASTNode hierarchy.
Because of this organization, when visit(XX) methods are overridden in a
subclass, and the visitor is applied to a node, only the most specialized
overridden method implementation for the node's type will be called, unless
this most specialized method calls other visit methods (this is discouraged)
or, (preferably) calls super.visit(XX node), (the reference type of the
parameter must be XX) which will invoke this class' implementation of the
method, which will, in turn, invoke the visit(YY) method corresponding to the
superclass, YY.
Thus, the dispatching behaviour achieved when
HierarchicalASTVisitors' visit(XX) methods, corresponding to a particular
concrete or abstract ASTNode descendant class, are overridden is exactly
analogous to the dispatching behaviour obtained when method implementations
are added to the same ASTNode descendant classes.