I have an Ecore model in an existing EMF project and want to print the name of all containing classes into a text file via XTend. How do you achieve this? The XTend examples don't show how to use a model and get Information out of it.
-
have a look at [Load EMF Model instance in XTend](http://stackoverflow.com/questions/12458852/load-emf-model-instance-in-xtend) – Mad Matts Aug 31 '16 at 11:00
-
I don't need an instance of the model. I just need the classes of the metamodel. – landunder Aug 31 '16 at 11:19
1 Answers
If you only need the EClasses of your Meta-Model then you can get them from your Model Package:
YourEMFModelPackage.eINSTANCE.getEClassifiers()
which returns a EList<EClassifier>
. Since an EClass
is an EClassifier
you get all your EClass implementations org.eclipse.emf.ecore.impl.EClassImpl
.
For type-safety concerns you probably check if this List only contains EClasses, since all your EDataTypes
are also EClassifier
.
So this should to the Trick:
EcoreUtil.getObjectsByType(YourEMFModelPackage.eINSTANCE.getEClassifiers(), EcorePackage.eINSTANCE.getEClass())
or:
List<EClass> allEClasses = YourEMFModelPackage.eINSTANCE.getEClassifiers().stream().filter(p -> EClass.class.isInstance(p)).map(m -> EClass.class.cast(m)).collect(Collectors.toList());
Update: If you don't have your Model-Code generated you're still able to do this, you only need to load your Ecore into a Resource:
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore",
new EcoreResourceFactoryImpl());
Resource resource = resourceSet.getResource(
URI.createFileURI(
"../path/to/your/Ecore.ecore"),
true);
EPackage model = (EPackage) resource.getContents().get(0);
If you have the EPackage
then you get your EClass
like mentioned above

- 1,118
- 10
- 14
-
I have the Ecore model in the model folder. I tried to address it by its package name in the Java file like YourEMFModelPackage but it doesn't work. What am I doing wrong? – landunder Aug 31 '16 at 11:54
-
This question implies that it is not possible to work with an Ecore model without generating the code, is this correct? – landunder Aug 31 '16 at 12:03
-
Yes you don't actually need Java code from your model to do this, I updated my answer – Mad Matts Aug 31 '16 at 12:09
-
The loading into a ressource works. But I can't get the .eINSTANCE of the package. It tells me that it can't be resolved or isn't a field. – landunder Aug 31 '16 at 12:45
-
1`resource.getContents().get(0)` returns an `EPackage` so you should be able to call `getEClassifiers()` on it – Mad Matts Aug 31 '16 at 13:11
-
-
Hows does one achieve the same, but with an xcore, rather than ecore, metamodel? – Magick Apr 27 '17 at 21:37