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:

Extended PO model


What You Should Be Able To Do

At the end of the lab, you should be able to:

Required Materials

General Advice / Warnings

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

Step A: Verify workbench configuration

  1. See Exercise 1, Step A.

Step B: Import the project into the workspace

  1. See Exercise 1, Step B.

Step C: Customize the Generator to Suppress Interfaces

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


Step D: Customize the Generator to Generate Array Accessors

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


Step E: Customize the Generator to Use Dynamic Templates

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

  2. Set the Template Directory generator model property in the PurchaseOrder generator model.
    1. 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.
    2. Save the genmodel file by selecting File -> Save.
    3. Set Template Directory in PurchaseOrder.genmodel

  3. Analyze the Header.javajetinc template replacement.
    1. Open the Header.javajetinc file available in the templates folder of the project imported in step B (if not already open).
    2. 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.
    3. Analyze Header.javajetinc

  4. Analyze the Class.javajet template replacement.
    1. Open the Class.javajet file available in the templates/model folder of the project imported in step B (if not already open).
    2. 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).
    3. Analyze Class.javajet

  5. Analyze the genFeature.insert.javajetinc template insertion.
    1. Open the genFeature.insert.javajetinc file available in the templates/model/Class folder of the project imported in step B (if not already open).
    2. 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.
    3. Analyze genFeature.insert.javajet


Step F: Generate and Examine the Model Code

  1. Generate the model code from the genmodel file.
    1. Open the PurchaseOrder.genmodel file available in the model folder of the project imported in step B (if not already open).
    2. Select the topmost node PurchaseOrder, right-click, and select Generate Model Code.
    3. 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.
    4. Generate the model code

  2. Analyze the Customer.java source file.
    1. Open the Customer.java file available in the org.emf.tutorial.advanced.po package.
    2. 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.
    3. Notice Customer class

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

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

    8. Notice that two additional methods have been generated for the purchaseOrders feature based on the template insertion from step E.
    9. Notice add and remove methods


Summary

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