问题
I have a code that removes all complex (not SimpleName expressions) from if
s. 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