You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EMF入门开发者:Eclipse插件多EMF模型开发技术咨询

Hey there! Let's break down your questions step by step since you're just getting started with EMF—totally get the learning curve here.

1. 创建可在插件中可见的Ecore模型

Getting your Ecore model recognized by your Eclipse plugin involves a few key steps:

  • First, design your Ecore model: Use Eclipse's built-in Ecore Model Editor to create your .ecore file. Define all your EClasses, EAttributes, references, and set the correct namespace URI (this is critical for later registration).
  • Integrate the model into your plugin project:
    • Place your .ecore file in a dedicated folder like model/ inside your plugin project to keep things organized.
    • Open your plugin's MANIFEST.MF, go to the Runtime tab, and add the folder containing your .ecore file to the Bundle-Classpath (e.g., add . if the model/ folder is at the root, or explicitly add model/).
    • Register the model so EMF can find it at runtime: You have two solid options here:
      1. Generate model code with GenModel: Right-click your .ecore file → New > Other... > EMF Generator Model to create a .genmodel file. Then right-click the root node of the .genmodel → Generate Model Code. This generates Java classes (including a package implementation and factory) that automatically register your model when the plugin starts. Just make sure these generated classes are included in your plugin's build path.
      2. Manual registration in the plugin activator: If you prefer not to generate code, add registration logic to your plugin's Activator class's start() method:
        @Override
        public void start(BundleContext context) throws Exception {
            super.start(context);
            // Load the Ecore model from your plugin
            ResourceSet resourceSet = new ResourceSetImpl();
            URI modelUri = URI.createPlatformPluginURI("/your.plugin.id/model/your-model.ecore", true);
            Resource resource = resourceSet.getResource(modelUri, true);
            // Get the root EPackage from the model
            EPackage modelPackage = (EPackage) resource.getContents().get(0);
            // Register it in the global EPackage registry
            EPackage.Registry.INSTANCE.put(modelPackage.getNsURI(), modelPackage);
        }
        
        Replace your.plugin.id and your-model.ecore with your actual plugin ID and model file path.
2. 在插件运行时创建Ecore模型的EMF实例

Once your model is properly registered, creating instances is straightforward—here's how to do it in both Java and Xtend:

Java Implementation

  • If you generated model code: Use the auto-generated factory class directly:
    // Replace with your actual factory and EClass names
    MyModelFactory modelFactory = MyModelFactory.eINSTANCE;
    Person newPerson = modelFactory.createPerson();
    newPerson.setName("Johan");
    
  • If you registered manually: You'll need to fetch the EClass first, then create the instance:
    // Fetch your registered EPackage using its namespace URI
    EPackage modelPackage = EPackage.Registry.INSTANCE.getEPackage("http://your-namespace-uri.com/mymodel");
    // Get the EClass you want to instantiate
    EClass personClass = (EClass) modelPackage.getEClassifier("Person");
    // Create the instance
    EObject newPerson = EcoreUtil.create(personClass);
    // Set attributes using EMF's generic API
    newPerson.eSet(modelPackage.getEStructuralFeature("Person", "name"), "Johan");
    

Xtend Implementation

Xtend's syntax makes this even cleaner:

  • With generated model code:
    val modelFactory = MyModelFactory::eINSTANCE
    val newPerson = modelFactory.createPerson
    newPerson.name = "Johan"
    
  • With manual registration:
    val modelPackage = EPackage.Registry.INSTANCE.getEPackage("http://your-namespace-uri.com/mymodel")
    val personClass = modelPackage.getEClassifier("Person") as EClass
    val newPerson = EcoreUtil::create(personClass)
    newPerson.eSet(modelPackage.getEStructuralFeature("Person", "name"), "Johan")
    

A quick tip: If you run into issues where the model isn't found at runtime, double-check your MANIFEST.MF classpath entries, ensure the namespace URI in your .ecore matches what you're using for registration, and verify that your activator's start method is actually executing.

内容的提问来源于stack exchange,提问作者Johan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:35:53