我有一个简单的代码片段,它抛出:
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
相关代码:
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
}
}
第二行是引发异常。My.Graphes 是我用 EMF 创建的插件,并将其导出到 eclipse 主机存储库。任何帮助将不胜感激
您
遇到的异常是由于getExtensionToFactoryMap
将Factory
作为映射值处理,而不是Resource
。它将URI
的扩展映射到将创建正确Resource
的Resource Factory
。
要处理您的问题,您只需注册XMIResourceFactoryImpl
而不是XMIResourceImpl
:
myMap.put("graphes", new XMIResourceFactoryImpl());