Eclipse EMF: How to access EAttribute by name?

I have a Java method that is passed String and EObject EMF. String means the attribute name of the EObject. For example, if it was passed to "foo" and EObject eobj, it would need to access eobj.getFoo (). I know how to get the EAttibute value from my featureID, but cannot find a way to get it by attribute name. Is it possible?

+2
source share
1 answer

The following should do the trick, but it's not at all elegant. He gets eClassyou eObject, find an appropriate definition of the attribute by name and refers to him. Used here getEAllAttributes()also includes attributes defined by parent classes.

    EObject eObject = null;
    String attributeName = "";
    EDataType resultingDataType = null;
    EList<EAttribute> eAllAttributes = eObject.eClass().getEAllAttributes();
    for (EAttribute eAttribute : eAllAttributes) {
        if (eAttribute.getName().equals(attributeName)) {
            resultingDataType = (EDataType) eObject.eGet(eAttribute);
        }           
    }
    System.out.println(resultingDataType);
+3
source

All Articles