1

I have a code where I am replacing a logical expression with its negated form. Like, replacing A || B with !A && !B. For this, I am using JDT ASTNode's copySubtree() function to make a copy of the original node. Then modifying the copy and replacing original with it. But after replacing it resolveBinding() method is always returning null. But if I don't make a copy and just modify the original node, then resolveBinding() method works fine. Now, I need to have both the original and the modified node. That's why making a copy of either is necessary. But after making a copy JDT cannot resolve the intrnal variable's bindings anymore.

My function that is negating the simple logical expression is given below:

public ASTNode negateLogicalExpression(ASTNode expression) {
        AST nAST = expression.getAST();
        ASTNode copyExpression = ASTNode.copySubtree(nAST, expression);
        copyExpression.accept(new ASTVisitor() {

            @Override
            public void endVisit(InfixExpression node) {
                InfixExpression.Operator op = node.getOperator();
                if(op.equals(Operator.LESS)) {
                    node.setOperator(Operator.GREATER_EQUALS);
                } else if(op.equals(Operator.LESS_EQUALS)) {
                    node.setOperator(Operator.GREATER);
                } else if(op.equals(Operator.GREATER)) {
                    node.setOperator(Operator.LESS_EQUALS);
                } else if(op.equals(Operator.GREATER_EQUALS)) {
                    node.setOperator(Operator.LESS);
                } else if(op.equals(Operator.EQUALS)) {
                    node.setOperator(Operator.NOT_EQUALS);
                } else if(op.equals(Operator.NOT_EQUALS)) {
                    node.setOperator(Operator.EQUALS);
                } else if(op.equals(Operator.CONDITIONAL_OR)) {
                    node.setOperator(Operator.CONDITIONAL_AND);
                } else if(op.equals(Operator.CONDITIONAL_AND)) {
                    node.setOperator(Operator.CONDITIONAL_OR);
                }
            }
        });
        return copyExpression;
    }

If anyone can, please let me know if there is a way to resolve variable bindings from a copied node. Also, my question is similar to JDT - Bindings are lost after copying subtree. But no one seemed to have answered it. So I am posting my question anyway.

0 Answers0