7

I'm looking for a java library that allows me to parse a java source file and that gives me an AST representation of the code.

Actually I'm only interested in the class and method definitions with their annotations. I don't need the AST of the method code.

I'm using this information for code generation. This is why I can't compile the source file first to get the information from the resulting class file. The code wouldn't compile without errors until I generate some additional classes.

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
tangens
  • 39,095
  • 19
  • 120
  • 139

6 Answers6

10

Java 6 supports this as a native part of the compiler and has standard APIs for it (javax.lang.model). You can read up on it here. It is designed specifically for your use-case (i.e. code generation from annotations and source).

Ramon
  • 8,202
  • 4
  • 33
  • 41
  • Yes, this looks good. Can you give me a hint which class is responsible for reading the source file? – tangens Oct 31 '09 at 20:10
  • You create a compiler "plugin" that you specify to javac on the command line. I think the interface you implement is `javax.annotation.processing.Processor` – Ramon Oct 31 '09 at 20:13
  • 1
    javax.lang.model is based on what is required for processing annotations, rather than being an abstract syntax tree for the whole language. So it has packages, class, methods, fields, parameters and variables, but not expressions or statements. – Pete Kirkham Oct 31 '09 at 20:17
  • What a shame. I really need to call the parser from within my generation framework. – tangens Oct 31 '09 at 20:21
  • 1
    What this allows you to do is to *build your code generation framework into javac itself*. @Pete: I am of course aware of this, question specifies that method bodies are *not* required. – Ramon Nov 01 '09 at 02:14
  • My code generation consists of different models (not only java files) that I parse individually and put them together in a big repository that is used by a freemarker template engine to generate lots of code. But yes, I'll think about it if I can put all of this into javac. – tangens Nov 01 '09 at 12:15
8

ANTLR (http://www.antlr.org/) has a parser for the Java language and it also suports ASTs.

try

options {
    output=AST;
}

in the *.g file (I haven't tried it personally);

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
2

I think NetBeans Javaparser might be something you'll be interested in.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
luvieere
  • 37,065
  • 18
  • 127
  • 179
2

You can try JavaCC with a grammar file for java language.

True Soft
  • 8,675
  • 6
  • 54
  • 83
2

I found the project javaparser, which parses a single java file and returns a well formed AST.

tangens
  • 39,095
  • 19
  • 120
  • 139
2

@tangens wrote about JavaParser but referenced a now outdated project.

There has been a fork in 2011 that is still actively maintained and now also parses Java 1.7 and 1.8.

You can find it there: https://github.com/javaparser/javaparser

Disclaimer: I am a contributor to that project.