If you have an EObject
, you may get the desired result just by recursively checking the eContainer()
. Such as:
public static EObject getRoot(EObject eo) {
EObject parent = eo.eContainer();
if (parent != null) {
return getRoot(parent);
}
return eo;
}
For any EObject
this should return the top-level EObject
that contains it.
But instead of rolling your own like that, you may want to rely on getRootContainer()
in the EcoreUtil
class. Don't overlook EcoreUtil
when working with EMF, it has helper methods at least some of which are bound to be useful in an EMF application.