I have this simple code snippet which throws:
Exception in thread "main" java.lang.ClassCastException: org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl cannot be cast to org.eclipse.emf.ecore.resource.Resource$Factory
relevant code:
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.Resource;
public class Graphe {
public static void main(String[] args) {
org.eclipse.emf.ecore.resource.Resource.Factory.Registry reg=org.eclipse.emf.ecore.resource.Resource.Factory.Registry.INSTANCE;
Map<String, Object> myMap=reg.getExtensionToFactoryMap();
myMap.put("graphes", new XMIResourceImpl());
ResourceSet resSet=new ResourceSetImpl();
Resource res= resSet.getResource(URI.createURI("My.graphes"), true);//exception here
}
}
the second line is throwing the exception. My.Graphes is a plugin that I created with EMF and exported it to eclipse host repository. Any help would be so much appreciated
The exception you are experiencing is due to the fact that the getExtensionToFactoryMap
deals with Factory
as map value, and not Resource
. It maps the extension of your URI
to a Resource Factory
that will create the right Resource
.
To deal with your issue, you simply need to register the XMIResourceFactoryImpl
instead of the XMIResourceImpl
:
myMap.put("graphes", new XMIResourceFactoryImpl());