How can I programmatically add an EGeneric Type Argument to an EAttribute
?
I can create an EAttribute like this:
EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
eAttribute.setName("myAttribute");
EDataType dataType = EcorePackage.eINSTANCE.getEEList();
// add here String to List as generic argument?
eAttribute.setEType(dataType);
But with that code snippet the generic type argument of the EEList
is not specified. In Eclipse I would fix that with New Child > EGeneric Type Argument
and then set the EClassifier
of the EGeneric Type Argument to EString
. But how can I do that programmatically?
It took me some time but I have a solution:
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
EGenericType
is created from the EcoreFactory
. That is our EGeneric Type Argument. EString
. EGenericType
of the EAttribute
(NOT the EType
we set earlier).In hindsight it makes sense that we do not modify the EDataType
, we rather modifiy the EAttribute
.