我通过带注释的Java代码使用EMF,如下所示
/**
* Adds the given type to this filter. Has no effect if the given type
* already belongs to this filter.
*
* @param type
* the type to add
* @model
*/
public void addEntityType(String type);
/**
* Returns the list of types belonging to this filter. Types are identified
* by there name.
*
* @return the list of types for this entity type filter
*
* @model
*/
public List<String> getEntityTypes();
/**
* Removes the given type from this filter. Has no effect if the given type
* doesn't belong to this filter.
*
* @param type
* the type to remove
* @model
*/
public void removeEntityType(String type);
从此带注释的界面创建ecore和genmodel文件后,生成代码后,getEntityTypes方法修改如下:
public EList<String> getEntityTypes();
出于封装的目的,我希望这个EList是不可修改的,因此接口客户端的代码只能通过添加和删除方法来修改列表。
有没有干净的方法来修改Java注释或genmodel文件以告诉生成器生成返回不可修改列表的代码? (谷歌搜索后我无法找到...)
你如何处理这种情况?
提前致谢
马努
答案 0 :(得分:4)
您需要修改生成的“Impl”类,如下所示:
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
private EList<String> getEntityTypesGen() {
if (entityTypes == null) {
entityTypes = new EDataTypeUniqueEList<String>(String.class,
this, NsPackage.THINGY__ENTITY_TYPES);
}
return entityTypes;
}
public EList<String> getEntityTypes() {
return ECollections.unmodifiableEList(getEntityTypesGen());
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
public void addEntityType(String type) {
getEntityTypesGen().add(type);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
public void removeEntityType(String type) {
getEntityTypesGen().remove(type);
}
请注意,我已完成以下操作:
但就个人而言,我不建议采用这种方法。 EMF通常返回多值引用的可修改列表,客户端应该修改这些列表以添加或删除项目。 EMF将根据需要懒洋洋地创建一个空列表,因此它可以实现更清晰的界面(不需要添加/删除方法)和一个不错的API(用户可以轻松获得列表API的全部功能,而不仅仅是添加/删除你提供的。)