您应该通过扩展EcoreUtil.Copier来定义自己的 EMF 复印机。
这样,您可以覆盖默认的 Copier 行为,并使用一些自定义行为处理感兴趣的 EStructuralFeature。
class PlaneCopier extends Copier {
int motorType;
public EObject copy(EObject eObject, int motorType) {
this.motorType = motorType
return super.copy(eObject);
}
@Override
protected void copyAttribute(EAttribute eAttribute, EObject eObject, EObject copyEObject) {
if (eAttribute.equals(YouEMFPackage.Literals.PLANE__MOTOR_TYPE)) {
copyEObject.eSet(YouEMFPackage.Literals.PLANE__MOTOR_TYPE, motorType);
} else {
super.copyAttribute(eAttribute, eObject, copyEObject);
}
}
}
并在循环中使用它:
PlaneCopier copier = new PlaneCopier();
Plane templatePlane = ...
int motorType = 0;
for (var i=0; i<nbPlanes; i++) {
motorType += 10;
newPlane = copier.copy(templatePlane, motorType);
}