In Eclipse JDT Java parser, is it possible to traverse the AST node by node without the using of visitors?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 14:21:19

问题


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

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.

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?


回答1:


If you don't care about node types, override any of ASTVisitor.preVisit(ASTNode), ASTVisitor.preVisit2(ASTNode), ASTVisitor.postVisit(ASTNode).



来源:https://stackoverflow.com/questions/51163199/in-eclipse-jdt-java-parser-is-it-possible-to-traverse-the-ast-node-by-node-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!