【问题标题】:How do I programmatically add an EGeneric Type Argument to an EAttribute?如何以编程方式将通用类型参数添加到属性?
【发布时间】:2017-05-17 13:48:45
【问题描述】:

如何以编程方式将 EGeneric 类型参数添加到 EAttribute? 我可以像这样创建一个 EAttribute:

EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
eAttribute.setName("myAttribute");
EDataType dataType = EcorePackage.eINSTANCE.getEEList();
// add here String to List as generic argument?
eAttribute.setEType(dataType);

但是使用该代码 sn-p 没有指定 EEList 的泛型类型参数。在 Eclipse 中,我将使用 New Child > EGeneric Type Argument 修复它,然后将 EGeneric 类型参数的 EClassifier 设置为 EString。但我怎样才能以编程方式做到这一点?

最后,属性应该如下所示:

【问题讨论】:

    标签: java eclipse-emf emf eclipse-emf-ecore


    【解决方案1】:

    我花了一些时间,但我有一个解决方案:

    EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
    eAttribute.setName("myAttribute");
    eAttribute.setEType(EcorePackage.eINSTANCE.getEEList());
    // This is the interesting part:
    EGenericType eGenericTypeArgument = ecoreFactory.createEGenericType(); // line 1
    eGenericTypeArgument.setEClassifier(EcorePackage.eINSTANCE.getEString()); // line 2
    eAttribute.getEGenericType().getETypeArguments().add(eTypeArgument); // line 3
    
    1. 第 1 行中,从 EcoreFactory 创建了一个新的 EGenericType。那就是我们的 EGeneric 类型参数。
    2. 现在,在 第 2 行,我们将 EGeneric 类型参数的数据类型设置为 EString
    3. 在最后一步中,在 第 3 行,我们将 EGeneric 类型参数添加到 EAttributeEGenericType(不是我们之前设置的 EType)。

    事后看来,我们不修改EDataType,而是修改EAttribute

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2012-12-13
      • 2019-05-28
      相关资源
      最近更新 更多