简体   繁体   中英

AST eclipse, trying to create an InfixExpression

I am working on an Eclipse code formatting plugin using the AST.

I am trying to create an InfixExpression using this code:

public static InfixExpression makeInfixExpression(final AST t, final ASTRewrite r, final InfixExpression.Operator o,
        final Expression left, final Expression right) {
    final InfixExpression $ = t.newInfixExpression();
    $.setOperator(o);
    $.setRightOperand(right.getParent() == null ? right : (Expression) r.createMoveTarget(right));
    $.setLeftOperand(left.getParent() == null ? left : (Expression) r.createMoveTarget(left));
    return $;
}

with this command:

makeInfixExpression(t, r, InfixExpression.Operator.PLUS, asgnThen.getRightHandSide(), otherAsgn)

Declarations:

final Assignment asgnThen, final Expression otherAsgn;

But the InfixExpression I am getting back from this function is 0+0 instead of 3 + 4 according to the parameters I sent. after some debugging I saw that it gets messed up in the createMoveTarget part but if i replace it with just right and left it throw an exception.... I also noticed that if I use t.newNumberLiteral to create 2 new lietrals and define the to be the value of what asgnThen.getRightHandSide and otherAsgn is the function returns a proper InfixExpression but the problem is it doesnt have to be a NumberLiteral so I cant count on that. when the plugin lets me see the preview is shows "3 + " and no 0+0.... wierd stuff going on.

Any suggestions?

Thanks!

Found the problem, needed to change the MoveTarget to CopyTarget. I trie to "Move" the expression to 2 places in the conditional but "Move" here means actually moving it and not copying it which means I tried to move the same expression to 2 places but you only have 1 of him so you have to copy it with CopyTarget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM