Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Eclipse JDT - Abstract Syntax Tree (AST) and the Java Model - Tutorial

Lars Vogel

Version 0.3

24.05.2009

Revision History
Revision 0.111.05.2009Lars Vogel
Created Article.
Revision 0.223.05.2009Lars Vogel
Described Java Model and AST
Revision 0.324.05.2009Lars Vogel
Added AST example

Eclipse JDT

This article describes how the Eclipse JDT (Java Development Tools) and the Eclipse AST (Abstract Syntax Tree) can be used to access, change and read the elements of a Java program.

The article assumes that you are familiar with Eclipse plugin development. In this article Eclipse 3.4 is used.

Level of this tutorial: Advanced


Table of Contents

1. Eclipse JDT
1.1. Overview
1.2. Java Model
1.3. Abstract Syntax Tree (AST)
2. Prerequisites
3. Accessing your Java projects with the JDT Java model
4. Creating new Java elements via the Java model
5. Using the AST
6. Thank you
7. Questions and Discussion
8. Links and Literature
8.1. Source Code
8.2. Eclipse JDT Resources
8.3. vogella Resources

1. Eclipse JDT

1.1. Overview

The Eclipse Java Development Tools (JDT) provide APIs to access and manipulate Java source code. It allows to access the existing projects in the workspace, create new projects and modify and read existing projects. It also allows to launch Java programs.

Java JDT allows to access Java source code via two different means. The Java Model and the Abstract Syntax Tree (AST).

which is a Document Object Model similar to the XML DOM

1.2. Java Model

Each Java project is represented in Eclipse as a Java model. The Eclipse Java model is a light-weight and fault tolerant representation of the Java project. It does not contain as many information as the Abstract Syntax Tree (AST) (for example it does not contain the method body of a method) but is fast to re-created in case of changes. For example the outline view is using the Java model for it representation; this way the information in the outline view can quickly get updated.

The Java model is defined in package and plugin "org.eclipse.jdt.core".

The Java model is represented as a tree structure which can be described via the following table.

Table 1. 

Project ElementJava Model elementDescription
Java projectIJavaProjectThe Java project which contains all other objects.
src folder / bin folder / or external libraryIPackageFragmentRoot Hold source or binary files, can be a folder or a library (zip / jar file )
Each packageIPackageFragment Each package is below the IPackageFragmentRoot, sub-packages are not leaves of the package, they are listed directed under IPackageFragmentRoot
Java Source FileICompilationUnitThe Source file is always below the package node
Types / Fields / MethodsIType / IField / IMethodTypes, fields and methods

1.3. Abstract Syntax Tree (AST)

The AST is a detailed tree representation of Java source code. The AST defines API to modify, create, read and delete source code.

The package for AST is org.eclipse.jdt.core.dom in the Eclipse org.eclipse.jdt.core plugin.

Each element in the Java source file is represented as a subclass of ASTNode. Each specific AST node provides specific information about the object it represents. For example you have MethodDeclaration (for methods), VariableDeclarationFragment (for variable declarations) and SimpleName (for any string which is not a Java keyword).

The AST is typically created based on a ICompilationUnit from the Java Model.