5

I implemented a program TreeScanner, to print information about all nodes in AST. The program supports all types (all visit methods are implemented). However, the problem is that for the statement, System.out.println(object.YYY); the program does not visit field reference YYY.

It detects object as identifier, but can not detect YYY as identifier. However, when I have System.out.println(YYY); then visitIdentifier will visit YYY.

Please let me know what is the difference between the both above lines, while in one YYY is visited by visitidentifier, in the another case it is not visited.

How can I visit YYY in object.YYY?

In class org.eclipse.jdt.core.dom we have FieldAccess which is called in both above cases for YYY, but seems TreeScanner in Javac has no similar method.

5
  • 2
    It might help to post some code... Commented Jan 7, 2015 at 12:15
  • Is YYY a static final (i.e. constant) field? Commented Jan 7, 2015 at 12:51
  • I checked all these cases: static, static final, and normal field. In all three cases it could not detect YYY as identifier. Commented Jan 7, 2015 at 12:54
  • Odd. If it was a constant, it might be an optimization where javac returns the value of the constant. But if it always happens ... Commented Jan 7, 2015 at 13:01
  • 1
    Can you post a working example? Commented Jan 7, 2015 at 13:02

1 Answer 1

1

The visitIdentifier method gets called on Identifier notes in the AST, which are created when an identifier is used as an expression. However the syntax for member selection in Java is <expression>.<identifier>, not <expression>.<expression>, meaning the YYY in object.YYY is not a sub-expression and thus doesn't get its own subtree. Instead the MemberSelectTree for object.YYY simply contains YYY as a Name directly, accessible via getIdentifier(). There is no visitName method in TreeScanner, so the only way to get at YYY here would be to do so from visitMemberSelect directly.

Here's how you'd print object.YYY using 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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.