Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Texo/Template Overriding"

(Template input: model annotations)
(Introduction)
Line 6: Line 6:
  
 
Overriding/implementing your templates within Texo has the following main advantages:
 
Overriding/implementing your templates within Texo has the following main advantages:
* Easy: it is easy, you just need to place your xpt files in a java project.
+
* '''Easy''': it is easy, you just need to place your xpt files in a java project, Texo template files are small and easy to understand (and therefore easy to adapt).
* Organize imports in generated java files: Texo takes care of organizing your inputs (following the project settings), so in your template you just need to use fully-qualified names, Texo resolves and organizes them.
+
* '''Organize imports in generated java files''': Texo takes care of organizing your inputs (following the project settings), so in your template you just need to use fully-qualified names, Texo resolves and organizes them.
* Java Source Formatting: Texo takes care of formatting your code according to your project settings.
+
* '''Java Source Formatting''': Texo takes care of formatting your code according to your project settings.
* EMF-like-merging: Texo supports the EMF merge approach, this is available out-of-the-box for your code also. You can mix manual and automatic code in your code.
+
* '''EMF-like-merging''': Texo supports the EMF merge approach, this is available out-of-the-box for your code also. You can mix manual and automatic code in your code.
* Model annotations: the Texo model annotations are available for your templates. The model annotation contain extra information relevant for code generation (java class, java type information, etc.).
+
* '''Model annotations''': the Texo model annotations are available for your templates. The model annotation contain extra information relevant for code generation (java class, java type information, etc.).
  
 
By overriding Texo templates you don't need to work directly with MWE workflows, this is taken care of by Texo.
 
By overriding Texo templates you don't need to work directly with MWE workflows, this is taken care of by Texo.

Revision as of 00:28, 8 March 2010

Introduction

Texo makes it really easy to override specific Texo templates or even completely replace the templates used by Texo. This can be done for each Eclipse project differently, so different Eclipse project can have different generation patterns.

Overriding/implementing your templates within Texo has the following main advantages:

  • Easy: it is easy, you just need to place your xpt files in a java project, Texo template files are small and easy to understand (and therefore easy to adapt).
  • Organize imports in generated java files: Texo takes care of organizing your inputs (following the project settings), so in your template you just need to use fully-qualified names, Texo resolves and organizes them.
  • Java Source Formatting: Texo takes care of formatting your code according to your project settings.
  • EMF-like-merging: Texo supports the EMF merge approach, this is available out-of-the-box for your code also. You can mix manual and automatic code in your code.
  • Model annotations: the Texo model annotations are available for your templates. The model annotation contain extra information relevant for code generation (java class, java type information, etc.).

By overriding Texo templates you don't need to work directly with MWE workflows, this is taken care of by Texo.

Texo uses different templates for generating different parts of the model code. The templates can be found in the org.eclipse.emf.texo.modelgenerator.templates package in the org.eclipse.emf.texo.modelgenerator plugin. You can find this plugin in CVS or in your eclipse plugins folder (after installing).

Template overriding

When generating model code, Texo checks the project properties to see if a template folder has been set in the Texo project properties. If this is the case, first this folder is checked for templates before using the standard Texo templates.

Note, Texo checks the project properties of the project which contains the model file. So if your model files are in different projects then each project needs such a templates folder set in the project properties. Currently (March 2010) the templates folder must be in the same project as the model file, in future versions it will probably be possible to select a workspace or filesystem folder outside of the project.

Templates to override

The templates can be found in cvs.

There are a few templates which are particularly interesting to override:

  • addition.xpt: this template is intended to be overridden to generate additional files, it is called for each EPackage, EClass and EEnum in the model.
  • advice.xpt: it can be used to define XTend advices (for more information on XPand/Xtend aspect oriented support see here). For Texo it is empty, it is intended to be copied and overridden.
  • model.xpt: is the main template which calls the other templates to generate all the code. By overriding this you can control the complete generation for an EPackage.

Here is a short description of some of the other templates:

  • ecorefile.xpt: this template takes care of saving the ecore file in the same package as the entity classes.
  • entity.xpt: an important template, takes care of generating the entity classes
  • interface.xpt: handles the EClasses which are interfaces
  • featuregroup.xpt, modelfeaturemap.xpt: involved in generating code for support of featuremaps
  • modelfactory.xpt: generates the ModelFactory class
  • modelobject.xpt: takes care of generating the model api (inner-classes in the ModelFactory)
  • modelpackage.xpt: responsible for generating the ModelPackage.

Overriding a template

To override one of the template you have to do the following:

  • create a folder in the same project as the model file
  • create a directory structure inside this folder corresponding to the Texo templates package: org/eclipse/emf/texo/modelgenerator/templates
  • create your template file inside the org/eclipse/emf/texo/modelgenerator/templates folder with the exact same name as the template file used by Texo. For example to override the generation of the main model classes you need to create an entity.xpt file (probably copy the original one and adapt it).


Org.eclipse.emf.texo.override.template.png


Template input: model annotations

The template is called with model annotations as the main class. The model annotation enriches the domain model with code generation specific information (for example the java class). There are model annotation for each of the main model elements (EPackage, EClass, EStructuralFeature, EReference, EAttribute, EDataType, EEnum).

The model annotation definition (ecore and java files) can be found in the org.eclipse.emf.texo.modelgenerator plugin. The ecore file defining the model annotations can be found here.

Texo will be extended with additional annotation sets (JPA, Hibernate Search, etc.), this means that over time more types of annotations are available to the templates.

Template Tips

  • Generating additional files: the addition.xpt is ment to be overridden for generating additional files.
  • Organize imports: Texo takes care of organizing imports and safely resolving fully qualified names. This means that in your template you should use fully qualified names.
  • Merging support: Texo also incorporates your additional files in the EMF-like merge process. This means that you can easily implement the same EMF-like-merge-support in your files (just place the @generated tag in the javadoc of classes, methods, members).
  • Clean up: to prevent orphaned files Texo removes files which are not regenerated. So be aware that generation should only be done in packages/directories which contain files which are always generated together.

Back to the top