Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to manage multiple modifications on the same AST
How to manage multiple modifications on the same AST [message #1272016] Sun, 16 March 2014 22:14 Go to next message
Mohammad Ghafari is currently offline Mohammad GhafariFriend
Messages: 1
Registered: March 2014
Junior Member
Hello everybody,

I need to visit each method declaration within a class in order to add some comments to the methods in the given class.

void parseMethodDeclaration() {

   ICompilationUnit unit = getCompilationUnitByName(unitName);
   ASTParser parser = ASTParser.newParser(AST.JLS4);

   CompilationUnit parse = (CompilationUnit) parser.createAST(null);

   parse.accept(new ASTVisitor() {

      public boolean visit(MethodDeclaration node) {

          String unitName = node.resolveBinding().getDeclaringClass().getName();

          //if some conditions are satisfied
          addComment(unitName, node);

          return super.visit(node);

And the addComment method is defined as follows to add comments to the given method:
void addComment(String unitName, MethodDeclaration method){

    ICompilationUnit unit = getCompilationUnitByName(unitName);
    AST ast = method.getAST();
    ASTRewrite rewriter = ASTRewrite.create(ast);
    ListRewrite listRewrite = rewriter.getListRewrite(method.getBody(), 

    Statement placeHolder =(Statement)rewriter.createStringPlaceholder("//MyComment",

    listRewrite.insertFirst(placeHolder, null);

    TextEdit edits = rewriter.rewriteAST();

    Document document = new Document(unit.getSource());

This code works properly to add comments to the first visited method, but it doesn't apply comments in the right place in other methods. The problem originates from the fact that I'm using the original AST to visit each method while its corresponding sourcecode (ICompilationUnit) will be changed after the comments are added to the first method. Then, listRewrite.insertFirst will put the comment in a wrong place.

Do you know how can I resolve this issue? Is there anyway to update the original AST as well?
Re: How to manage multiple modifications on the same AST [message #1273004 is a reply to message #1272016] Wed, 19 March 2014 09:37 Go to previous messageGo to next message
shankha banerjee is currently offline shankha banerjeeFriend
Messages: 40
Registered: February 2013
Does this help:



and then


Re: How to manage multiple modifications on the same AST [message #1369868 is a reply to message #1273004] Tue, 20 May 2014 19:36 Go to previous message
Noopur Gupta is currently offline Noopur GuptaFriend
Messages: 58
Registered: December 2012
You should use the same instance of ICompilationUnit and ASTRewrite.
First, record changes to all the methods using the single ASTRewrite instance and then use #rewriteAST at the end.
Previous Topic:if i want to cancel all java.awt......
Next Topic:How to get (or set) what a class extends?
Goto Forum:

Current Time: Sat May 18 04:08:29 GMT 2024

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

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

Back to the top