javaeclipse-emfemfeclipse-emf-ecore

How do I programmatically add an EGeneric Type Argument to an EAttribute?


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?

At the end, the attribute should look like this: EAttribute Tree View


Solution

  • 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
    
    1. In line 1 a new EGenericType is created from the EcoreFactory. That is our EGeneric Type Argument.
    2. Now, in line 2, we set the data type of the EGeneric Type Argument to EString.
    3. In the last step, in line 3, we add the EGeneric Type Argument to the 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.