We had EMF generated model classes and a Util class with lot of helper methods. Both were exposed to clients as an API. Most of the methods in the Util classes were like:
//API - Util class
Book getBook(Writer writer, String title);
//Customer code
Util.getBook(daveSteinberg, "Eclipse Modeling Framework");
These methods would have been more appropriate in the Writer class itself rather than in a Util class, and it will be more natural to code like:
daveSteinberg.getBook("Eclipse Modeling Framework");
This tip is about how to add such methods in the generated code.
The first way is simple. Just edit the generated java code and add these methods. EMF is smart enough to identify this method and keeps it safe during regeneration of code. But if you were like me, who consider the generated Java files are as good as class files and don't want them checked into the repository, you can follow the second way.
In the eCore Editor for your model, right click the EClass and add a new child EOperation.
Go to the Properties view and specify the name, this will be the name of your method. The EType represents the return type of the method.
If your method has any parameters, then add EParameter children to the EOperation & specify their types and names.

To add the code, add an EAnnotation to the EOperation.
In the properties view, set the Source to "http://www.eclipse.org/emf/2002/GenModel".
Add a details entry to the EAnnotation.
In the properties view set the key to 'body' and value to the code that you want to be generated.
Reload your .genmodel from .ecore and generate the code:
May 5, 2008
Adding util methods to the generated EMF classes
This blog has moved. Click here for an updated version of this blog entry
Posted by
Prakash G.R.
at
10:26 PM
Labels: eclipse, EMF, guidelines
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment