views:

206

answers:

4

As far as I know, the only way to parse Java source-code into an AST (Abstract Syntax Tree) is to use the Java Compiler Tree API: com.sun.source.tree

I have two questions:

  1. What JDKs support com.sun.source.tree?
  2. Is there a portable replacement that works for all JDKs?
+2  A: 

You can possibly take the tools.jar and use it. javac is open source so you can just grab that code (assuming you can deal with the license). Antlr has grammars for Java as well.

TofuBeer
Redistributing tools.jar: good point! OpenJDK's classpath exception makes for a great license.
Gili
A: 

Regarding your second question, there are dozens of Java parsers available in addition to Sun's. Here is a small sample:

  • Eclipse's org.eclipse.jdt.core.dom package.
  • Spoon outputs a very nice annotated parse tree with type information and variable binding (and uses Eclipse's parser internally)
  • ANTLR is a parser-generator, but there are grammars for Java available
  • javaparser (which I have not used)

My best advice is to try each of them to see which works best for your needs.

Brett Daniel
What is the difference between Eclipse's jdt DOM and Spoon?
Gili
The actual AST classes are roughly analogous, but Spoon's parse tree includes semantic information like variable binding without requiring a massive IDE infrastructure to be running. One can parse and analyze Java files by simply adding one jar file to the classpath.
Brett Daniel
+1  A: 

I've used Eclipse's AST parser. I found it to be pretty good (well it was part of an Eclipse plug-in so it did make sense to use it). See Exploring Eclipse's ASTParser.

Jeremy Raymond
A: 

See SD's Java Front End, which is a full featured Java parser built on top of the DMS Software Reengineering Toolkit.

The point of DMS is that it provides a huge variety of useful machinery (attribute grammars, symbol tables, flow analysis, AST manipulation including access and update, as well as source-to-source transformations) to analyze and transform that AST into results and/or modified source code. If you get "just" a Java parser (e.g., JavaCC + Java grammar) you will, IMHO, not be able to do a lot with it. DMS makes it possible to do a lot, without having to invent all that extra machinery yourself.

Ira Baxter