21

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.

Jarek
  • 7,425
  • 15
  • 62
  • 89
  • You probably get a better answer if you tell us what you try to accomplish. There are a lot of code generators and inspection tools, but all with a different purpose. – M Platvoet Aug 09 '11 at 16:31
  • 4
    Reflection is a poor way to analyze code: it can't give you anything the compiler writers decided weren't worth reflection. For instance, you can't get any information about what's in an expression. Nor can it help you with code generation. – Ira Baxter Aug 11 '11 at 04:43
  • 5
    Additionally, sarcasm can be a poor way to answer a serious question. – Daniel Aug 13 '11 at 03:38
  • @Ira Baxter, reflection is indeed far from code generation, at least if you apply a common sense. Unfortunately, Microsoft has its own, incompatible version of common sense, placing the code generation functionality into namespace `System.Reflection.Emit`. Hence the persistant confusion, affecting even the Java community. – SK-logic Aug 17 '11 at 09:54
  • This looks like two questions in one: you want to parse Java source code, and generate Java source code as well. These are two separate questions that are likely to have two separate answers. – Anderson Green Feb 08 '13 at 23:30
  • @AndersonGreen: Some tools provide both capabilities in an integrated way, e.g., Program Transformation tools. One example is our DMS; see answer to this question. – Ira Baxter Jan 07 '14 at 20:12

4 Answers4

20

If you need to parse existing source code, use JavaParser. It gives you visitor-based access to the AST. You can write new code, but many things are a pain (e.g. referencing other classes)

If you need to generate source code use CodeModel. It lets you programmatically create classes, packages, methods etc, and it's very easy to use. However, I don't think it can import existing code.

Both are pretty awesome in their respective domains.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • With [JavaForger](https://github.com/daanvdh/JavaForger) you can use data parsed by JavaParser inside templates to generate classes. An overview of all parsed variables can be found [here](https://github.com/daanvdh/JavaForger/blob/master/TemplateVariables.md). – Daan Sep 10 '19 at 08:14
19

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.

Daniel
  • 10,115
  • 3
  • 44
  • 62
2

Our DMS Software Reengineering Toolkit and its Java Front End can do this. They are designed to enable the construction of custom analyzers and code generators.

DMS provides generic parsing, abstract-syntax tree (with comments) and symbol table building, tree navigation/inspection/modification facilities, and the ability to regenerate the complete source code from the modified tree. Additional facilities includes source-to-source transformation rules ("if you see this syntax, replace it with that syntax"), and patterns (used to build or recognize subtree), attribute grammar evaluators, control and data flow analysis, and call-graph construction. The Java Front End specializes DMS to do all of this for Java 1.4-1.6 with 1.7 nearby.

(EDIT May 2016: Now handles Java 1.8)

DMS is also designed to handle scale: it is often used to process many compilation-units (source files) at the same time, enabling analysis and transformations that cross file boundaries. It can also handle multiple languages at the same time; DMS has front ends for a wide variety of languages.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
1

Check out Antlr. One of its examples is a Java grammar.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55