Nih*_*oti 5 java compiler-construction static-analysis javac
我实现了一个程序TreeScanner,用于打印AST中所有节点的信息.该程序支持所有类型(所有访问方法都已实现).但是,问题是对于语句,System.out.println(object.YYY);程序不访问字段引用YYY.
它将对象检测为标识符,但无法将YYY检测为标识符.但是,当我有System.out.println(YYY); 然后visitIdentifier将访问YYY.
请让我知道上面这两行之间的区别是什么,而在一个YYY中访问者是visitidentifier,在另一个案例中它没有访问过.
我如何可以访问YYY在object.YYY?
在类org.eclipse.jdt.core.dom中我们有FieldAccess,在上面两种情况下都为YYY调用,但似乎Javac中的TreeScanner没有类似的方法.
该visitIdentifier方法在 AST 中的标识符注释上调用,这些标识符注释是在将标识符用作表达式时创建的。然而,Java 中成员选择的语法是<expression>.<identifier>, not <expression>.<expression>,这意味着YYYinobject.YYY不是子表达式,因此没有自己的子树。相反,MemberSelectTreeforobject.YYY只是包含YYY为可Name直接访问的 via getIdentifier()。中没有visitName方法TreeScanner,因此到达YYY这里的唯一方法是直接从此处执行此操作visitMemberSelect。
以下是object.YYY使用以下方法进行打印的方法visitMemberSelect:
Void visitMemberSelect(MemberSelectTree memberSelect, Void p) {
// Print the object
memberSelect.getExpression().accept(this, p);
System.out.print(".");
// Print the name of the member
System.out.print(memberSelect.getIdentifier());
}
Run Code Online (Sandbox Code Playgroud)