将 eclipse.emf.ecore.xmi.impl.XMIResourceImpl 转换为 eclipse.emf.ecore.resource.Resource$Factory
cast eclipse.emf.ecore.xmi.impl.XMIResourceImpl to eclipse.emf.ecore.resource.Resource$Factory
我有这个简单的代码片段,它会抛出:
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 Factory
,这将创建正确的 Resource
.
要处理您的问题,您只需注册 XMIResourceFactoryImpl
而不是 XMIResourceImpl
:
myMap.put("graphes", new XMIResourceFactoryImpl());
我有这个简单的代码片段,它会抛出:
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 Factory
,这将创建正确的 Resource
.
要处理您的问题,您只需注册 XMIResourceFactoryImpl
而不是 XMIResourceImpl
:
myMap.put("graphes", new XMIResourceFactoryImpl());