[XPAND] Traverse all elements of a class [message #873604] |
Fri, 18 May 2012 12:28  |
Eclipse User |
|
|
|
Hey there,
I'm looking for a feature in XPAND to list/access all attributes of a given class.
I could only find old entries suggesting the following statement: "«FOREACH allElements() ...", but it seems to be obsolete.
Is there any way to achieve this functionallity with the current release?
Thank you,
Søren
|
|
|
|
Re: [XPAND] Traverse all elements of a class [message #873640 is a reply to message #873611] |
Fri, 18 May 2012 14:32   |
Eclipse User |
|
|
|
Hi Christian,
thanks for your answer!
I tried a few things, but unfortunately I didn't get things done.
Could you please provide me with further instructions/documentation to keep going on?
In particular, after I type «ecore::EClass.» there's no code-completion anymore and the suggested method(s) you mentioned can't be found.
The relevant template-part looks like this:
..
«DEFINE buildWidget FOR Widget»
<«this.keyword»>
// this is where the magic should happen
</«this.keyword»>
«ENDDEFINE»
..
Maybe some background information:
I have modeled an abstract superclass "Widget" that has LOTS of subclasses. Some common attributes (id, height, width, ...) are modeled in the class Widget. It'd be a horror-scenario to write a template for every subclass, so I'm looking for a way to approach something similar with XPand:
<pseudo-code>
for (attribute : getAllAttributes()) {
print(attribute, getValue(attribute))
}
</pseudo-code>
Thanks a lot,
Søren
|
|
|
Re: [XPAND] Traverse all elements of a class [message #873652 is a reply to message #873640] |
Fri, 18 May 2012 15:11  |
Eclipse User |
|
|
|
Hi,
your snippet makes no sense to me. btw wich metamodel kind do you use? (java / emf(registry) = dynamic emf)
dynamic emf does not allow this kind of reflection. so you may have to deal with xpands reflection
this.metatype.....
or you use a javaextension
ecore::EClass getEClass(emf::EObject o):
JAVA template.Helper.getEClass(org.eclipse.emf.ecore.EObject);
Object eGet(emf::EObject o, ecore::EStructuralFeature f) :
JAVA template.Helper.eGet(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EStructuralFeature);
package template;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
public class Helper {
public static EClass getEClass(EObject o) {
return o.eClass();
}
public static Object eGet(EObject o, EStructuralFeature f) {
return o.eGet(f);
}
}
«FILE name+".txt"»
«FOREACH this.getEClass().eAllAttributes AS a»
«a.name» «eGet(a)»
«ENDFOREACH»
«ENDFILE»
alternatively: if you can use the javabeansmetamodel it will work out of the box
~Christian
|
|
|
Powered by
FUDForum. Page generated in 0.24323 seconds