Search code examples
javaeclipse-emfeclipse-emf-ecore

Traverse ecore model


I have generated model code from Ecore. Within my model I have a derived reference: derivedThings.

What I want to do in the derivedThingsImpl is the following:

I want to traverse the whole model and depending on the element, I want to add it to a collection or not.

for(TreeIterator iter = EcoreUtil.getAllContents(rootObject); iter.hasNext();)
 ...

The problem is, how can I access the root object from the derivedThingsImpl ?! is there something like getRootObject() ?!

Thanks

UPDATE:

EObject e = this;
while(e.eContainer() != null) {
  e =  e.eContainer()
  if (e instanceof RootElement)
    break;
}
// No I should have the root element. Is this a good and clean way ?!

Solution

  • There is a better way of doing it:

    EcoreUtil.getRootContainer(eObject);