完整的 Java 元编程框架?
我对元编程(即帮助程序员完成繁琐的编程任务的程序)感兴趣。我正在寻找一个具有以下属性的工具:
- 在编译时和运行时都可用;
- 检查程序结构;
- 可以添加新的类、方法或字段并使它们对 Java 编译器可见;
- 可以改变方法的行为;
- 基于 Java(嗯,根据一些排名,Java 是最流行的编程语言);
- 与 IDE 和构建工具(如 Ant、Gradle 或 Maven)良好集成;
- 积极维护的项目;
- 易于使用和扩展;
有一些解决方案,例如:
- 反射
- AspectJ
- 注释处理工具
- 字节码操作(CGLIB、Javassist、java.lang.instrument)
- Eclipse JDT
- Project Lombok
- Groovy、JRuby、Scala
但不幸的是它们都没有满足上述所有标准。 Java 有完整的元编程解决方案吗?
I'm interested in metaprogramming (i.e. programs that help programmers do tedious programming tasks). I'm looking for a tool which has the following properties:
- usable both at compile time and runtime;
- inspects program structure;
- can add new classes, methods or fields and make them visible to Java compiler;
- can change behavior of methods;
- Java-based (well, Java is most popular programming language according to some rankings);
- good integration with IDEs and build tools like Ant, Gradle or Maven;
- actively maintained project;
- easy to use and extend;
There are some solutions for this, like:
- reflection
- AspectJ
- Annotation Processing Tool
- bytecode manipulation (CGLIB, Javassist, java.lang.instrument)
- Eclipse JDT
- Project Lombok
- Groovy, JRuby, Scala
But unfortunately none of them meets all the criteria above. Is there any complete metaprogramming solution for Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有 JackPot,它是基于 Java 的,但我认为当前并没有引起太多关注。有 AST 和符号表 AFAIK。你也许可以扩展它;我怀疑有人会阻止(或帮助)你。
Sun,呃,Oracle java 编译器有基于 Java 的编译器 API。它们可能会得到积极维护,但我认为您无法修改源代码并重新生成它。当然有符号表;不知道树木。可能很难扩展;你必须跟上编译器的步伐,而不是相反。
有 ANTLR,它有一个 Java 实现和一个可以构建 AST 的 Java 解析器。我认为它没有完整的符号表,因此进行认真的代码分析/修订可能很困难。 ANTLR 当然得到了积极的维护,没有人会反对你用符号表来增强 Java 语法。请注意,如果您只做这些,那么 Java 1.6 大约需要 6 个月的时间。 (这就是我们内部[聪明]人员为 DMS 做这件事所花费的时间,从 1.4 的符号表支持开始)。
我们的 DMS 软件重新工程工具包及其 Java 前端。
DMS 是通用编译器机制:解析、AST 构建、符号表机制、流分析机制,以及源到源转换和 AST 的通用漂亮打印回法律文本(包括保留注释)的额外好处。它提供了一组支持这些服务的 API,以及用于定义语法和依赖于语言的流分析器的附加工具。
Java 前端为 DMS 提供了关键细节(使用这些 API)以允许其处理 Java:语法/解析器、Java 1.4-1.6 的完整符号表构造(1.7 即将发布)以及一些控制和数据流分析(随着时间的推移会延长,因为这个东西非常有用)。
通过使用DMS和Java前端提供的服务,人们可以合理地考虑构建任意的Java分析和转换工具。 (这使得该工具成为“完整的”元编程工具,因为它可以检查任何语言结构,或更改任何语言结构,而不是模板元编程或反射)。我们相信这比临时工具更有效,因为您不必构建基础设施,所提供的基础设施很强大,可以处理您没有精力实施的情况,并且它旨在支持此类任务。 YMMV。
DMS/Java 前端已用于构建各种 Java 工具:测试覆盖率、分析器、死代码消除、大规模克隆检测、带有超链接源代码的 JavaDoc、快速 XML 解析器/生成器等。
是的,它正在积极维护;自1998年第一个版本以来一直在不断增强。
There's JackPot, which is Java based but I don't think gets a lot a current attention. Has ASTs and symbol tables AFAIK. You can probably extend it; I doubt anybody will stop (or help) you.
There's the Java-based compiler APIs for the Sun, er, Oracle java compiler. They're likely actively maintained, but I don't think you can modify source code and regenerate it. Certainly has symbol tables; dunno about trees. Probably pretty hard to extend; you have to keep up with the compiler guys, not the other way round.
There is ANTLR, which has a Java implementation and a Java parser that will build ASTs. I don't think it has full symbol tables, so doing serious code analysis/revision is likely to be hard. ANTLR is certainly actively maintained, and nobody will object to you enhancing the Java grammar with symbol tables. Just know that will take you about 6 months for Java 1.6 if that's all you do. (That's how long it took our internal [smart] guy to do it for DMS, starting with symbol table support for 1.4).
Not in Java, and not easily integrated into IDEs, but capable of carrying massive analysis and transformation on Java code is our DMS Software Reengineering Toolkit with its Java Front End.
DMS is generic compiler machinery: parsing, AST building, symbol table machinery, flow analysis machinery, with that additional bonuses of source-to-source transformations and generic prettyprinting of ASTs back to legal text including retention of comments. It offers a set of APIs supporting these services, and additional tools for defining grammars and langauge-dependent flow analyzers.
The Java Front End gives crucial detail (using those APIs) to DMS to allow it process Java: a grammar/parser, full symbol table construction for Java 1.4-1.6 (with 1.7 due momentarily), as well as some control and data flow analysis (to be extended over time because this stuff is so useful).
By using the services provided by DMS and the Java Front end, one can reasonably contemplate building arbitrary Java anlaysis and transformation tools. (This makes the tool a "complete" metaprogramming tool, in that it can inspect any language structure, or change any language structure, as opposed to say template metaprogramming or reflection). We believe this to be much more effective than ad hoc tools because you don't have to build the infrastructure, the infrastructure provided is robust and handles cases you don't have the energy to implement, and it is designed to support such tasks. YMMV.
DMS/Java Front end have been used to construct a variety of Java tools: test coverage, profilers, dead code elimination, clone detection on scale, JavaDoc with hyperlinked source-code, fast XML parser/generators, etc.
Yes, its actively maintained; undergoing continuous enhancement since the first version in 1998.
Tapestry IOC 中有一个 Java 元编程框架,名为 塑料。它使用自定义类加载器来处理类字节码,我还没有尝试过,但看起来它提供了一个简单的接口,仍然使程序员能够进行强大的元编程更改。
There's a Java metaprogramming framework that is part of Tapestry IOC, it's called Plastic. It munges class bytecodes using custom classloaders, I haven't tried it yet but it looks like it gives a simple interface that still enables the programmer to make powerful metaprogramming changes.
查看元编程系统:
http://www.jetbrains.com/mps/
它有很棒的功能IDE 支持,JetBrains 的聪明人经常使用它。
Check out the Meta Programming System:
http://www.jetbrains.com/mps/
It has great IDE support and is used quite frequently by the smart folks at JetBrains.
查看 Spring Roo。
Check out Spring Roo.