简体   繁体   English

AST:访问和仅使用查询方法有什么区别?

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

For instance, I get a CompilationUnit from the ASTParser. 例如,我从ASTParser获取一个CompilationUnit。 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. 从这里开始,我一直沿AST走下去,直到像这样的叶子节点为止,而无需使用访问。 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). 访客( ASTVisitor子类型)的目的是遍历整个AST,因此您可以轻松检查每个AST节点(除非visit方法返回false,这时将跳过当前节点下方的子树)。

When directly querying the AST, you are responsible of traversing to all interesting nodes. 直接查询AST时,您有责任遍历所有有趣的节点。

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. 使用unit.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). 因此,在需要编写的代码数量和代码的清晰度方面存在差异(训练有素的眼睛会立即理解访问者的用法,但需要仔细阅读手动遍历)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中的块和方法有什么区别? - What's the difference between blocks and methods in Java? “configure”和“configureGlobal”方法有什么区别? - What's the difference between 'configure' and 'configureGlobal' methods? 这两种通用方法有什么区别? - What's difference between these two generic methods? Calendar 的 getActualMinimum 和 getGreatestMinimum 方法有什么区别? - What's the difference between Calendar's getActualMinimum and getGreatestMinimum methods? EasyMock 的.andReturn 和.andAnswer 方法有什么区别? - What's the difference between EasyMock's .andReturn and .andAnswer methods? Java向量方法set()和setElementAt()有什么区别? - What's the difference between the java vector methods set() and setElementAt()? Java 8 中的 map() 和 flatMap() 方法有什么区别? - What's the difference between map() and flatMap() methods in Java 8? servlet中的findAttribute()和getAttribute()方法有什么区别? - What's the difference between the methods findAttribute() and getAttribute() in servlet? HttpServletRequest中getRequestURI和getPathInfo方法之间的区别是什么? - What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest? 关于级联,休眠中的 save() 和 persists() 方法有什么区别? - what's the difference between save() and persists() methods in hibernate regarding with cascading?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM