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());
 
		parser.setKind(ASTParser.K_STATEMENTS);
 
		Block block = (Block) parser.createAST(null);
 
		//here can access the first element of the returned statement list
		String str = block.statements().get(0).toString();
 
		System.out.println(str);
 
		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.
org.eclipse.jdt.core.dom.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.

References:

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FBlock.html
http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FASTParser.html

7 thoughts on “Parse a sequence of Java statements by using JDT ASTParser”

  1. 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?

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

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

    error:

    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

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

Leave a Comment