Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-core-dev] How to manage multiple modifications on the same AST

You are rewriting the AST for every addComment(). That is wrong. You should do it just once at the end after done with all the method declarations.

BTW, these questions are better asked at the eclipse-jdt/core forum.

Regards,
Jay



From:        Mohammad Ghafari <ghafari.ir@xxxxxxxxx>
To:        jdt-core-dev@xxxxxxxxxxx,
Date:        03/17/2014 03:28 AM
Subject:        [jdt-core-dev] How to manage multiple modifications on the same AST
Sent by:        jdt-core-dev-bounces@xxxxxxxxxxx





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);
       parser.setKind(ASTParser.K_COMPILATION_UNIT);
       parser.setSource(unit);
       parser.setResolveBindings(true);

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

        parse.accept(new ASTVisitor() {

           @Override
           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(),
       Block.STATEMENTS_PROPERTY);

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

   listRewrite.insertFirst(placeHolder, null);

   TextEdit edits = rewriter.rewriteAST();

 
    Document document = new Document(unit.getSource());
   edits.apply(document);
   unit.getBuffer().setContents(document.get());
}



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?



Thank you very much


--

Mohammad Ghafari

DeepSE group @ DEIB - Politecnico di Milano

http://home.deib.polimi.it/ghafari_______________________________________________
jdt-core-dev mailing list
jdt-core-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jdt-core-dev


Back to the top