Eclipse EMF: How to get access EAttribute by name?

风格不统一 提交于 2019-12-12 14:26:51

问题


I have a Java method that is passed a String and an EMF EObject. The String is meant to be the name of an attribute of the EObject. For example, if it were passed "foo" and EObject eobj, it would need to access eobj.getFoo(). I know how to get the value of an EAttibute from its featureID, but can't seem to find a way to get it by attribute name. Is this even possible?


回答1:


The following should do the trick, but it is not elegant at all. It obtains the eClass of your eObject, finds a matching attribute definition by name and accesses it. The getEAllAttributes() used here 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);


来源:https://stackoverflow.com/questions/13370773/eclipse-emf-how-to-get-access-eattribute-by-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!