For instance, I get a CompilationUnit from the ASTParser. Why do I need to accept a visitor instead of using the normal methods:
ASTParser parser ... //all that stuff
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
then I do:
unit.types() //get all type declarations
and from here on I just keep going down the AST until the leaf nodes just like that without using visit. What's the advantage of using the visitor pattern over doing what I proposed?
The purpose of visitors (subtypes of ASTVisitor
) is to traverse the entire AST, so you can inspect every single AST node with little effort (unless a visit method returns false, at which point the sub-tree below the current node is skipped).
When directly querying the AST, you are responsible of traversing to all interesting nodes.
When using unit.types()
you only get the top-level type declarations, but using a visitor it's easy to handle all types in the compilation unit including nested types.
When implemented correctly both approaches should exhibit the same behaviour. Thus differences are in the amount of code needing to be written and clarity of code (a trained eye will immediately understand usage of a visitor, but needs to carefully read the manual traversal).