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
getExtensionToFactoryMapdeals withFactoryas map value, and notResource. It maps the extension of yourURIto aResource Factorythat will create the rightResource.To deal with your issue, you simply need to register the
XMIResourceFactoryImplinstead of theXMIResourceImpl: