Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711246] |
Wed, 14 October 2015 09:43 |
Sergey Toshin Messages: 56 Registered: May 2015 |
Member |
|
|
Hi,
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(e));
list.add(i++, vds);
stmt.setExpression(varName);
}
}
Before this operation, all bindings from expr are lost
[Updated on: Wed, 14 October 2015 10:35] Report message to a moderator
|
|
|
|
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711361 is a reply to message #1711265] |
Thu, 15 October 2015 10:15 |
Sergey Toshin Messages: 56 Registered: May 2015 |
Member |
|
|
How to generate new AST if I have edited AST?
Is that possible to keep old ASTNodes positions? When I add a new node, it has -1 start position and 0 length, it's important to make changes and have old position numbers. But I believe after changing the AST it won't be possible. Maybe there is another way to implement this feature?
The most obvious way I see is running ASTVisitor for ASTNode on old AST, collecting all ASTNodes and then running it for new AST with appending found nodes (they should be the same and have the same positions). Later to get old position, I can call hashMap.get(newNode).getStartPosition. But maybe there is a better way? Thanks
[Updated on: Thu, 15 October 2015 10:17] Report message to a moderator
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04108 seconds