Friday, May 28, 2010

Code Generation Patterns

The Acceleo IDE provides an extension point to help you to define your own code generation pattern proposals.

Here is an example available in Acceleo 3.0 (use the same way to define your own proposal) :


The extension point is org.eclipse.acceleo.ide.ui.proposal
The Java class implements "org.eclipse.acceleo.ide.ui.views.proposals.patterns.IAcceleoPatternProposal"

Generation patterns have been introduced in acceleo because we noticed that something that happens really often when developing code generators is the need to implement some behavior on an interface and all or part of its subtypes.

For example, let's imagine you are implementing a java generator from UML. What you want is to have a template called javaName which will generate the name of any classifier, with some default behavior and some specific behavior on classes and interfaces.

This is where the Generation Patterns view comes into play:
  • Locate the cursor in the template, at the position where you want to insert your javaName templates
  • In the Generation patterns view, select "[template] for all selected types" in the top part
  • Select the types for which you want to create javaName templates for
Note the bottom part of the Generation Patterns views presents a hierarchical view of the metamodel you are using. Each node represents a type of the metamodel and contains all of its subtypes. So, one type can appear several times in this view, one time for each of its super-classes or super-interfaces. When you select a node (by checking the combo-box before it), all its descendants are also selected by default, but you can uncheck those you don't need.


Once you have selected the types you need, go back to the editor and activate the completion by hitting Ctrl+Space. The first choice should be "[template] for all selected types", select it.


New templates are then inserted into you Acceleo module. They are called "name" by default but you can immediately rename them by just entering the name you want. All templates will be renamed simultaneously.


Here, Acceleo has done his job, now it's time for you to do yours: implement these newly created templates!

No comments: