简体   繁体   English

在AST访问者中,我如何知道我正在访问哪个节点的属性?

[英]In an AST Visitor, how can I know which node's property I am visiting?

I'm programming an AST Visitor (eclipse JDT). 我正在编程AST访问者(eclipse JDT)。

An EnumDeclaration node contains the following structural properties: EnumDeclaration节点包含以下结构属性:

JAVADOC , MODIFIERS , NAME , SUPER_INTERFACE_TYPES , ENUM_CONSTANTS and BODY_DECLARATIONS . JAVADOCMODIFIERSNAMESUPER_INTERFACE_TYPESENUM_CONSTANTSBODY_DECLARATIONS

When I visit a child node of EnumDeclaration (a SimpleName node, for instance), is it possible to know which of the lists of nodes I'm visiting? 当我访问EnumDeclaration的子节点(例如,一个SimpleName节点)时,是否可以知道我正在访问哪些节点列表? Is it possible to differentiate? 是否可以区分?

I'd like to process a node differently, depending on whether I found it in ENUM_CONSTANTS or BODY_DECLARATIONS . 我想以不同的方式处理节点,具体取决于我是在ENUM_CONSTANTS还是BODY_DECLARATIONS找到它。

I found a solution. 我找到了解决方案。 Explicitly visiting the nodes in the list (WITH accept() , not visit() ). 显式访问列表中的节点(WITH accept() ,而不是visit() )。 Something like (for visiting the super interfaces): 类似的东西(用于访问超级界面):

List<Type> superInterfaces = enumDecNode.superInterfaceTypes();
for( Type superInterface: superInterfaces)
    superInterface.accept( this);

Note that it is not possible to use: 请注意,无法使用:

    this.visit( superInterface);

because Type is an umbrella abstract class for which no visit( Type node) implementation exists. 因为Type是一个伞形抽象类,不存在visit( Type node)实现。

This also forces the children of the nodes in the superInterfaces list to be visited as soon as their parent is visited. 这也会强制访问其父级时访问superInterfaces列表中的节点的子节点。 Problem solved. 问题解决了。

On a side note, if you already process all the children of a node via these lists, you can forbid the visitor from re-visiting its children, by returning false. 另外,如果您已经通过这些列表处理了节点的所有子节点,则可以通过返回false来禁止访问者重新访问其子节点。

Your nodes should invoke corresponding methods. 您的节点应该调用相应的方法。

MODIFIERS -> visitModifiers 
NAME -> visitNAME

and so on 等等

Another alternative solution (thanks to Markus Keller @ eclipse JDT forum): 另一种替代解决方案(感谢Markus Keller @ eclipse JDT论坛):

Use "node.getLocationInParent() == EnumDeclaration.NAME_PROPERTY" or other *_PROPERTY constants. 使用“node.getLocationInParent()== EnumDeclaration.NAME_PROPERTY”或其他* _PROPERTY常量。

Markus 马库斯

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

相关问题 我怎么知道我正在运行哪个版本的RichFaces? - How do I know which version of RichFaces I am running? 如何知道我使用的是哪个 servlet 和 JSP 版本? - How to know which servlet and JSP version am I using? 使用节点访问者时,如何获得两个节点之间的不间断空格? - How can I get the non-breaking spaces between two nodes when using a node visitor? 我如何知道 JavaFX 中关注哪个节点? - How do I know which node is focused in JavaFX? 在使用自定义AST节点类型进行树过滤时,如何避免抛出ClassCastException? - How can I avoid ClassCastException being thrown when tree filtering with a custom AST node type? 我们能否直接从Eclipse AST指向一个节点,而不是访问所有节点 - Can we directly point to a node from Eclipse AST instead of visiting all the nodes 如何让我的网站检查访问者的 java 安装并提供更新或安装它? - How can I have my website check the visitor's java installation and offer to update or install it? 我怎么知道当前时间是否是特定时区的凌晨 5 点 - How can i know if current time is 5 am in specific time zone 我怎么知道哪个maven包中包含我要使用的类? - How do I know which maven package contains the class I am going to use? 数组列表中的Java jbutton,我怎么知道单击了哪个 - Java jbutton in an arraylist, how can i know which one is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM