Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code / template generator for Java

For C#, I have often used CodeSmith and lately the T4 generator which is part of 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.

like image 434
rbrayb Avatar asked Oct 16 '08 19:10

rbrayb


People also ask

How do you create a code generator in Java?

You can create a generator model with the YAKINDU Statechart generator model wizard by selecting File → New → Code generator model. The code generation is performed automatically whenever the statechart or the generator file is modified. See also chapter Running a generator for more information.

How do you do templating in Java?

Template Method is a behavioral design pattern that allows you to defines a skeleton of an algorithm in a base class and let subclasses override the steps without changing the overall algorithm's structure.

What is a FreeMarker template?

FreeMarker is a Java-based template engine which can be used in stand-alone or servlet-based Java programs. In FreeMarker you define templates, which are text files that contain the desired output, except that they contain placeholders like ${name} , and even some logic like conditionals, loops, etc.

What is templating engine in Java?

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data.


2 Answers

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

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.

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

like image 152
Vincent Ramdhanie Avatar answered Sep 21 '22 10:09

Vincent Ramdhanie


I have worked with both Velocity and 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.

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

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

like image 29
joel.neely Avatar answered Sep 20 '22 10:09

joel.neely