I am writing a code to get all the contents of an EMF model instance. I tried to follow this tutorial that I found here: Create and modify ecore files and their instances from code in order to load the model.
In this tutorial, the model instance is an XMI file. In my case, I have a .myExt file (the extension I decided upon when I created the metamodel to which this model instance conforms to in ecore). I wrote the following code, but I get the following error:
class GetModelObjects {
ResourceSet resourceSet = new ResourceSetImpl()
Resource metamodel
EPackage mp
Resource model
EFactory mappinginstance
//EList <EObject> modelobjects = new BasicEList<EObject>()
def doTransform() {
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("ecore", new EcoreResourceFactoryImpl)
resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put("myext", new MyExtFactoryImpl)
metamodel = resourceSet.getResource(URI.createFileURI("myext.ecore"), true)
mp = metamodel.contents.get(0) as EPackage
resourceSet.packageRegistry.put("http://www.example.org/myExt", metamodel)
model = resourceSet.getResource(URI.createURI("model.myext"), true)
mappinginstance = mp.getEFactoryInstance
System.out.println("Model:" + model.contents)
}
def static void main(String[] args) {
new GetModelObjects().doTransform()
}
}
class myExt.impl.MyExtFactoryImpl cannot be cast to class org.eclipse.emf.ecore.resource.Resource$Factory (myExt.impl.MyExtFactoryImpl
and org.eclipse.emf.ecore.resource.Resource$Factory are in unnamed module of loader 'app')
One thing I am not quite sure about, and I think might be the reason for this error, is this line:
resourceSet. resourceFactoryRegistry.extensionToFactoryMap.put("myext", new MyExtFactoryImpl)
Because I am not sure if this is the factory that should be placed here. I would really appreciate any input on this.
Thank you in advance!
For reference, use Vogella's tutorials: https://www.vogella.com/tutorials/EclipseEMF/article.html#load-an-existing-model
The error message you are getting is a bit weird, I would check whether MyExtFactoryImpl (which should be the generated factory implementation for your metamodel) works (e.g. MyExtFactoryImpl.eINSTANCE.createXXX()) as intended. Also the error looks like it may be related to Java modules so check up on that (is EMF Resource on your classpath etc?)