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?


Asked by: Rafael974 | Posted: 21-01-2022






Answer 1

Yes, it is possible, but only since Java 6. Peter von der Ahé talks about the two JSRs in this interview. Of JSR 199:

The JSR 199 Compiler API consists of three things: The first one basically allows you to invoke a compiler via the API. Second, the API allows you to customize how the compiler finds and writes out files. I mean files in the abstract sense, since the files the compiler deals with aren't necessarily on the file system. JSR 199's file abstraction allows you to have files in a database, and to generate output directly to memory, for example. Finally, the JSR 199 API lets you collect diagnostics from the compiler in a structured way so that you can easily transform error messages, for instance, into lines in an IDE's editor.

JSR 269 is the annotation processing API.

This article gives an excellent overview of accessing the Compiler Tree API. The section "Accessing the Abstract Syntax Tree: The Compiler Tree API" seems particularly suitable for your question.

Depending on what you're doing, you may also want to look at the Jackpot Rule Language, which is a standalone refactoring engine that plugins into the Compiler Tree.

Answered by: Brooke379 | Posted: 22-02-2022



Answer 2

Compile and run this with -cp tools.jar (where you have to specify the location of your tools.jar, obviously).

import com.sun.source.util.Trees;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class JCTreeTest {
    private static final JavaCompiler javac
            = ToolProvider.getSystemJavaCompiler();

    public static void main(String[] args) {
        final StandardJavaFileManager jfm
                = javac.getStandardFileManager(null, null, null);
        final JavaCompiler.CompilationTask task
                = javac.getTask(null, jfm, null, null, null,
                  jfm.getJavaFileObjects(args));
        final Trees trees = Trees.instance(task);
        // Do stuff with "trees"
    }
}

It compiles and runs for me, though I have not played with the trees stuff myself, so you'll have to read the javadoc yourself. :-) Good luck!

Answered by: Blake875 | Posted: 22-02-2022



Similar questions

java - 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: Class1.java package parseable; public class Class1 { /** * @param args */ public static void main(String[] args) { System.out.println("Hello world!"); } }


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 | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top