2017-01-03 72 views
1

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

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

回答

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. 线1EGenericType被从EcoreFactory创建。这是我们的EGeneric Type Argument。
  2. 现在,在第2行中,我们将EGeneric Type Argument的数据类型设置为EString
  3. 在最后一步,在线3,我们添加EGeneric类型参数的EAttribute(不是我们之前设置的EType)的EGenericType

事后看来这是有道理的,我们没有修改EDataType,我们宁可modifiy的EAttribute

相关问题