在Eclipse JDT Java解析器中,是否可以逐节遍历AST节点而无需使用访问者? [英] In Eclipse JDT Java parser, is it possible to traverse the AST node by node without the using of visitors?
问题描述
通过Eclipse JDT API访问节点信息的标准方法是使用Visitor的模式。例如:
The standard way to access information on Nodes through the Eclipse JDT API is with Visitor's pattern. For example:
unit.accept(new MyVisitorAdapter<Object>() {
@Override public void visit(MethodCallExpr node, Object arg) {
System.out.println("found method call: " + node.toString());
}
}, null);
在这种情况下,要访问节点,我需要指定我感兴趣的节点类型( 此案例的MethodCallExpr
。但是,为了继续以通用方式访问节点信息,我应该覆盖所有 visit()
方法,可能枚举Eclipse JDT API中可用的每种节点。找到它的完整示例这里。
In this case, to visit a node I need to specify what type of node I'm interested (MethodCallExpr
for this case). But, to proceed to access node information in a generic way, I should override all the visit()
method, potentially enumerating every kind of node available in the Eclipse JDT API. A full example of where it is done is found here.
在这种情况下,尽管不完全属于代码覆盖范围的相同域,但我希望能够控制遍历由Eclipse JDT Java Parser完成。我想通过AST节点,可能通过所有这些节点,选择我想要的,但不限制类型,如上面的代码所示。可能吗?是否有通过Eclipse JDT API执行此操作的标准方法?
In this context, although not exactly in the same domain of Code Coverage, I would like to have control over the traversal done by the Eclipse JDT Java Parser. I would like to walk through the AST nodes, potentially passing by all of them, selecting what I want, but without to restrict to a type, as shown in the code above. Is it possible? Is there a standard way to do that through the Eclipse JDT API?
推荐答案
如果您不关心节点类型,请覆盖任何 ASTVisitor.preVisit(ASTNode)
, ASTVisitor.preVisit2(ASTNode)
, ASTVisitor。 postVisit(ASTNode)
。
If you don't care about node types, override any of ASTVisitor.preVisit(ASTNode)
, ASTVisitor.preVisit2(ASTNode)
, ASTVisitor.postVisit(ASTNode)
.
这篇关于在Eclipse JDT Java解析器中,是否可以逐节遍历AST节点而无需使用访问者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!