Java source code parsers/generators [closed]

2019-01-17 07:29发布

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.

4条回答
Luminary・发光体
2楼-- · 2019-01-17 07:48

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.

查看更多
爷的心禁止访问
3楼-- · 2019-01-17 07:54

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

查看更多
我只想做你的唯一
4楼-- · 2019-01-17 07:58

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.

查看更多
Root(大扎)
5楼-- · 2019-01-17 08:04

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.

查看更多
登录 后发表回答