Tuesday, November 12, 2013

Making Annotation part of the model when generating EMF model

When generating EMF model from XSD or otherwise it is beneficial to make annotations such as documentation part of the model. This information can be used to communicate to the user what particular model object does. Unfortunately while this seems like a easy task it did not work for me right away.
 There is a nifty class EcoreUtil that has vast amount of information that you can get from your model. One of the methods that was useful to me was EcoreUtil.getDocumentation(EModelElement element) and to use it all I need to do is:
EObject myObj = ...
String documentation = EcoreUtil.getDocumentation(myObj.eClass());
Simple enough, but it did not work. I was always getting null for documentation. Looking further and doing some debugging in getDocumentation() method I saw that first line was:
protected static final String GEN_MODEL_PACKAGE_NS_URI = "http://www.eclipse.org/emf/2002/GenModel";
...
EAnnotation eAnnotation = eModelElement.getEAnnotation(GEN_MODEL_PACKAGE_NS_URI);
Which suggests that obviously code should look for annotations with GEN_MODEL_PACKAGE_NS_URI.
However, I was just not getting my annotation which in fact had URI of http://www.eclipse.org/emf/2002/GenModel. Looking in GenModel class that is responsible for generating my model, there was a method GenModel.setSuppressGenModelAnnotations(boolean bool). Naming of the method suggests what it does and in fact in my genmodel's Model properties there is an entry 'Suppress GenModel Annotations' which is set to 'true' by default. Setting this property to false generated my annotations. I am still not sure why it's set to 'true' by default as setting to 'false' would make more sense in my opinion.

No comments:

Post a Comment

Blogger Syntax Highliter