Martin Taal's Blog

About EMF, Teneo, CDO, Texo, Hibernate, Open-Source ERP and other topics

Archive for February 2012

Texo: easy (custom) code generation with EMF merging, code formatting and import resolving

leave a comment »

Texo provides code/JPA entity generation from ecore/xsd models specific for web server/service environments. In this post I will discuss how easy it is to extend/override Texo code generation with your own templates. You can even replace the complete Texo code generation with your own templates. Using Texo for your own code generation has some distinctive advantages.

Texo uses XPand as the templating language, the first question is why use Texo and not use XPand directly, well here are several good reasons:

  • EMF merging: Texo supports the same merging of manual code as EMF, you can manually change the generated code, manual changes will be retained when regenerating, so if you generate your code with the @generated javadoc tag you will get EMF merging for free (incl. support of merging of annotations etc.)
  • Code formatting: Texo makes sure that your code is formatted according to the development project code formatting standards
  • Import resolving: an important/difficult topic to solve yourselve, just generate all your code with fully-qualified class names, Texo will do automatic import resolving for you.
  • Efficient/Effective: a source file is only touched if it is actually changed by Texo, so your code will not show unneeded diffs, source files which are not needed anymore are automatically removed

All this is available out-of-the box and will work for all your generated code! Now how to extend/override code generation?

Let’s first see how to generate Texo standard code, it is very easy… After installing Texo, right-click any ecore/model and in the Texo submenu choose one of the generate code options.

Texo Entity Generation

Now the next step is to add/override Texo templates, it is a just few steps:

  • add the Texo nature to the project

Add Texo nature

  • create a folder structure inside your development project, the folder structure must match the template path in Texo
  • place your own templates in the folder structur


Now let’s see an example of a template, here is one which generates a simple subclass of a generic DAO:

«IMPORT org::eclipse::emf::texo::modelgenerator::modelannotations»
«IMPORT org::eclipse::emf::texo::annotations::annotationsmodel»
«IMPORT org::eclipse::emf::ecore»
«IMPORT org::eclipse::emf::texo::generator»

«EXTENSION org::eclipse::emf::texo::modelgenerator::templates::util»

«DEFINE root(ModelController modelController) FOR EClassModelGenAnnotation-»

«LET ((EPackageModelGenAnnotation)this.ownerEPackageAnnotation) AS ePackageAnnotation»

«FILE "org/eclipse/emf/texo/example" + toLowerCase( + "/" + this.simpleClassName + "" java-»
package org.eclipse.emf.texo.example.daoimpl.«toLowerCase(»;

 * The Dao implementation for the {@link «this.qualifiedClassName»} entity.
 * @generated
public class «this.simpleClassName-»HbDaoImpl extends org.eclipse.emf.texo.example.GenericDaoHbImpl<«this.qualifiedClassName», Long> implements org.eclipse.emf.texo.example.dao.«toLowerCase(».«this.simpleClassName-»Dao

 * @generated
 public «this.simpleClassName-»HbDaoImpl(org.eclipse.emf.texo.example.HbSessionProvider sp, org.eclipse.emf.texo.example.dao.DaoFactory df) {
 super(«this.qualifiedClassName».class, sp);


See that the @generated doc tag is used, this makes it possible to manually change the generated code, and re-generate without loosing your changes.

This was basically it, so it is not more than adding your own templates and tell Texo where to find them.

Thanks for reading! Visit the Texo wiki for more information. Let me know if you have any questions or remarks on the EMFT newsgroup or forum.

gr. Martin

Written by martintaal

February 23, 2012 at 12:24 pm

Posted in Texo