Eclipse Search

Loading

May 5, 2008

Adding util methods to the generated EMF classes

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 Framew
ork");

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:

6 comments:

  1. Great Tips!
    And is it possible to use annotation in the value of an another annotation?

    What I mean : in the value of body key, I want to use the value of another key.

    Why? in order to extract information, because the code in the value for body key is not really readable, and maintainable if the my other value annotation will be evolved.
    ReplyDelete
  2. @scratsh,
    I've not tried, but that should be possible.
    ReplyDelete
  3. @Prakash G.R.
    Have you an idea how to proceed?

    modify templates?
    Set an escape character in the value? (some sort of #myRefToAnotherAnnotation)
    ReplyDelete
  4. @scratsh,
    Hmmm, haven't touched EMF in the past one year. Have to digg into the EMF classes to find the answer. Or probably someone in the newsgroup will help
    ReplyDelete
  5. asked in the newsgroup : http://www.eclipse.org/newsportal/article.php?id=1664&group=eclipse.modeling.m2t#1664
    ReplyDelete
  6. Works great. One thing though: in ecore editor (ecorediag) the annotation created this way does not show up. Why?
    Another thing: I tryed but can't make it work when create annotation different way: creat annotation in a scope of model (root package) and then link it to method. Is that possible?

    Thanks
    Alexey: alexeyy2@gmail.com
    ReplyDelete