Code / template generator for Java

22,243

Solution 1

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.

Solution 2

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.

Solution 3

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

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

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

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

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

Solution 4

I use JavaForger to generate code from templates. It parses existing classes so that you can use that class-data inside 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 is open source and uses FreeMarker as template engine and JavaParser as parser.

Solution 5

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

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

Share:
22,243
rbrayb
Author by

rbrayb

I'm a Microsoft Identity architect, an Enterprise Mobility MVP and a Microsoft Identity evangelist. I cover Azure AD, Azure AD B2C, ADFS, the ADAL and MSAL libraries and the Microsoft Identity Platform. I'm also across WIF, SAML 2.0, OpenID Connect and OAuth 2. I've also helped customers integrate with Auth0, Okta, IdentityServer4 and ForgeRock. I have a passion for talking Identity and love whiteboard sessions.. I believe in the Auckland IT community and help run four user groups. I also help to organise the Global Azure conference in Auckland. I tweet regularly on Identity topics as @rbrayb. I used to blog at http://nzpcmad.blogspot.co.nz but my new blog is at https://medium.com/the-new-control-plane. My presentations: https://rbrayb.github.io/Presentations/ All my posts on Authory: https://authory.com/RoryBraybrook

Updated on August 02, 2020

Comments

  • rbrayb
    rbrayb over 3 years

    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.

  • Infernoz
    Infernoz over 3 years
    The docs are inadequate to tell if it is usable by only declaration.
  • Infernoz
    Infernoz over 3 years
    Command line, please? A pure declaration Maven plugin is the only thing that I'm interested, anything boring should be automated.