Exercise 2. Code Generator Customization
What This Exercise Is About
Using the PurchaseOrder model, you will experiment with how the generated code can be customized. In particular, you will customize the generated code by enabling generator options and using dynamic templates.
For your reference, here again is the UML class diagram describing the purchase order model:

What You Should Be Able To Do
At the end of the lab, you should be able to:
- Make use of code generator options to affect the generated code for your model.
- Enable dynamic template code generation.
- Replace the header for generated Java source files.
- Insert additional methods into the code generated for classes in your model.
Required Materials
- Eclipse 3.2
- Eclipse Modeling Framework (EMF) 2.2
General Advice / Warnings
- Eclipse conventions suggest naming plug-in projects with a fully qualified plug-in ID, such as com.example.po. Do not use dashes in project names - this will create invalid entries in your MANIFEST.MF file.
- Make sure you're using the IBM JDK or else have implemented the Sun JDK Crimson DOM bug workaround (http://eclipse.org/emf/downloads-xerces.php).
Exercise Instructions
This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) version 3.2 with the Eclipse Modeling Framework (EMF) 2.2 installed into it. The exercise instructions refer to this product as either Eclipse or as "the workbench".
These instructions are located in the exercises/Exercise2_CodeGen_Customization folder. If you browse into this location by expanding the folders, you should find one additional folder: org.eclipse.emf.tutorial.advanced.po. This is an Eclipse project that you will import into your workspace to complete this exercise.
The folder exercises/Solution2 contains examples of the files you will create during this exercise.
Directions
- See Exercise 1, Step A.
- See Exercise 1, Step B.
- Enable the Suppress Interfaces generator model option in the PurchaseOrder generator model.
- Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B (if not already open).
- Select the topmost node PurchaseOrder, right-click, and select Show Properties View (if this view is not already shown).
- Scroll the Properties view to find the Model twisty. Underneath is the Suppress Interfaces property. Set that property to true.

- Enable the Array Accessors generator model option in the PurchaseOrder generator model.
- Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B (if not already open).
- Select the topmost node PurchaseOrder, right-click, and select Show Properties View (if this view is not already shown).
- Scroll the Properties view to find the Model twisty. Underneath is the Array Accessors property. Set that property to true.

- Enable the Dynamic Templates generator model option in the PurchaseOrder generator model.
- Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B (if not already open).
- Select the topmost node PurchaseOrder, right-click, and select Show Properties View (if this view is not already shown).
- Scroll the Properties view to find the Templates & Merge twisty. Underneath is the Dynamic Templates property. Set that property to true.

- Set the Template Directory generator model property in the PurchaseOrder generator model.
- Scroll the Properties view to find the Templates & Merge twisty. Underneath is the Template Directory property. Set that property to /org.eclipse.emf.tutorial.advanced.po/templates.
- Save the genmodel file by selecting File -> Save.

- Analyze the Header.javajetinc template replacement.
- Open the Header.javajetinc file available in the templates folder of the project imported in step B (if not already open).
- Examine the contents and change it if desired (don't forget to save any changes you make). The contents of this file will be used to replace the EMF generator's default header.

- Analyze the Class.javajet template replacement.
- Open the Class.javajet file available in the templates/model folder of the project imported in step B (if not already open).
- Examine the contents and change it if desired (don't forget to save any changes you make). The contents of this file will be used to replace the EMF generator's default template for classes. It looks like this file imports itself but what it's really doing is importing the contents the default template for classes, along with any insertions or overrides you may have specified (see below).

- Analyze the genFeature.insert.javajetinc template insertion.
- Open the genFeature.insert.javajetinc file available in the templates/model/Class folder of the project imported in step B (if not already open).
- Examine the contents and change it if desired (don't forget to save any changes you make). The contents of this file will be dynamically inserted by the EMF generator into the template used for classes. It declares two additional methods that will be generated for every multivalued, changeable feature.

- Generate the model code from the genmodel file.
- Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B (if not already open).
- Select the topmost node PurchaseOrder, right-click, and select Generate Model Code.
- The code should be compiled automatically as it is generated, and should recompile whenever it is changed. If you have disabled automatic building in the workbench preferences, you can initiate compilation manually by selecting Build All from the Project menu.

- Analyze the Customer.java source file.
- Open the Customer.java file available in the org.emf.tutorial.advanced.po package.
- Notice that only a class (and no interface) was generated as per the option enabled in step C, and that the body of the class contains both source and Javadoc for the class's features.

- Notice that array-oriented accessor methods have been generated for the purchaseOrders feature as per the option enabled in step D.

- Notice that the copyright was generated based on the header replacement from step E.

- Notice that two additional methods have been generated for the purchaseOrders feature based on the template insertion from step E.

Summary
You experimented with various ways to customize the EMF code generator and witnessed the effect of such customizations on the generated code.