Eclipse Abstract Syntax Tree Programmatic Access

Could you provide an example of accessing the Eclipse Abstract Syntax Tree programmatically for a given piece of code?

eg getting the AST for:

package parseable;

public class Class1 {

 * @param args
public static void main(String[] args) {
    System.out.println("Hello world!");


Asked by: Luke782 | Posted: 28-01-2022

Answer 1

It is not an exact answer, that may give you a place where to start:

As said in this question,

A full example is available in this eclipse corner article, also more details in the eclipse help. And in the slide 59 of this presentation, you see how to apply a change to your source code.

Answered by: Emily346 | Posted: 01-03-2022

Answer 2

// get an ICompilationUnit by some means
// you might drill down from an IJavaProject, for instance 
ICompilationUnit iunit = ...

// create a new parser for the latest Java Language Spec
ASTParser parser = ASTParser.newParser(AST.JLS3);

// tell the parser you are going to pass it some code where the type level is a source file
// you might also just want to parse a block, or a method ("class body declaration"), etc

// set the source to be parsed to the ICompilationUnit
// we could also use a character array

// parse it.
// the output will be a CompilationUnit (also an ASTNode)
// the null is because we're not using a progress monitor
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

Don't be confused by the ICompilationUnit vs CompilationUnit distinction, which seems to be just the result of uncreative naming on their part. CompilationUnit is a type of ASTNode. ICompilationUnit in this context resembles a file handle. For more info on the distinction, see here:

Answered by: Edgar903 | Posted: 01-03-2022

Similar questions

java - javac.exe AST programmatic access example

Is it possible to access the Abstract Syntax Tree(AST) inside the javac.exe programmatically? Could you provide an example?

url - Programmatic way to place a website into a new Word file... in Java

Is it possible to programmatically place the contents of a web page into a Word file? To further complicate this, I'd like to do these steps in Java (using JNI if I must). Here are the steps I want to do programmatically, followed by ways that I would do this manually today: Provide a method with a URL (Manually: Open page in Firefox) Copy the contents of that URL (Ma...

html - Programmatic HTMLDocument generation using Java

Does anyone know how to generate an HTMLDocument object programmatically in Java without resorting to generating a String externally and then using HTMLEditorKit#read to parse it? Two reasons I ask: Firstly my HTML generation routine needs to be very fast and I assume that parsing a string into an internal model is more costly than directly constructing this model. Secondly, an object-oriented approach would ...

Programmatic web browser Java library

Does anyone know of any Java library for programmatic web browsing? Prowser doesn't cut it because there's no "push the button" method and Watij is limited to Internet Explorer Windows only.

java - Can I mix declarative and programmatic layout in GWT 2.0?

I'm trying to redo an existing panel that I made before GWT 2.0 was released. The panel has a few text fields and a scrollable panel below in a VerticalPanel. What I'd like to do is to make the scrollable panel with UIBinder and then add that to a VerticalPanel Below is an example I created to illustrate this: public class ScrollTablePanel extends ResizeComposite{ interface Binder extends Ui...

java - Programmatic property setting for maps in Hazelcast?

Is there a way to programmatically set the "time to live" property (or in fact, any property) for a distributed map in Hazelcast? I want to avoid having to change the Hazelcast config XML for this. I am using Hazelcast version 1.7.1

programmatic memory tracking in java

I am working on a large pre-existing system and can't use any of the external profiler tools that have been mentioned in other questions. That being said, is there any programttic way for me to get java to print out what variables are still allocated and using heap space? Variable name and type would be ideal but any identifying variable information would be helpful.

java - Connection pool problem with Spring and programmatic transaction management

I need your help in order to solve connection pool problem with Spring. I’m using Spring with Java 1.4 (no annotation). Here is the datasource and the connection pool definition: <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${database.jdbcName}</value> </property&g...

java - Provide Programmatic Access to All Data Available in String Form: toString()

Bloch said: Provide Programmatic Access to All Data Available in String Form. I am wondering if he means to override toString() which should involve 'all data available'? I think the 'in string form' means that the string is for human reading, so override toString() is enough for the advice. Am I correct?

java - Programmatic close of JFrame

What's the programmatic equivalent of clicking the close (x) button in the upper right corner of a JFrame? There's the dispose() method but that's not the same thing, since a JFrame can be set to do several different things upon closing (not to mention if there's a

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)