JDT - Bindings are lost after copying subtree

痞子三分冷 提交于 2019-12-24 15:11:47

问题


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

来源:https://stackoverflow.com/questions/33122461/jdt-bindings-are-lost-after-copying-subtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!