thSoft
thSoft

Reputation: 22660

How to disambiguate between FieldAccess and QualifiedName?

I'm generating Java code as string, and I have expressions like

parameter.field.method();

Then I parse the generated code to AST with Eclipse JDT's ASTParser, and the subexpression

parameter.field

becomes a QualifiedName, not a FieldAccess. This causes problems because later I clean up the qualified names (using code from the Clean Qualified Types Plugin).

As the JavaDoc of FieldAccess states:

An expression like "foo.bar" can be represented either as a qualified name (QualifiedName) or as a field access expression (FieldAccess) containing simple names. Either is acceptable, and there is no way to choose between them without information about what the names resolve to (ASTParser may return either).

What should I generate so that the parser can know unambiguously that this is a FieldAccess?

(An interesting side-question: how does the Java compiler disambiguate between the possibilities? Whenever it encounters a QualifiedName, it also tries to interpret it as a FieldAccess by resolving the names?)

Upvotes: 1

Views: 103

Answers (1)

thSoft
thSoft

Reputation: 22660

The minimal solution I found is to generate parentheses around the parameter:

(parameter).field.method();

Upvotes: 1

Related Questions