Java source code parsers/generators [closed]

早过忘川 提交于 2019-11-28 06:25:11

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!