Use ASTParser to Parse a Java Project

This example belongs to Eclipse JDT Tutorial Series.

I have talked about how to parse a Java method, how to parse a sequence of statements. Now let's see how to parse a Java project.

This example use Eclipse JDT ASTParser and Java Model. Since Java Model has to be used inside of a plug-in, the following code should be run inside a plug-in project. You can read the tutorial of how to create a plug-in.

The following is the code for handling an action, and it parses a project.

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
public class GetInfo extends AbstractHandler {
        public Object execute(ExecutionEvent event) throws ExecutionException {
                IWorkspace workspace = ResourcesPlugin.getWorkspace();
                IWorkspaceRoot root = workspace.getRoot();
                // Get all projects in the workspace
                IProject[] projects = root.getProjects();
                // Loop over all projects
                for (IProject project : projects) {
                        try {
                                if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
                                        IPackageFragment[] packages = JavaCore.create(project)
                                        // parse(JavaCore.create(project));
                                        for (IPackageFragment mypackage : packages) {
                                                if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
                                                        for (ICompilationUnit unit : mypackage
                                                                        .getCompilationUnits()) {
                                                                // Now create the AST for the ICompilationUnits
                                                                CompilationUnit parse = parse(unit);
                                                                //MethodVisitor visitor = new MethodVisitor();
                                                               // parse.accept(visitor);
                                                                /*for (MethodDeclaration method : visitor.getMethods()) {
                                                                        System.out.print("Method name: "
                                                                                        + method.getName()
                                                                                        + " Return type: "
                                                                                        + method.getReturnType2());
                                                                }  */
                        } catch (CoreException e) {
                return null;
         * Reads a ICompilationUnit and creates the AST DOM for manipulating the
         * Java source file
         * @param unit
         * @return
        private static CompilationUnit parse(ICompilationUnit unit) {
                ASTParser parser = ASTParser.newParser(AST.JLS3);
                return (CompilationUnit) parser.createAST(null); // parse

* You can search using Google code, and get a bunch of code examples like above. Actually, the code above is an example of search-driven programming style. It needs to be fully tested before being moved to production.

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";
  • Saikat Dutta

    can u share the eclipse project?

  • Ayesha Sadiq

    Hi. I am so much confused with all this. I just develop an plug in application and started my plug in using tutorial of “how to create a plug-in”. but i am confused where i should write the above code to parse a project wither in the same plug in project where i have written or add it in the new window where plugin opens with “Hello Eclipse message”. I am bit confused how to run the application to see parsing results.

    Please help me to see the results of parsed project