Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to use ASTRewrite to update/add contents to a method?(How ASTRewrite is to be used to add statements to the body of a method. )
How to use ASTRewrite to update/add contents to a method? [message #715015] Fri, 12 August 2011 05:22 Go to next message
sagar  is currently offline sagar Friend
Messages: 12
Registered: August 2011
Junior Member
public void implementMethod(){

astOfMethod=methodToBeImplemented.getAST();

ASTRewrite astRewrite= ASTRewrite.create(astOfMethod);

Block body=astOfMethod.newBlock();
methodToBeImplemented.setBody(body);

Block oldBody=methodToBeImplemented.getBody();

MethodInvocation newMethodInvocation = astOfMethod.newMethodInvocation();
QualifiedName name = astOfMethod.newQualifiedName(astOfMethod
.newSimpleName("System"), astOfMethod.newSimpleName("out"));
newMethodInvocation.setExpression(name);
newMethodInvocation.setName(astOfMethod.newSimpleName("println"));

ExpressionStatement expressionStatement = astOfMethod.newExpressionStatement(newMethodInvocation);

body.statements().add(expressionStatement);


astRewrite.set(oldBody, MethodDeclaration.BODY_PROPERTY, body, null);

ctcObj.document = new Document(ctcObj.source);

edit= astRewrite.rewriteAST(ctcObj.document, null);

try {
edit.apply(ctcObj.document);
} catch (MalformedTreeException e) {

e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Note: In above code, the statement System.out.println() gets added to the body of the method. Because when I print methodToBeImplemented, I get the statement in the body of method. But Its not getting reflected in the document object.

In above code there is exception getting generated "IllegalArgumentException" for astRewrite.set method. I am not whether i did use it correctly or not?
Kindly help. Files are attached for reference. Input2.java is source file to be parsed. 2) Heuristics.java : above code is present in this file 3) ASTModifier.java : Visitor pattern for finding different nodes in AST. 4) ContractToCode.java: Main file to be executed.
Re: How to use ASTRewrite to update/add contents to a method? [message #716068 is a reply to message #715015] Tue, 16 August 2011 11:58 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
I don't see the need of calling astRewrite.set(). The body is already assigned.
Re: How to use ASTRewrite to update/add contents to a method? [message #717087 is a reply to message #715015] Fri, 19 August 2011 09:10 Go to previous messageGo to next message
skywalker.sky is currently offline skywalker.skyFriend
Messages: 9
Registered: July 2011
Junior Member
Try adding statement to the body using something like this:

ListRewrite statementsListRewrite = astRewrite.getListRewrite(body,Block.STATEMENTS_PROPERTY);

//Then add the statements using that ListRewrite
statementsListRewrite.insertFirst(newCode, null);


This worked for me..
Re: How to use ASTRewrite to update/add contents to a method? [message #717211 is a reply to message #717087] Fri, 19 August 2011 15:46 Go to previous message
skywalker.sky is currently offline skywalker.skyFriend
Messages: 9
Registered: July 2011
Junior Member
You can also use:

astRewrite.replace(oldBody, body, null);
Previous Topic:Problem using Java Search Engine
Next Topic:does anyone know how to configure this feature
Goto Forum:
  


Current Time: Fri Apr 26 01:05:40 GMT 2024

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

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

Back to the top