1

I have a code that removes all complex (not SimpleName expressions) from ifs. It works fine and the code like

if(obj.getSomeInt() > 10)
{
/* body */
}

is converted to

boolean value = obj.getSomeInt() > 10;
if(value)
{
/* body */
}

But before changing AST the call to resolveBinding() and resolveTypeBinding() for obj gives correct results, but after I get null for the both calls. Is that as designed? If yes, please tell how to fix that?

My code:

if(node instanceof IfStatement)
{
    IfStatement stmt = (IfStatement) node;
    AST ast = node.getAST();
    Expression expr = stmt.getExpression();
    if(!(expr instanceof SimpleName))
    {
        SimpleName varName = ast.newSimpleName(generateVariableName());

        VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
        vdf.setName((SimpleName) ASTNode.copySubtree(ast, varName));
        vdf.setInitializer((Expression) ASTNode.copySubtree(ast, expr));

        VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
        vds.setType(resolveType(expr));
        list.add(i++, vds);

        stmt.setExpression(varName);
    }
}

Before this operation, all bindings from expr are losed

Ivan
  • 361
  • 3
  • 16

0 Answers0