返回 EMF 不可修改列表的生成方法

发布于 2024-07-23 15:16:48 字数 1041 浏览 2 评论 0原文

我通过带注释的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2024-07-30 15:16:48

您需要将生成的“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);
}

请注意,我已完成以下操作:

  1. 将生成的 getEntityTypes 方法的名称和可见性分别更改为 getEntityTypesGen 和 private。 EMF 重新生成此方法时不会扰乱可见性。 此外,即使我们现在有一个未生成的 getEntityTypes 方法,EMF 将继续生成此后缀为“Gen”的方法。
  2. 添加了一个公共的、非生成的 getEntityTypes 方法,该方法将默认实现的结果包装在不可修改的 EList 中。
  3. 通过委托生成的 getEntityTypesGen 方法(其结果仍然可修改)来实现(并更改为非生成)add/removeEntityType 方法。

但就我个人而言,我不推荐这种方法。 EMF 通常返回多值引用的可修改列表,客户端应该修改这些列表以添加或删除项目。 EMF 将根据需要惰性地创建一个空列表,因此它可以提供更清晰的界面(不需要添加/删除方法)和一个漂亮的 API(用户可以轻松使用列表 API 的全部功能,而不仅仅是添加/删除方法)您提供的)。

You would need to modify your generated "Impl" class to look like this:

/**
 * <!-- 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);
}

Note that I've done the following:

  1. Changed the generated getEntityTypes method's name and visibility to getEntityTypesGen and private, respectively. EMF won't mess with the visibility when it regenerates this method. Also, EMF will continue to generate this "Gen" suffixed method even though we now have a non-generated getEntityTypes method.
  2. Added a public, non-generated getEntityTypes method that wraps the default implementation's result in an unmodifiable EList.
  3. Implemented (and changed to non-generated) the add/removeEntityType methods by delegating to the generated getEntityTypesGen method (whose result is still modifiable).

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文