Search code examples
eclipseeclipse-jdt

How to know if a PostfixExpression belongs to a for statement?


I'm using eclipse parser to work with expressions and statements in java code.

I have a function:

    public boolean visit(PostfixExpression node)

which deals with Postfix expressoins, such ass i++;

Problem is i want to distinguish between a for statement postfix, and other postfixes. I thought maybe i could get to the node's parent and somehow check if it's a for. Something like node.getParent()... but node.getParent() doesn't return an expression.

Any ideas how to recognize if the PostfixExpression belongs to a for loop?

Thanks

edit:

By "for statement postfix" i mean the postfix in the for loop's first line. Such as:

for(i=0;i<10;i++)

So i want to distinguish this i++ from other i++'s.


Solution

  • I solved this by creating a for_updaters List (using node.updaters()) and updating it every time i visit a for loop (could also be nested loops). Also, whenever i come across a PostfixExpression (including for updaters), i add it to another List, and then delete from this List all similar occurrences that appear in for_updaters List. This way i'm only left with non-for-updaters Postfixes. This also works for me because every time i visit a for loop i clear both Lists, so no worries about duplicate variable names.

    Note: node.updaters() returns the exact full expression: [i++]. But i only need i. It's easy to extract it by converting the updater to String and then use substring().