Java 源代码解析器/生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6999500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java source code parsers/generators
提问by Jarek
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
方便地解析 Java 源代码并轻松访问给定的元素。
轻松生成源代码文件,轻松将数据结构转化为代码
Any good tips, libraries, frameworks, tools? Thank you for help.
有什么好的技巧、库、框架、工具吗?谢谢你的帮助。
采纳答案by Daniel
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.
从 Java 6 开始,编译器在 JDK 中包含了一个 API。通过它,您可以通过javax.lang.model
API访问 Java 解析器的结果。JDK5 以Mirror API的形式提供了相同的功能。有一个很好的介绍性文章在这里。
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.
我见过的最好的代码生成工具是CodeModel。它有一个非常简单的 API,可以一次生成多个 Java 源文件。
回答by Sean Patrick Floyd
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)
如果您需要解析现有的源代码,请使用JavaParser。它使您可以基于访问者访问 AST。您可以编写新代码,但很多事情是痛苦的(例如引用其他类)
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.
如果您需要生成源代码,请使用CodeModel。它允许您以编程方式创建类、包、方法等,并且非常易于使用。但是,我认为它不能导入现有代码。
Both are pretty awesome in their respective domains.
两者在各自的领域都非常棒。
回答by Ira Baxter
Our DMS Software Reengineering Toolkitand its Java Front Endcan do this. They are designed to enable the construction of custom analyzers and code generators.
我们的DMS 软件再造工具包及其Java 前端可以做到这一点。它们旨在支持构建自定义分析器和代码生成器。
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.
DMS 提供通用解析、抽象语法树(带注释)和符号表构建、树导航/检查/修改设施,以及从修改后的树重新生成完整源代码的能力。其他工具包括源到源转换规则(“如果您看到此语法,请用该语法替换它”)和模式(用于构建或识别子树)、属性语法评估器、控制和数据流分析以及调用-图构建。Java Front End 专门为 Java 1.4-1.6 和 1.7 附近的 Java 1.4-1.6 使用 DMS 来完成所有这些。
(EDIT May 2016: Now handles Java 1.8)
(编辑 2016 年 5 月:现在处理 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.
DMS 还设计用于处理规模:它通常用于同时处理许多编译单元(源文件),从而实现跨文件边界的分析和转换。它还可以同时处理多种语言;DMS 具有适用于多种语言的前端。