Reputation: 59
I have been stuck on a particular problem that is to extract an infixexpression from a VariableDeclarationStatement. for example:
String s = 'a'+'b'+'c';
This is an instance of VariableDeclarationStatement. and i need to get the infixexpression 'a'+'b'+'c'
out of it.
I have tried : 1.Tried converting to string.But no conversion back is possible.
2.Tried converting to list but still not possible.
I have tried above methods to try and manipulate and extract InfixExpression out of it.Please help me.
EDIT
here is what i have done :
if (node instanceof InfixExpression) {
infixExpression= (InfixExpression) node;
} else if (node.getParent() instanceof InfixExpression) {
infixExpression= (InfixExpression) node.getParent();
} else { //while trying to get this proposal with spaces its reaching here.
String nodeString =node.toString();
String infixExp="s";
int t;
for (t=0;nodeString.charAt(t)!='=';t++);
infixExp.concat(nodeString.substring(t+1, nodeString.length()));
infixExpression = (InfixExpression)infixExp; //this cast doesn't work here
}
Upvotes: 0
Views: 73
Reputation: 8576
Its still unclear what you want to do.
public class InfixVisitor extends ASTVisitor {
@Override
public boolean visit(InfixExpression node) {
// NOTE: node.toString() should only be used debugging.
// Probably you could use it anyway.
...
return super.visit(node);
}
}
With above visitor you can access all InfixExpression
nodes with e.g.:
ASTNode sourceNode = ...
sourceNode.accept(new InfixVisitor());
Upvotes: 1