Use JDT ASTParser to Parse Single .java files

This example belongs to Eclipse JDT Tutorial Series.

First of all, Eclipse JDT ASTParser CAN work in a standard Java application. This privides the convenience of simple parsing of a large amount of Java class files. Suppose you want to parse 100 .java files which are under a directory. You would not be able to import to eclipse workspace as a project. Fortunately, we can still parse those files by using ASTParser.

In brief, there are 3 steps to do this:
1. read file names from a directory
2. get code strings from each file
3. use JDT ASTParser to parse

Below is the jar files required to run the program. This is prerequisite condition, problem will happen if this is not configured right. Here is download link: lib.zip.

jar file required for JDT ASTParser

Here is a complete code of ASTParser in a standalone standard Java application.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
 
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
 
public class Main {
 
	//use ASTParse to parse string
	public static void parse(String str) {
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource(str.toCharArray());
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
 
		final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
 
		cu.accept(new ASTVisitor() {
 
			Set names = new HashSet();
 
			public boolean visit(VariableDeclarationFragment node) {
				SimpleName name = node.getName();
				this.names.add(name.getIdentifier());
				System.out.println("Declaration of '" + name + "' at line"
						+ cu.getLineNumber(name.getStartPosition()));
				return false; // do not continue 
			}
 
			public boolean visit(SimpleName node) {
				if (this.names.contains(node.getIdentifier())) {
					System.out.println("Usage of '" + node + "' at line "
							+ cu.getLineNumber(node.getStartPosition()));
				}
				return true;
			}
		});
 
	}
 
	//read file content into a string
	public static String readFileToString(String filePath) throws IOException {
		StringBuilder fileData = new StringBuilder(1000);
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
 
		char[] buf = new char[10];
		int numRead = 0;
		while ((numRead = reader.read(buf)) != -1) {
			System.out.println(numRead);
			String readData = String.valueOf(buf, 0, numRead);
			fileData.append(readData);
			buf = new char[1024];
		}
 
		reader.close();
 
		return  fileData.toString();	
	}
 
	//loop directory to get file list
	public static void ParseFilesInDir() throws IOException{
		File dirs = new File(".");
		String dirPath = dirs.getCanonicalPath() + File.separator+"src"+File.separator;
 
		File root = new File(dirPath);
		//System.out.println(rootDir.listFiles());
		File[] files = root.listFiles ( );
		String filePath = null;
 
		 for (File f : files ) {
			 filePath = f.getAbsolutePath();
			 if(f.isFile()){
				 parse(readFileToString(filePath));
			 }
		 }
	}
 
	public static void main(String[] args) throws IOException {
		ParseFilesInDir();
	}
}

This is one way to parse a java class file, another way is to do it in a plug-in.

4 thoughts on “Use JDT ASTParser to Parse Single .java files”

  1. Can you tell how to get statement’s information in text form from a method ? please help . @ David can you please share your code ?

  2. This is for a single .java file. Is it possible to parse a complete java project (having ant build) by this standalone parser, not in eclipse plugin?

  3. If you’re using this, you’ll probably want to set the compiler version to something higher than the default (1.3):

    Map options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options);
    parser.setCompilerOptions(options);

  4. Could you tell me how to modify variable names and save changes of a java source file? and in a standalone application, not eclipse plugin. Thank you

Leave a Comment