In this best practices I'd like to explain when and how to best adapt EMF’s default code generation facilities. The goal of this best practise is to motivate developers to get in control of their code generation process. Knowing how EMF's code generation process works and if needed, where to start to make changes, is a big step towards extensive model driven development. EMF comes with a set of default templates which support the generation of model code, UI independent controller code and Eclipse specific editor code. According to my experience, one can go a long way with EMF’s default templates and the code generation configuration mechanisms available on genmodel level. However, knowing about the possibility to interfere in the generation process empowers developers to apply project specific template adaptation, decide weather to make changes to generated code or templates and extend code generation to areas not yet covered by EMF.
EMF default templates can be found in the plugin org.eclipse.emf.codegen.ecore. The subfolder /templates/ contains all EMF templates necessary to generate model, edit, editor and model test code. During code generation EMF compiles its templates to Java template classes holding all template text in String member variables and a single method generate.
Once done and no changes have been made to the template, thecompilation step may be omitted to speed up the generation process. This can be controlled through the GenModel attribute dynamicTemplates. Setting dynamicTemplates to false will only compile the Java template classes if not yet available. Setting dynamicTemplates to true will recompile template classes anytime. Thus any change to a template will lead to changes in the final generation artefact.
To let developers override EMF templates on a per model basis the GenModel attribute templateDirectory is used. The attribute specifies the location of the new template directory with respect to the workspace root (e.g. at.somecompany.somePluginProject/templates). While name and location of the directory are arbitrary, its internal subdirectory structure must stick to the predefined directory structure in org.eclipse.emf.codegen.ecore/templates.
It is always best to selectively copy original templates to the specified template directory. If the GenModel attribute dynamicTemplates is set to true EMF’s Generator implementation will first try to retrieve the required template from the specified directory, if none could be found EMF’s default template directory is searched.
EMF’s template mechanism is based on Java Emitter Templates (JET). JET supports JSP like syntax. A template contains fixed text, which is output as is, and text within JSP tags which are used an navigate through a model or directly express java code. To learn more about JET syntax see this EMF Corner Article. A downturn of using JET is its limited tool support. Up to Eclipse Helios 3.6 no working jet template editor was available.
Hopefully to be continued...
EMF default templates can be found in the plugin org.eclipse.emf.codegen.ecore. The subfolder /templates/ contains all EMF templates necessary to generate model, edit, editor and model test code. During code generation EMF compiles its templates to Java template classes holding all template text in String member variables and a single method generate.
Once done and no changes have been made to the template, thecompilation step may be omitted to speed up the generation process. This can be controlled through the GenModel attribute dynamicTemplates. Setting dynamicTemplates to false will only compile the Java template classes if not yet available. Setting dynamicTemplates to true will recompile template classes anytime. Thus any change to a template will lead to changes in the final generation artefact.
To let developers override EMF templates on a per model basis the GenModel attribute templateDirectory is used. The attribute specifies the location of the new template directory with respect to the workspace root (e.g. at.somecompany.somePluginProject/templates). While name and location of the directory are arbitrary, its internal subdirectory structure must stick to the predefined directory structure in org.eclipse.emf.codegen.ecore/templates.
It is always best to selectively copy original templates to the specified template directory. If the GenModel attribute dynamicTemplates is set to true EMF’s Generator implementation will first try to retrieve the required template from the specified directory, if none could be found EMF’s default template directory is searched.
EMF’s template mechanism is based on Java Emitter Templates (JET). JET supports JSP like syntax. A template contains fixed text, which is output as is, and text within JSP tags which are used an navigate through a model or directly express java code. To learn more about JET syntax see this EMF Corner Article. A downturn of using JET is its limited tool support. Up to Eclipse Helios 3.6 no working jet template editor was available.
Hopefully to be continued...