如何以编程方式将 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);
但是在该代码片段中,未指定 EEList 的泛型类型参数。在 Eclipse 中,我将使用 New Child > EGeneric Type Argument
修复该问题,然后将 EGeneric Type Argument 的 EClassifier
设置为 EString
。但我怎样才能以编程方式做到这一点呢?
最佳答案
我花了一些时间,但我有一个解决方案:
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 行中添加一个新的
EGenericType
是从EcoreFactory
创建的。这就是我们的 EGeneric 类型参数。 - 现在,在第 2 行中,我们将 EGeneric Type Argument 的数据类型设置为
EString
。 - 在最后一步中,在第 3 行中,我们将 EGeneric 类型参数添加到
EGenericType
EAttribute
的(不是我们之前设置的EType
)。
事后看来,我们不修改 EDataType
是有道理的。 ,我们宁愿修改 EAttribute
.
关于java - 如何以编程方式将通用类型参数添加到属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41444023/