Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Check eContainer for equality
Check eContainer for equality [message #800044] Thu, 16 February 2012 16:13 Go to next message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Hi @all!

I want to traverse my AST from my DSL for scoping reasons. I have a node somewhere and want to go up (in direction to the root) to some (grand)parent. I know the type of the (grand)parent. Since I make these calls more often, I have a generic function in order to search for the parent (in XTend):

def getParentOfType(EObject obj, Class clazz) {
   var parent = obj;
   while(!parent.eClass.instanceClass.equals(clazz))
        parent = parent.eContainer
   parent
}


Surely I could also call something like and work on nodes
val parent = NodeModelUtils::getNode(obj).semanticElement


The question is: Is there an easier way to check eContainers for type equality with a generic eClass? Or is the method above the "most" generic and short one?

The problem is that if call this function I have to write something like:

val x = some_eObject.getParentOfType(typeof(some_eClass)) as some_eClass


and this is not nice in my opinion.

Furthermore the function above returns null if no parent eContainer along the tree to the root is found. Since I use this function in local scope methods, I would need to return therefore not null but IScope::NULLSCOPE. Is there a way to return the NULLSCOPE by default, if null would have been returned?
Re: Check eContainer for equality [message #800076 is a reply to message #800044] Thu, 16 February 2012 16:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6550
Registered: July 2009
Senior Member
What about using getContainerOfType in Xtexts ECoreUtil2
Re: Check eContainer for equality [message #801038 is a reply to message #800076] Fri, 17 February 2012 21:02 Go to previous message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Cool, didn't know about these utilities. Thank you.
Previous Topic:Custom Formatter - minimising impact when grammar changes?
Next Topic:Problems writing Scope provider for enumeration literals
Goto Forum:
  


Current Time: Sat Dec 20 04:43:41 GMT 2014

Powered by FUDForum. Page generated in 0.01615 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software