EMF中复制对象属性

1、简单的场景就是复制一个EObject,可以用工具类中的方法EcoreUtil.copy()。

2、场景:自己的TO类继承了EMF创建出的类,需要复制父类中的所有属性。

    /**
     * 将父类所有的属性COPY到子类中。 类定义中child一定要extends father;
     * @param father
     * @param child
     */
    public static void fatherToChild(EObject father, EObject child) {
        for (Field field : father.getClass().getDeclaredFields()) {
            EStructuralFeature feature = father.eClass().getEStructuralFeature(field.getName());
            if(feature!=null) {
                Object value = father.eGet(feature);
                child.eSet(feature, value);
            }
        }
    }    

 

posted @ 2015-01-09 17:20  demonrain  阅读(311)  评论(0)    收藏  举报