Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Bindings are lost after copying subtree with ASTNode.copySubtree(...)
Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711246] Wed, 14 October 2015 09:43 Go to next message
Sergey Toshin is currently offline Sergey ToshinFriend
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 #1711265 is a reply to message #1711246] Wed, 14 October 2015 13:06 Go to previous messageGo to next message
Erick Hagstrom is currently offline Erick HagstromFriend
Messages: 107
Registered: April 2014
Location: USA
Senior Member
Yes, this is as designed. Per the Eclipse documentation (JDT Plug-in Developer Guide > Programmer's Guide > JDT Core), "As soon as the tree has been modified, all positions and bindings are lost."

The thing is, there is no way in general to tell what obj is once the tree has been changed. The bindings have to be invalidated on any change.

To fix it, you'd need to apply your changes and get a new AST.
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711361 is a reply to message #1711265] Thu, 15 October 2015 10:15 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
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

Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711880 is a reply to message #1711361] Tue, 20 October 2015 08:55 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Any ideas?
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711881 is a reply to message #1711361] Tue, 20 October 2015 08:56 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Any ideas?
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1711945 is a reply to message #1711881] Tue, 20 October 2015 11:45 Go to previous messageGo to next message
Erick Hagstrom is currently offline Erick HagstromFriend
Messages: 107
Registered: April 2014
Location: USA
Senior Member
Take a look at Eclipse Documentation: JDT Plug-in Developer Guide > Programmer's Guide > JDT Core > Manipulating Java code. (At least that's where it is in my Luna installation. Your Mars-ilage may vary.) There's a section about 40% down called Creating an AST by reconciling a working copy. You may get some ideas there.
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1713740 is a reply to message #1711945] Fri, 06 November 2015 11:06 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
I've looked, but I have no idea how to point my CompilationUnit in recouncile method.
My code:
:
:
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
:
:
//changes to AST
:
:
	    
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = Path.fromOSString(MY_SOURCE_FILE.getAbsolutePath());
IFile file = workspace.getRoot().getFileForLocation(path);
ICompilationUnit compilationUnit = (ICompilationUnit) JavaCore.create(file);
compilationUnit.reconcile(AST.JLS8, true, true, workingCopyOwner (???), iProgressMonitor (???) );


Can you please help me with that? Thanks
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1714301 is a reply to message #1713740] Wed, 11 November 2015 15:26 Go to previous messageGo to next message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
So my general point is to 1) save original positions in the modified code (so, I'm not satisfied when I make changes and bindins are missed, but other things are perfect: old positions are saved, created elements have -1 position); 2) save bindings. I feel that when I do rewrite, I will lose old code elements positions, but it's not allowed
Re: Bindings are lost after copying subtree with ASTNode.copySubtree(...) [message #1715411 is a reply to message #1714301] Mon, 23 November 2015 12:12 Go to previous message
Sergey Toshin is currently offline Sergey ToshinFriend
Messages: 56
Registered: May 2015
Member
Help please. Some examples for reconcile or other ways to restore bindings

[Updated on: Mon, 23 November 2015 12:13]

Report message to a moderator

Previous Topic:Discussion, about this java code
Next Topic:Suggest A Book For Mobile Apps Development
Goto Forum:
  


Current Time: Thu Apr 25 01:25:50 GMT 2024

Powered by FUDForum. Page generated in 0.04108 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top