2.4 Generating Code
The most important benefit of EMF, as with modeling in general, is the boost in productivity that results from automatic code generation. Let's say that you've defined a model, for example the purchase order Ecore model shown in Section 2.3.3, and are ready to turn it into Java code. What do you do now? In Chapter 4, we'll walk through this scenario and others where you start with other forms of the model (e.g., Java interfaces). For now, suffice to say that it only involves a few mouse clicks. All you need to do is create a project using the EMF Project wizard, which automatically launches the generator, and select Generate Model Code from a menu.
2.4.1 Generated Model Classes
So what kind of code does EMF generate? The first thing to notice is that an Ecore class (i.e., an EClass) actually corresponds to two things in Java: an interface and a corresponding implementation class. For example, the EClass for PurchaseOrder maps to a Java interface:
public interface PurchaseOrder ...
and a corresponding implementation class:
public class PurchaseOrderImpl extends ... implements PurchaseOrder {
This interface–implementation separation is a design choice favored by EMF. Why do we advocate this approach? The reason is simply that we believe it's the best pattern for any model-like API. For example, the Document Object Model (DOM) is like this and so is much of Eclipse. It's also a necessary pattern to support multiple inheritance in Java.
The next thing to notice about each generated interface is that it extends directly or indirectly from the base interface EObject like this:
public interface PurchaseOrder extends EObject {
EObject is the EMF equivalent of java.lang.Object; that is, it's the base of all modeled objects. Extending EObject introduces three main behaviors:
- eClass() returns the object's metaobject (an EClass).
- eContainer() and eResource() return the object's containing object and resource.
- eGet(), eSet(), eIsSet(), and eUnset() provide an API for accessing the objects reflectively.
The first and third items are interesting only if you want to generically access the objects instead of, or in addition to, using the type-safe generated accessors. We'll look at how this works in Sections 2.5.3 and 2.5.4. The second item is an integral part of the persistence API that we will describe in Section 2.5.2.
Other than that, EObject has only a few convenience methods. However, there is one more important thing to notice; EObject extends yet another interface:
public interface EObject extends Notifier {
The Notifier interface is also quite small, but it introduces an important characteristic to every modeled object; model change notification as in the Observer design pattern [3]. Like object persistence, notification is an important feature of an EMF object. We'll look at EMF notification in more detail in Section 2.5.1.
Let's move on to the generated methods. The exact pattern that is used for any given feature (i.e., attribute or reference) implementation depends on the type and other user-settable properties. In general, the features are implemented as you'd expect. For example, the get() method for the shipTo attribute simply returns an instance variable like this:
public String getShipTo() { return shipTo; }
The corresponding set() method sets the same variable, but it also sends a notification to any observers that might be interested in the state change:
public void setShipTo(String newShipTo) { String oldShipTo = shipTo; shipTo = newShipTo; if (eNotificationRequired()) eNotify(new ENotificationImpl(this, Notification.SET, POPackage.PURCHASE_ORDER__SHIP_TO, oldShipTo, shipTo)); }
Notice that, to make this method more efficient when the object has no observers, the relatively expensive call to eNotify() is avoided by the eNotificationRequired() guard.
More complicated patterns are generated for other types of features, especially bidirectional references where referential integrity is maintained. In all cases, however, the code is generally as efficient as possible, given the intended semantic. We'll cover the complete set of generator patterns in Chapter 10.
The main message you should go away with is that the generated code is clean, simple, and efficient. EMF does not pull in large base classes, or generate inefficient code. EMF's runtime framework is lightweight, as are the objects generated for your model. The idea is that the code that's generated should look pretty much like what you would have written, had you done it by hand. However, because it's generated, you know it's correct. It's a big time saver, especially for some of the more complicated bidirectional reference handshaking code, which might otherwise be fairly difficult to get right.
Before moving on, we should mention two other important classes that are generated for a model: a factory and a package. The generated factory (e.g., POFactory) includes a create method for each class in the model. The EMF programming model strongly encourages, but doesn't require, the use of factories for creating objects. Instead of simply using the new operator to create a purchase order, you should do this:
PurchaseOrder aPurchaseOrder = POFactory.eINSTANCE.createPurchaseOrder();
The generated package (e.g., POPackage) provides convenient accessors for all the Ecore metadata for the model. You might already have noticed, in the implementation of setShipTo() shown earlier, the use of POPackage. PURCHASE_ORDER__SHIP_TO, a static int constant representing the shipTo attribute. The generated package also includes convenient accessors for the EClasses, EAttributes, and EReferences. We'll look at the use of these accessors in Section 2.5.3.
2.4.2 Other Generated "Stuff"
In addition to the interfaces and classes described in the previous section, the EMF generator can optionally generate the following:
- A skeleton adapter factory10 class (e.g., POAdapterFactory) for the model. This convenient base class can be used to implement adapter factories that need to create type-specific adapters; for example, a PurchaseOrderAdapter for PurchaseOrders, an ItemAdapter for Items, and so on.
- A convenience switch class (e.g., POSwitch) that implements a "switch statement"-like callback mechanism for dispatching based on an object's type (i.e., its EClass). The adapter factory class, as just described, uses this switch class in its implementation.
- Plug-in manifest files and property files, so that the model can be used as an Eclipse plug-in.
If all you're interested in is generating a model, this is the end of the story. However, as we'll see in Chapters 3 and 4, the EMF generator can, using the EMF.Edit extensions to the EMF core, generate adapter classes that enable viewing and command-based, undoable editing of a model. It can even generate a complete working editor for your model. We will talk more about EMF.Edit and its capabilities in the following chapter. For now, we just stick to the basic modeling framework itself.
2.4.3 Regeneration and Merge
The EMF generator produces files that are intended to be a combination of generated pieces and handwritten pieces. You are expected to edit the generated classes to add methods and instance variables. You can always regenerate from the model as needed and your additions will be preserved during the regeneration.
EMF uses @generated markers in the Javadoc comments of generated interfaces, classes, methods, and fields to identify the generated parts. For example, getShipTo() actually looks like this:
/** * @generated */ public String getShipTo() { ...
Any method that doesn't have this @generated tag (i.e., anything you add by hand) will be left alone during regeneration. If you already have a method in a class that conflicts with a generated method, your version will take precedence and the generated one will be discarded. You can, however, redirect a generated method if you want to override it but still call the generated version. If, for example, you rename the getShipTo() method with a Gen suffix:
/** * @generated */ public String getShipToGen() { ...
Then if you add your own getShipTo() method without an @generated tag, the generator will, on detecting the conflict, check for the corresponding Gen version and, if it finds one, redirect the generated method body there.
The merge behavior for other things is generally reasonable. For example, you can add extra interfaces to the extends clause of a generated interface (or the implements clause of a generated class) and specify that they should be retained during regeneration. The single extends class of a generated class, however, will always be overwritten by the model's choice. We'll look at code merging in more detail in Chapter 10.
2.4.4 The Generator Model
Most of the data needed by the EMF generator is stored in the Ecore model. As we saw in Section 2.3.1, the classes to be generated and their names, attributes, and references are all there. There is, however, more information that needs to be provided to the generator, such as where to put the generated code and what prefix to use for the generated factory and package class names, that isn't stored in the Ecore model. All this user-settable data also needs to be saved somewhere so that it will be available if we regenerate the model in the future.
The EMF code generator uses a generator model to store this information. Like Ecore, the generator model is itself an EMF model. Actually, a generator model provides access to all of the data needed for generation, including the Ecore part, by wrapping the corresponding Ecore model. That is, generator model classes are decorators [3] of Ecore classes. For example, GenClass decorates EClass, GenFeature decorates EAttribute and EReference, and so on.
The significance of all this is that the EMF generator runs off of a generator model instead of an Ecore model; it's actually a generator model editor.11 When you use the generator, you'll be editing a generator model, which in turn indirectly accesses the Ecore model from which you're generating. As you'll see in Chapter 4 when we walk through an example of using the generator, there are two model resources (files) in the project: an .ecore file and a .genmodel file. The .ecore file is an XMI serialization of the Ecore model, as we saw in Section 2.3.3. The .genmodel file is a serialized generator model with cross-document references to the .ecore file. Figure 2.6 shows the conceptual picture.
Figure 2.6 The .genmodel and .ecore files.
Separating the generator model from the Ecore model like this has the advantage that the actual Ecore metamodel can remain pure and independent of any information that is only relevant for code generation. The disadvantage of not storing all the information right in the Ecore model is that a generator model might get out of sync if the referenced Ecore model changes. To handle this, the generator model elements are able to automatically reconcile themselves with changes to their corresponding Ecore elements. Users don't need to worry about it.