Java 代码/模板生成器

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

Code / template generator for Java

javatemplatescode-generation

提问by nzpcmad

For C#, I have often used CodeSmithand lately the T4 generatorwhich is part of Visual Studio.

对于 C#,我经常使用CodeSmith和最近的T4 生成器,它是 Visual Studio 的一部分。

I'm looking for something similar for Java, in particular an Eclipse add-in since I do all my Java development using the Eclipse IDE.

我正在寻找类似于 Java 的东西,特别是 Eclipse 插件,因为我使用 Eclipse IDE 进行所有 Java 开发。

回答by Vincent Ramdhanie

I've found that freemarkerdoes a pretty good job for generating any type of code. From the website:

我发现freemarker可以很好地生成任何类型的代码。从网站:

FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.

FreeMarker 是一个“模板引擎”;基于模板生成文本输出(从 HTML 到自动生成的源代码的任何内容)的通用工具。它是一个 Java 包,是 Java 程序员的类库。它本身不是面向最终用户的应用程序,而是程序员可以嵌入到他们产品中的东西。

It is used by struts2 a lot. The website has a long list of other products that use freemarker.

struts2 经常使用它。该网站有一长串使用 freemarker 的其他产品。

回答by joel.neely

I have worked with both Velocityand StringTemplate. Velocity is a bit more conventional (think JSP/ASP concepts), while StringTemplate seems a bit cleaner. in a sense described in this Artima interview. Both are pure templating engines, and will require you to write some code around them, if you want to do full-blown code generation from a model.

我已经使用过VelocityStringTemplate。Velocity 更传统一些(想想 JSP/ASP 概念),而 StringTemplate 似乎更简洁一些。在某种意义上描述了这次 Artima 采访。两者都是纯模板引擎,如果您想从模型生成完整的代码,则需要您围绕它们编写一些代码。

StringTemplate is used internally by ANTLR, which may be useful to your effort.

StringTemplate 由ANTLR内部使用,这可能对您有用。

As an alternative approach, you could go with Eclipse, using EMFand JET.

作为替代方法,您可以使用Eclipse,使用EMFJET

回答by lgu

You should try Telosys Tools, an Eclipse plugin for code generation working from an existing database with customizable Velocity templates

您应该尝试 Telosys Tools,这是一个 Eclipse 插件,用于从具有可自定义 Velocity 模板的现有数据库中生成代码

See: http://www.telosys.org/

见:http: //www.telosys.org/

Eclipse Marketplace : http://marketplace.eclipse.org/content/telosys-tools

Eclipse 市场:http: //marketplace.eclipse.org/content/telosys-tools

The tutorials are here : https://sites.google.com/site/telosystutorial/

教程在这里:https: //sites.google.com/site/telosystutorial/

See this other question about CodeSmith : Is there any freeware tool available which is like Codesmith?

请参阅有关 CodeSmith 的另一个问题:是否有类似 Codesmith 的免费软件工具可用?

回答by Daan

I use JavaForgerto generate code from templates. It parses existing classes so that you can use that class-datainside your templates. It can both create new classes or insert code into existing classes. You can determine where generated code will be inserted based on a string conversion rule (e.g. myProject/dao/ProductDao.java => myProject/service/ProductService.java).

我使用JavaForger从模板生成代码。它解析现有的类,以便您可以在模板中使用该类数据。它既可以创建新类,也可以将代码插入到现有类中。您可以根据字符串转换规则(例如 myProject/dao/ProductDao.java => myProject/service/ProductService.java)确定将生成的代码插入到何处。

JavaForger is open source and uses FreeMarkeras template engine and JavaParseras parser.

JavaForger 是开源的,使用FreeMarker作为模板引擎,使用JavaParser作为解析器。

回答by Adam Gent

This is an old question but the only thing that comes close (for Java) to do what CodeSmith Generator does is Spring Roo.

这是一个老问题,但唯一接近(对于 Java)做 CodeSmith Generator 所做的事情是Spring Roo

The reason is that Java does not have Partial Classes like C# does. Spring Roo gets around this by using AspectJ ITDs.

原因是 Java 没有像 C# 那样的分部类。Spring Roo 通过使用AspectJ ITD解决了这个问题。

回答by Arturo Hernandez

My answer is to use StringTemplate, but there is a bit more to it than just what tool to use.

我的答案是使用 StringTemplate,但它不仅仅是使用什么工具。

Is it the issue to generate java code? Or is it to use java tools? Programmers would be normally very comfortable writing code. Therefore, it would not be a leap to write some java classes and write a walk that would generate code using StringTemplate. I personally think it is a good exercise to create example models, generate your java code from the models. And depending on your use case you could end up writing JSON models by hand and never having to write any java code to produce the java code. Or you could end up writing Java classes that produce equivalent models.

是生成java代码的问题吗?或者是使用java工具?程序员通常会很舒服地编写代码。因此,编写一些 java 类并编写使用 StringTemplate 生成代码的 walk 并不是一个飞跃。我个人认为创建示例模型、从模型生成 Java 代码是一个很好的练习。根据您的用例,您最终可以手动编写 JSON 模型,而不必编写任何 Java 代码来生成 Java 代码。或者,您最终可能会编写生成等效模型的 Java 类。

You could use the StringTemplate based STST, which reads JSON. STST is command line based, and I am sure you could hook it to both eclipse and/or Visual Studio.

您可以使用基于StringTemplate 的 STST,它读取 JSON。STST 是基于命令行的,我相信您可以将它连接到 eclipse 和/或 Visual Studio。

I personally think about portability, JSON is an extremely simple language. And almost every language has libraries that support it.

我个人认为可移植性,JSON 是一种极其简单的语言。几乎每种语言都有支持它的库。

回答by karajdaar

Take a look at my project https://github.com/karajdaar/templatorand see if that helps.

看看我的项目https://github.com/karajdaar/templator,看看是否有帮助。

回答by harish2704

I wrote a simple web based application for my use. its available at https://github.com/harish2704/templatesand a demo is available at http://templates-harish2704.rhcloud.com/Its language independent tool. GUI supports several languages ( highlighting, snippet completion ect )

我编写了一个简单的基于 Web 的应用程序供我使用。它可以在https://github.com/harish2704/templates 上找到,演示可以在http://templates-harish2704.rhcloud.com/ 上找到它的语言独立工具。GUI 支持多种语言(突出显示、片段完成等)

回答by NSherwin

eclipse has a built-in template system.

eclipse 有一个内置的模板系统。

look in window -> preferences -> java -> code style -> code templates

查看窗口 -> 首选项 -> java -> 代码风格 -> 代码模板

回答by fastcodejava

You might look at my plugin : http://fast-code.sourceforge.net/. It allows one to select multiple fields and generate code using user specified velocity templates.

你可以看看我的插件:http: //fast-code.sourceforge.net/。它允许选择多个字段并使用用户指定的速度模板生成代码。