[Xtend] Accessing EClass of a model element? [message #543725] |
Wed, 30 June 2010 08:26  |
Eclipse User |
|
|
|
Hello everybody,
within an Xtend transformation, is there an easy way to access a model element's EClass?
Imagine I have an .ecore file that contains EClasses A, B, C, and D.
Further imagine I have an instance of said .ecore file with model elements a1, a2, a3, b1, c1, c2.
...a1, a2, and a3 are of type A
...b1 is of type B and
...c1 and c2 are of type C.
Within an Xtend transformation I would like to identify all used EClasses. For instance, with the set myInstances = {a1, a2, a3, b1, c1, c2}, I'd like to derive a new set {A, B, C}.
Currently, I'm using the rather cumbersome way of
myInstances.collect(e|e.metaType).toSet()
then an element "aMetaType" of this derived set is processed the following way:
let typeName = aMetaType.toString() :
switch (typeName) {
case 'myPackage::A' : someExpression1
case 'myPackage::B' : someExpression2
case 'myPackage::C' : someExpression3
case 'myPackage::D' : someExpression4
default : expressionForUnknownType
};
(... e.metaType is of type xpand2::Type)
This solution depends on string equality which is not nice imho.
So, coming back to my initial question: what is the easiest and nicest way to get a model element's EClass?
Thanks, Abdull
|
|
|
|
|
|
Re: [Xtend] Accessing EClass of a model element? [message #544654 is a reply to message #543799] |
Mon, 05 July 2010 04:10  |
Eclipse User |
|
|
|
Abdull schrieb:
> Great, this works - thank you Christian!
>
> I guess I cannot use a property "myobject.getEClass()" directly (that
> is, I have to use your given JAVA extension), as the EMF public
> interface EObject exposes a method of name "eClass()", and not of name
> "getEClass()".
You could use the JavaBeansMetamodel instead of the EMF Metamodel.
To do so change the corresponging configuration in your workflow file as
well as in the preferences of your Eclipse workbench.
The main difference is that you use the java qualified names instead of
the shorter EMF namespaces (which boils down to a different import
statement in your Xtend and Xpand files).
Using JavaBeans Metamodel is better since it integrates better with
other Java types and Java extensions. Also the tooling is slightly
better since it is based on JDT and doesn't need the long running EMF
analyzers.
Sven
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
|
|
|
Powered by
FUDForum. Page generated in 0.06741 seconds