返回 EMF 不可修改列表的生成方法
我通过带注释的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文件来告诉生成器生成返回不可修改列表的代码? (谷歌搜索后我无法找到......)
你如何处理这种情况?
预先感
谢马努
I am using EMF through annotated Java code as following
/**
* 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);
After creating ecore and genmodel files from this annotated interface, and after generating code the getEntityTypes method is modified as following:
public EList<String> getEntityTypes();
For encapsulation purpose I want this EList to be unmodifiable, thus the interface client's code can only modify the list through add and remove methods.
Is there any clean way to do that I.e modifying Java annotation or the genmodel file to tell the generator to generate code returning unmodifiable list ? (I was unable to find that after googling ...)
How do you manage such situations ?
Thanks in advance
Manu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将生成的“Impl”类修改为如下所示:
请注意,我已完成以下操作:
但就我个人而言,我不推荐这种方法。 EMF 通常返回多值引用的可修改列表,客户端应该修改这些列表以添加或删除项目。 EMF 将根据需要惰性地创建一个空列表,因此它可以提供更清晰的界面(不需要添加/删除方法)和一个漂亮的 API(用户可以轻松使用列表 API 的全部功能,而不仅仅是添加/删除方法)您提供的)。
You would need to modify your generated "Impl" class to look like this:
Note that I've done the following:
Personally, though, I wouldn't recommend this approach. EMF generally returns modifiable lists for multi-valued references which clients are supposed to modify in order to add or remove items. EMF will lazily create an empty list as needed, so it makes for a cleaner interface (don't need add/remove methods) and a nice API (user has full power of the list API at their fingertips instead of just the add/remove that you provide).