Jarek Jarek - 6 months ago 10
Java Question

Java source code parsers/generators

I need tools to:


  • Conveniently parse Java source code and easily access given elements.

  • Easily generate source code files, to easily transform data structures into code



Any good tips, libraries, frameworks, tools? Thank you for help.

Answer

Since Java 6, the compiler has an API included in the JDK. Through it you can access the results of the Java parser through the javax.lang.model APIs. The same functionality was present with JDK5 in the form of the Mirror API. There's a good introductory article here.

The best code generation tool I've seen is CodeModel. It has a very simple API and can generate multiple Java source files at once.