2017-01-03 61 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

相關問題