Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Add a method call in a block statement
Add a method call in a block statement [message #1721478] Wed, 27 January 2016 17:50 Go to next message
Williams Mising name is currently offline Williams Mising nameFriend
Messages: 60
Registered: July 2009
Member
I would like to add a method call call_m() at the beginning of each block. I have the following code:
	public void add_MethodCall(CompilationUnit cu,Block node) throws CoreException, MalformedTreeException, BadLocationException 
	{		AST ast = node.getAST();
		ASTRewrite astRewrite= ASTRewrite.create(ast);

		SimpleName newName= ast.newSimpleName("call_m");
		MethodInvocation newInvocation = ast.newMethodInvocation();
		newInvocation.setName(newName);
		Statement newStatement = ast.newExpressionStatement(newInvocation);
		ListRewrite listRewrite = astRewrite.getListRewrite(node, Block.STATEMENTS_PROPERTY);
		
		listRewrite.insertFirst(newStatement,null);
		
//		doc = new Document(doc.get());  // class attribute doc denotes the current  working document
		doc = new Document(f_content); // class attribute f denotes the content of the current working document
		TextEdit edits = astRewrite.rewriteAST(doc,null);
		System.out.println("call_m is added BEFORE " + doc.get());
		
		edits.apply(doc);
		
		System.out.println("call_m is added AFTER " + doc.get());
}

It seems it works when each block statement is visited. Since the input file has multiple blocks and I want to save the previous method calls. So, I just use the statement commented out. But, it seems that the final document gets messed up. The intended method call is not appropriated inserted. Any suggestion about the program?
Re: Add a method call in a block statement [message #1722453 is a reply to message #1721478] Thu, 04 February 2016 21:36 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
When you need multiple edits on the same document, I'd suggest to collect all changes in one instance of ASTRewrite, and perform rewriting only at the very end, i.e., once per document.
Stephan
Previous Topic:Publishing exported packages with specific version in ecj.jar?
Next Topic:Apply patch error
Goto Forum:
  


Current Time: Wed Apr 24 15:44:03 GMT 2024

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

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

Back to the top