简体   繁体   中英

AST: what's the difference between visiting and just using query methods?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM