Java代码生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3385495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:49:23  来源:igfitidea点击:

Java code generation

javacode-generation

提问by YaOg

What are the leading frameworks for java code generation?

java代码生成的主要框架有哪些?

I am not looking for a DB or app generation tool. I have a skeleton of a class, and I need to generate it with different dynamic parts for different use cases. The majority of the class is identical, hence I want to run code that generates different flavors of the class.

我不是在寻找数据库或应用程序生成工具。我有一个类的骨架,我需要为不同的用例生成不同的动态部分。该类的大部分内容是相同的,因此我想运行生成不同风格的类的代码。

Anyone know a good framework?

谁知道一个好的框架?

Thanks.

谢谢。

采纳答案by stacker

cglibis a powerful, high performance and quality Code Generation Library

cglib是一个强大、高性能和高质量的代码生成库

jet/velocityare using templates.

jet/velocity正在使用模板。

If you're looking to generate a whole application check out JBoss Seamframework.

如果您希望生成整个应用程序,请查看JBoss Seam框架。

回答by Bozho

Note that these utilities are mainly for kickstarting your project by generating boilerplate code.

请注意,这些实用程序主要用于通过生成样板代码来启动您的项目。

回答by Michael Borgwardt

SQL2Javagenerates Database CRUD code from the DB schema.

SQL2Java从 DB 模式生成数据库 CRUD 代码。

回答by Neel

[SQL2JAVA] is a great tool. It generates All [CRUD] code for the Database Schema. It's connection pooling is not good enough,you can customize it's Manager class to maintain your own pool.

[ SQL2JAVA] 是一个很棒的工具。它为数据库架构生成所有 [ CRUD] 代码。它的连接池不够好,你可以自定义它的Manager类来维护你自己的池。

Other than this if you are interested with Model Driven Development (MDD),you can use [AndroMDA] or [Borland's Together] , one of the best [eclipse] based tool out there.

除此之外,如果您对模型驱动开发 ( MDD)感兴趣,您可以使用 [ AndroMDA] 或 [ Borland's Together],这是目前最好的基于[ eclipse] 的工具之一。

If your are interested in going a bit further with code generation and getting into model driven software development you should have a look at [openArchitectureWare].

如果您有兴趣进一步了解代码生成并进入模型驱动软件开发,您应该查看 [ openArchitectureWare]。

Other java development tools that support code generation are [Lombok] and [Spoon]. Project Lombok offers the features like auto-generation of the default getter/setter methods, automatic resource management (using @Cleanup annotation) and annotation driven exception handling.

其他支持代码生成的java开发工具有【Lombok】和【Spoon】。Project Lombok 提供诸如自动生成默认 getter/setter 方法、自动资源管理(使用 @Cleanup 注释)和注释驱动的异常处理等功能。

[cglib],[Velocity template] and [AppFuse] are also great.

[ cglib]、[ Velocity template] 和 [ AppFuse] 也很棒。

回答by Esko Luontola

I presume that ASMis the most popular Java bytecode generation library. It's the most low-level bytecode library there is, but there are other higher-level code generation libraries which are built on ASM, although using ASM directly isn't too hard either (one benefit of direct use is that ASM's JAR size is very small). Some of ASM's users are mentioned at http://asm.ow2.org/users.html

我认为ASM是最流行的 Java 字节码生成库。它是最底层的字节码库,但还有其他更高级的代码生成库建立在 ASM 之上,虽然直接使用 ASM 也不是太难(直接使用的一个好处是 ASM 的 JAR 大小非常大)小的)。在http://asm.ow2.org/users.html中提到了 ASM 的一些用户



EDIT:

编辑:

For the updated question, which mentions the use case ("I have a skeleton of a class, and I need to generate it with different dynamic parts for different use cases."), my answer is to improve the design so that all code duplication is removed and no code generation is needed.Reading about different design patterns can give some ideas on how to do it. For a more specific answer, you will need to show some code and be more specific about your needs.

对于提到用例的更新问题(“我有一个类的骨架,我需要为不同的用例使用不同的动态部分生成它。”),我的答案是改进设计,以便所有代码重复被删除,不需要代码生成。阅读不同的设计模式可以提供一些关于如何做的想法。要获得更具体的答案,您需要显示一些代码并更具体地说明您的需求。

回答by Impiastro

I used the Druid Database Manager: http://druid.sourceforge.net/

我使用了德鲁伊数据库管理器:http: //druid.sourceforge.net/

It starts from db, which can be reverse engineered, and it can generate documentation, sql, classes and files based on tables and fields.

它从db开始,可以进行逆向工程,可以根据表和字段生成文档、sql、类和文件。

The template language used is velocity.

使用的模板语言是velocity。

The software is extensible through a plugin system.

该软件可通过插件系统进行扩展。

回答by Rui Curado

AtomWeaver http://www.atomweaver.comis now in public beta. It's a code generation IDE that can be used alongside your current IDE (so, it's no framework, but a standalone app). With it you can generate boilerplate code, but also a complete project.

AtomWeaver http://www.atomweaver.com现在处于公开测试阶段。它是一个代码生成 IDE,可以与您当前的 IDE 一起使用(因此,它不是框架,而是一个独立的应用程序)。有了它,您可以生成样板代码,也可以生成一个完整的项目。

AtomWeaver implements ABSE (Atom-Based Software Engineering), which is a form of model-driven software development (has nothing to do with UML or MDA).

AtomWeaver 实现了 ABSE(Atom-Based Software Engineering),它是一种模型驱动的软件开发形式(与 UML 或 MDA 无关)。

It's essentially a template-based system.

它本质上是一个基于模板的系统。

回答by Pau

http://www.andromda.org

http://www.andromda.org

AndroMDA (pronounced: andromeda) is an open source code generation framework that follows the Model Driven Architecture (MDA) paradigm. It takes model(s) from CASE-tool(s) and generates fully deployable applications and other components.

AndroMDA(读作:andromeda)是一个遵循模型驱动架构(MDA)范式的开源代码生成框架。它从 CASE 工具中获取模型并生成完全可部署的应用程序和其他组件。

I have used it and it is very powerfull. It not only generates configuration files and code from UML, leaving to the developer only the implementation of the business methods to develop, but also maintains the generate code through the use of well-known dessing patterns.

我用过它,它非常强大。它不仅从UML生成配置文件和代码,只留给开发者实现业务方法去开发,而且还通过使用众所周知的设计模式来维护生成的代码。

回答by Ralph

Try Xtext (http://www.eclipse.org/Xtext/). You use XText to define a DSL, XText will gernerate a Eclipse Editor supporting this DSL. Then you can use XPand to define templates to generating all kind of text (for example java souce code).

尝试 Xtext ( http://www.eclipse.org/Xtext/)。您使用 XText 来定义 DSL,XText 将生成支持此 DSL 的 Eclipse 编辑器。然后您可以使用 XPand 定义模板来生成所有类型的文本(例如 java 源代码)。

回答by Salil

Another vote for the Velocity template engine. I've used it in multiple projects, for generating EJB2.1 boilerplate code, database objects, etc. Works great and is pretty easy to learn as well.

对 Velocity 模板引擎的另一票。我在多个项目中使用过它,用于生成 EJB2.1 样板代码、数据库对象等。效果很好,也很容易学习。