Code generation using StringTemplate

I’ve been using StringTemplate (http://www.stringtemplate.org) for some cool code generation for some time and its ease has really impressed me. I first used it to generate some templates to send email and got hooked on. The simplicity is just awesome. After reading some codegen articles by Terence listed here, I decided to use it to generate java files.

Update: Here is a link one should read to understand code generation with StringTemplate.
http://www.codegeneration.net/tiki-read_article.php?articleId=65

What I like about StringTemplate:

  • Strict MVC which is very important IMO. I’ve fallen into traps using other engines where the model can be modified from within the view. As soon as I wanted to add a new view, I was totally lost. I had to go into the old view and find all places where I set some magic value into the model. I like StringTemplate’s policy of just disallowing it instead of giving you an option :-)
  • Flexibility of using various delimiters such as ‘<' or '$' is nice.
  • StringTemplateGroups: I find this very helpful since its very natural to the way one generates code. Group all the templates belonging to a certain action into a group (now doesnt that make sense)
  • Pretty printing is turned on by default. Now all the new lines are exactly where you put them in your template.

(There as many other features which I haven’t yet used but I’ll update on as I do)

Lately I’ve been using it to generate a template for my entire app. I call it appgen (short for app generator) which takes a few options and spits out neatly intended java code such as filters, servlets, listeners and the standard web.xml and weblogic.xml descriptors in any given directory.
Before I wrote this tool, I have some qwerty little script which copied empty war files into a directory. I’d call this tool and then modify all the classnames/ xml descriptors to suit my needs and then fire up my deployer to deploy this war to the server. The tool now lets me specify a set of properties and point it to a dir and I get an entire war all done up. More on appgen soon…

Share
  • del.icio.us
  • Facebook
  • Digg
  • description

Related posts:

  1. AppGen
  2. TML

blog comments powered by Disqus