Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:48:54+00:00 2026-05-28T19:48:54+00:00

Update 2: thanks again to @deepak-azad, I managed to solve my problem : here

  • 0

Update 2: thanks again to @deepak-azad, I managed to solve my problem : here is a link to the main code : https://gist.github.com/1714641

Update : thanks to @deepak-azad, I complemented the code, but still not working.

Im trying to instrument a source file in Java Using EclipseJDT. My main goal is to put some statement “x();” below every variable declaration.

for example:
From this:

int a = 10;

to this :

int a = 10;
method();

I was able to create the ast and made a class tha extend the visit method to get all the variable declaration inside the code :

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class VDS extends ASTVisitor {
List<VariableDeclarationStatement> vds = new ArrayList<VariableDeclarationStatement>();

    @Override
    public boolean visit(VariableDeclarationStatement node) {
        vds.add(node);
        return super.visit(node);
    }   

    public List<VariableDeclarationStatement> getVDS() {
        return vds;
    }
}

I tested it and it works fine the capture of each variable, but not the insertion of a new node, using something like this:

VDS vds = new VDS();
unit2.accept(vds); // unit2 is the compilation unit
System.out.println("Variables :" + vds.getVDS().size());

for(VariableDeclarationStatement vds_p : vds.getVDS()){
    System.out.println(vds_p.toString()) 

    List<Statement> a = ((Block) vds_.getParent()).statements();
     a.add(e);//e is the artificial statement 

        ListRewrite lrw = astRewrite.getListRewrite(vds_p.getParent().BLock.STATEMENTS_PROPERTY);
lrw.insertAfter(e,vds_p,null);

    }

And the code to actually write down is (this part works I tested with a arbitrary annotation)

IDocument document2;
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); // get the buffer manager
IPath path = unit2.getJavaElement().getPath(); // unit: instance of CompilationUnit
try {
    bufferManager.connect(path, null); 
    ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
    // retrieve the buffer
    document2 = textFileBuffer.getDocument(); 
    textFileBuffer.commit(null /* ProgressMonitor */, false /* Overwrite */); 

} finally{ bufferManager.disconnect(path, null);}
TextEdit edits = unit2.rewrite(document2, null);
edits.apply(document2);
write2File(document2);  

Im a bit confused on how does the hierarchy works, and I came to an idea based on the fact that each variable statement belong to a block , and each block belong to a method, but still dont know how to handle this in the ListRewrite element.

I’ve been reading about ast in eclipse, but all question are related with the creation of code but not the process of edition

When I executed this, I get an illegal argument exception like this :

java.lang.IllegalArgumentException
    at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1905)
    at org.eclipse.jdt.core.dom.ASTNode$NodeList.add(ASTNode.java:1269)
    at java.util.AbstractList.add(AbstractList.java:91)
    at plugin.astsimple.handlers.SampleHandler.execute(SampleHandler.java:109)
    at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
        ...

where line 109 of my code is the one that adds e to a

a.add(e);

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:48:56+00:00Added an answer on May 28, 2026 at 7:48 pm

    Each ‘Block’ consists of a ‘list’ of statements, hence you need to use “org.eclipse.jdt.core.dom.rewrite.ListRewrite.insertAfter(ASTNode, ASTNode, TextEditGroup)”. You can look for examples of this method’s usage in org.eclipse.jdt.ui plug-in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
Code updated to working version. Thanks again for the help :) Hey guys. I
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
Update Thanks to Marc's help the AlphaPagedList class is now available on CodePlex if
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
Update: Solved, with code I got it working, see my answer below for the
So, I have built an auto update program to my program. The code that
Update: Thanks for the responses so far. To clarify, I'm not really looking for
Update2 Please see my answer to my question. Update So, I updated the code
UPDATE: Thanks for the answers! I can't seem to figure it out, even with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.