Monday, June 7, 2010

How an Android file could become a code generation template in few clicks?

Let's take a simple Android metamodel with different kinds of Widgets, like Text, Spinner, Button...

Acceleo provides a way to customize quickly your Android code generator, and manage the different code generation alternatives. With the For/If menu item, you can apply a specific treatment to each kind of Widgets.

In the Java file which contains Android widgets declarations, select them and then right-click and run: Source > As For/If (Alt+Shift+F).


Each kind of Widget is isolated as a new code generation alternative.


Then fill the appropriate kind of Widget in each condition (This is the link between the metamodel concept and the textual alternative to produce for this concept).


Leave just one TextField declaration and delete the other ones. Complete it by changing the static name of Widgets with a dynamic Acceleo expression by using the Quick replacement tool.


Repeat for the other files. The Alt+Shift+F feature is more useful the second time you use it because it tries to fill automatically the alternative condition.


Acceleo 3.0 comes with an Android Tutorial. This is meant to give beginners a tutorial to get them started with defining their own code generators... and advanced users a way to discover some of Acceleo 3.0 features.

I have shown this feature at the Eclipse modeling runaway 2010 and some of you asked me to have more details...

I hope it will be useful for you.


No comments: