如何以编程方式将EGeneric Type Argument添加到EAttribute?

时间:2017-01-03 12:55:18

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

如何以编程方式将{EGeneric Type Argument}添加到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);

但是使用该代码片段,未指定EEList的泛型类型参数。在Eclipse中,我将使用New Child > EGeneric Type Argument修复此问题,然后将EGeneric Type Argument的EClassifier设置为EString。但是我怎么能以编程方式做到这一点?

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

1 个答案:

答案 0 :(得分:2)

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

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行中,从EGenericType创建新的EcoreFactory。这是我们的EGeneric Type Argument。
  2. 现在,在第2行中,我们将EGeneric Type Argument的数据类型设置为EString
  3. 在最后一步中,在第3行中,我们将EGeneric Type Argument添加到EGenericType的{​​{1}}(不是我们之前设置的EAttribute )。
  4. 事后看来,我们不修改EType是有道理的,我们宁愿修改EDataType

相关问题