7

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.

1
  • 2
    What are you trying to generate specifically?
    – ScArcher2
    Commented Oct 16, 2008 at 20:31

11 Answers 11

6

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.

6

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.

2

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?

2

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.

1
  • The docs are inadequate to tell if it is usable by only declaration.
    – Infernoz
    Commented Aug 15, 2020 at 14:18
1

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.

1

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

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.

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.

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

1
  • Command line, please? A pure declaration Maven plugin is the only thing that I'm interested, anything boring should be automated.
    – Infernoz
    Commented Aug 15, 2020 at 14:23
0

I'm not a C# man so I don't know what the equivalents would be, however I've found xdoclet to be very good in the past. I don't think it integrates with eclipse as such but you can run it from an ant script. Does things like generating Hibernate mapping files from annotated Java classes. Useful if that's what you're looking for :)

0

eclipse has a built-in template system.

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

0

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.

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.