要做這種事情,你必須處理元模型和EFactory
。通過查看EStructuralFeature
中的setDefaultValue
,可以看到EStructuralFeature
類型的EFactory
用於生成該值(僅當EStructuralFeature
類型爲EDatatype
時)。
這是一個普遍的片斷(我們假設我們有一個EObject eobj
):
// We get the estructuralfeature
EStructuralFeature feature = eobj.eClass().getEStructuralFeature("myfeature");
// Is the feature type "primitive"
if (feature.getEType() instanceof EDataType) {
EDataType dtype = (EDataType)ea.getEType();
// We get the EFactory
EFactory factory = feature.getEType().getEPackage().getEFactoryInstance();
eobj.eSet(feature, factory.createFromString(dtype, "mystringvalue"));
}
這裏是UML的例子:
Property p = UMLFactory.eINSTANCE.createProperty();
EStructuralFeature ea = p.eClass().getEStructuralFeature("lower");
... // if and stuffs
EFactory factory = ea.getEType().getEPackage().getEFactoryInstance();
p.eSet(ea, factory.createFromString((EDataType)ea.getEType(), "1"));