Load EMF model instance with Xtend/Java [message #1846931] |
Sat, 09 October 2021 09:17  |
Eclipse User |
|
|
|
Hi all,
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()
}
}
Quote:
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!
|
|
|
|
|
|
|
Re: Load EMF model instance with Xtend/Java [message #1846951 is a reply to message #1846946] |
Sun, 10 October 2021 00:41  |
Eclipse User |
|
|
|
Hi
This is obviously impossible, so you are being deceived in what you observe.
Xtend auto-translates to Java and reverse presents the Java errors in the Xtend editor. If you open the underlying Java file you may get to see something that explains the problem. For instance a bad import can block a correct import.
I suspect that the auto-translate has been disabled in some way. The easiest way to fix it is to refresh then clean the project and if that doesn't work manually delete the xtend-gen folder. If that doesn't work clean all projects and restart Eclipse a couple of times. If that doesn't work start closing projects and you may find a copy and paste duplicate experiment that has polluted the index with confusion.
Regards
Ed Willink
|
|
|
Powered by
FUDForum. Page generated in 0.04417 seconds