Generics in Eclipse Modelling Framework
Java introduced the power of generics in version 1.5. Generics make many common tasks in programming not only simple but elegant. The most notable instance is the task of iterating lists. Prior to Java 1.5, one had to type cast a list in iteration as shown in Listing 1.
List fruits = new ArrayList();
…
for (int i =0; i < fruits.size(); i++) {
Fruit fruit = (Fruit) fruits.get(i);
…
}
Listing 1
With generics, iterating through the list is far less cumbersome. Equivalent code is shown in Listing 2.
List<Fruit> fruits = new ArrayList<Fruit>();
…
for (Fruit fruit : fruits) {
…
}
Listing 2
In this article, we do not have the space and time to delve into the full background of Java generics. There are some good references available on line such as the tutorial by Gilad Bracha. We will instead focus on how to implement generics in the Eclipse Modeling Framework (EMF).
For our first example, we will use a trivial model of fruit and fruit baskets. Figure 1 shows the basic model in the Ecore editor. The objects Fruit and Basket are abstract. The Basket class has an attribute contents which provides a list of Fruit objects.
Figure 1: Basic Ecore Model
We would like to enforce that AppleBasket only contains Apple objects. Before generics, we would have two choices, neither of them preferable. First, we could overwrite the contents method on the AppleBasket to check that the Fruit object was an Apple. Second, we could create a method named getApples on the AppleBasket. Both approaches compromise the advantages and reusability of the interfaces in our object-oriented design.
To add generics to our Ecore model, we must turn on the “Show Generics” option. You will find this on the “Sample Ecore Editor” menu
Figure 2: Enabling Ecore for Generics
Now you will notice that the context menu in the editor (right click) has more choices.
We will add the generic to our Basket class. Right-click on the Basket class and select “New Child::Etype Parameter”. In the properties view, give the parameter a simple name like “F”. Such is the naming convention of Java generics. Select the contents attribute and change its EType to F. Figure 3 shows the results. Notice that the model will display “contents: F” but the properties will display “EObject.”
Figure 3: Initial Generics Declaration
If we generate our EMF code from the model, we will see the following in the Basket interface.
public interface Basket<F> extends EObject {
public EList<F> getContents();
}Listing 3
- Login or register to post comments
- 3004 reads
- Email this Article
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






