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
.
JAVADOC,MODIFIERS,NAME,SUPER_INTERFACE_TYPES,ENUM_CONSTANTS和BODY_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? Is it possible to differentiate?
当我访问EnumDeclaration的子节点(例如,一个SimpleName节点)时,是否可以知道我正在访问哪些节点列表?是否可以区分?
I'd like to process a node differently, depending on whether I found it in ENUM_CONSTANTS
or BODY_DECLARATIONS
.
我想以不同的方式处理节点,具体取决于我是在ENUM_CONSTANTS还是BODY_DECLARATIONS中找到它。
3 个解决方案
#1
2
I found a solution. Explicitly visiting the nodes in the list (WITH accept()
, not visit()
). Something like (for visiting the super interfaces):
我找到了解决方案。显式访问列表中的节点(WITH accept(),而不是visit())。类似的东西(用于访问超级界面):
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是一个伞形抽象类,不存在访问(类型节点)实现。
This also forces the children of the nodes in the superInterfaces
list to be visited as soon as their parent is visited. Problem solved.
这也会强制访问其父级时访问superInterfaces列表中的节点的子节点。问题解决了。
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来禁止访问者重新访问其子节点。
#2
1
Your nodes should invoke corresponding methods.
您的节点应该调用相应的方法。
MODIFIERS -> visitModifiers
NAME -> visitNAME
and so on
等等
#3
0
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
#1
2
I found a solution. Explicitly visiting the nodes in the list (WITH accept()
, not visit()
). Something like (for visiting the super interfaces):
我找到了解决方案。显式访问列表中的节点(WITH accept(),而不是visit())。类似的东西(用于访问超级界面):
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是一个伞形抽象类,不存在访问(类型节点)实现。
This also forces the children of the nodes in the superInterfaces
list to be visited as soon as their parent is visited. Problem solved.
这也会强制访问其父级时访问superInterfaces列表中的节点的子节点。问题解决了。
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来禁止访问者重新访问其子节点。
#2
1
Your nodes should invoke corresponding methods.
您的节点应该调用相应的方法。
MODIFIERS -> visitModifiers
NAME -> visitNAME
and so on
等等
#3
0
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