Insert/Add Statements to Java Source Code by using Eclipse JDT ASTRewrite

This belongs to Eclipse JDT Tutorial Series.

This code example is for inserting a statement to existing Java source code by using eclipse JDT. It works inside of a Plug-in.

For simplicity purpose, the following code simple add a method invocation statement called "add" to the first line of a method.

public class Main {
	public static void main(String[] args) {
	}
	public static void add() {
		int i = 1;
		System.out.println(i);
	}
}

The code above becomes the following:

public class Main {
	public static void main(String[] args) {
		add();
	}
	public static void add() {
		int i = 1;
		System.out.println(i);
	}
}

Again, this is a completely working code tested under Eclipse Indigo.

	private void AddStatements() throws MalformedTreeException, BadLocationException, CoreException {
 
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("testAddComments");
		IJavaProject javaProject = JavaCore.create(project);
		IPackageFragment package1 = javaProject.getPackageFragments()[0];
 
		// get first compilation unit
		ICompilationUnit unit = package1.getCompilationUnits()[0];
 
		// parse compilation unit
		CompilationUnit astRoot = parse(unit);
 
		// create a ASTRewrite
		AST ast = astRoot.getAST();
		ASTRewrite rewriter = ASTRewrite.create(ast);
 
		// for getting insertion position
		TypeDeclaration typeDecl = (TypeDeclaration) astRoot.types().get(0);
		MethodDeclaration methodDecl = typeDecl.getMethods()[0];
		Block block = methodDecl.getBody();
 
		// create new statements for insertion
		MethodInvocation newInvocation = ast.newMethodInvocation();
		newInvocation.setName(ast.newSimpleName("add"));
		Statement newStatement = ast.newExpressionStatement(newInvocation);
 
		//create ListRewrite
		ListRewrite listRewrite = rewriter.getListRewrite(block, Block.STATEMENTS_PROPERTY);
		listRewrite.insertFirst(newStatement, null);
 
		TextEdit edits = rewriter.rewriteAST();
 
		// apply the text edits to the compilation unit
		Document document = new Document(unit.getSource());
 
		edits.apply(document);
 
		// this is the code for adding statements
		unit.getBuffer().setContents(document.get());
	}
Category >> Java Development Tools (JDT)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Carad on 2012-12-4

    I get a “The method parse(ICompilationUnit) is undefined for the type Main” message.
    Where Main is the name of my class and java file (Main.java).
    How could I fix it?

  2. Zeinab on 2013-1-5

    Hi,
    I need to add some strings and also an if-else that whenever the condition is right some part of the original code is executed do you think that the code here could help?
    could you please guide me?

  3. Ron on 2013-1-8

    I don’t understand exactly what you mean. Can you give me an example which shows your input and your expected output?

Leave a comment

*