04. Generating the domain classes

4. Generating the domain classes #

See the YouTube playlist for all videos.

4.1. Generating Java code #

Based on the .genmodel files, you can generate Java code.

  • Right-click on the root node of the .genmodel file and select Generate Model Code. This creates the Java implementation of the EMF model in the current project.

EMFCodeGen10

Hint: Make sure your generated code compiles. Otherwise you probably did something wrong, e.g., give the wrong Java datatype for the datatype “calendar”.

4.2. Review the generated code #

The generated code will consist of the following:

  • de.vogella.emf.webpage.model.webpage — Interfaces and the Factory to create the Java classes
  • de.vogella.emf.webpage.model.webpage.impl — Concrete implementation of the interfaces defined in model
  • de.vogella.emf.webpage.model.webpage.util — The AdapterFactory

The central factory has methods for creating all defined objects via createObjectName() methods.

For each attribute the generated interface and its implementation contain getter and (if allowed in the model definition) setter methods. Each setter also has a generated notification to observers of the model. This means that other objects can attach them to the model and react to changes in the model.

Each generated interface extends the EObject interface. EObject is the base of every EMF class and is the EMF equivalent of java.lang.Object.EObject and its corresponding implementation class EObjectImpl provide a lightweight base class that lets the generated interfaces and classes participate in the EMF notification and persistence frameworks.

Every generated method is tagged with @generated. If you want to manually adjust the method and have EMF overwrite the method during the next generation run, you need to remove this tag.

4.3. Updating the model #

If you change your .ecoremodel then you can update the .genmodel simply by reloading.

EMFModel70