Parse a sequence of Java statements by using JDT ASTParser

Let's take a look how to parse a sequence of Java statement by using ASTParser from Eclipse JDT.

By saying a sequence of a statement, I mean lines of code that are from a method like the following:

int i = 9; 
int j = i+1;

Here is the code of how to do this.

public static void main(String[] args) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource("int i = 9; \n int j = i+1;".toCharArray());
		Block block = (Block) parser.createAST(null);
		//here can access the first element of the returned statement list
		String str = block.statements().get(0).toString();
		block.accept(new ASTVisitor() {
			public boolean visit(SimpleName node) {
				System.out.println("Name: " + node.getFullyQualifiedName());
				return true;

In the code above, parser.setKind(ASTParser.K_STATEMENTS) makes parser accept a sequence of statements. Once it is set to this, parser.createAST(null) will return a Block instead of a CompilationUnit.

Here is the class hierarchy of Block.

Since it is a subclass of ASTNode, it also has method that can accept an ASTVisitor to traverse the Abstract Syntax Tree.

You can download the required jar file here.


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:
String foo = "bar";
  • coachabower

    Sometimes parser.createAST(null); returns an empty Block, but sometimes it works.

    In my case I have a java file with about 10 methods, most are private, all are static, one or two accept a parameter. Three of these methods work every time and the rest always return empty. The main method is one that has contents in the Block (has a parameter String[] args), another method called fillArray() works and has no parameters and the last working one is called setFilename(String filename).

    The main method is public, setFilename is public, fillArray is private, all are static. The main method and setFilename have void return type, fillArray returns a boolean…

    I have monitored the variables as they get to the createAST(null) statement and the source charArray is populated correctly for each method in the file.

    I’m stumped as to what makes these three methods stand out and work as compared to the rest. The program under test has no warnings or errors according to eclipse and runs as expected both in eclipse and via command line.

    Do you know why the other methods may be returning an empty Block?

  • arnab

    when i execute this program i get the following error for the line

    Block block = (Block) parser.createAST(null);


    Multiple markers at this line

    – The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is indirectly referenced from

    required .class files

    could u please help me in this ..
    thans in advance

  • ryanlr

    Did you try the getParent() method of Block? It is a inherited method from ASTNode.

  • Imagine you are selecting a block of statements from a method. Is posible to know the method where you have selected the bock statement?

  • ryanlr

    What do you mean by saying “which method belongs to the block”?
    A Block is in the format of ” { { Statement } } “.

  • Any way to know to which method belongs the block???


  • Admin