简体   繁体   English

EMF Ecore 模型中的枚举是否可以实现接口?

[英]Is it possible to have an enumeration in a EMF Ecore model implement an interface?

Is it possible to have an enumeration in a EMF Ecore model implement an interface ?是否可以在 EMF Ecore 模型中进行枚举实现接口? It is possible in Java to have something like : public enum MyEnum implements MyInterface .在 Java 中可能有这样的东西: public enum MyEnum implements MyInterface I'd like to be able to generate something like this via EMF (btw, It seems by default all enums generated by EMF implement org.eclipse.emf.common.util.Enumerator ).我希望能够通过 EMF 生成这样的东西(顺便说一句,默认情况下,EMF 生成的所有枚举似乎都实现了org.eclipse.emf.common.util.Enumerator )。

I can't find a way to have my generated enumeration implement a particular interface.我找不到让我生成的枚举实现特定接口的方法。 I can't define inheritance relationships with an enumeration in the ecore diagram editor, nor in the ecore model editor.我无法在 ecore 图编辑器中定义与枚举的继承关系,也无法在 ecore 模型编辑器中定义继承关系。 I can add the implements bit by hand to the enumeration after generating the code, but then it gets overwritten everytime I generate the code again.我可以在生成代码后手动将implements添加到枚举中,但是每次我再次生成代码时它都会被覆盖。

Alternatively, is there a way to have the implements (and only it) not being overwritten by EMF's code generation ?或者,有没有办法让implements (只有它)不被 EMF 的代码生成覆盖?

I know I can modify the @generated tag in the class javadoc comment to @generatedNOT so the code generator knows it musn't overwrite the class, but it prevents the class from being updated when I modify the model.我知道我可以将类 javadoc 注释中的@generated标记修改为@generatedNOT因此代码生成器知道它不能覆盖该类,但是当我修改模型时它会阻止更新该类。

For now what I do is I manually add the implements each time I modify this enumeration in the model and keep the @generatedNOT tag the rest of the time.现在我所做的是每次修改模型中的枚举时手动添加implements ,并在其余时间保留@generatedNOT标记。 I feel this will get dangerous in a few months when I'll forget about it, or worse, when someone else tries to modify it, even if properly documented.我觉得这会在几个月后变得危险,当我忘记它时,或者更糟的是,当其他人试图修改它时,即使有适当的记录。

Somewhat related : EMF Eclipse: enumeration with custom fields (properties)有点相关: EMF Eclipse: enumeration with custom fields (properties)

An Enum is represented by an instance of EEnum . Enum 由EEnum的一个实例EEnum An interface is represented by an instance of EClass that has the value of Interface set to true .接口由EClass的实例表示,该实例的Interface值设置为true

The interfaces to an EClass are then available at getEAllSuperTypes() .然后可以在getEAllSuperTypes()处获得getEAllSuperTypes()的接口。

Note that EEnum and EClass are distinct subinterfaces of EModelElement and therefore do not share any members beyond those exposed in EModelElement .需要注意的是EEnumEClass是不同的子接口EModelElement ,因此不同意超出暴露任何成员EModelElement
This is because java is special in its treatment of Enums, implementing them as special classes.这是因为 java 在处理枚举方面是特殊的,将它们实现为特殊的类。 EMF is intended to allow for the more "common" understanding of Enums where they can not expose interfaces. EMF 旨在允许对 Enum 无法公开接口的情况有更“普遍”的理解。

As such it's not possible in EMF to have Enums implement an interface.因此,在 EMF 中不可能让 Enum 实现一个接口。
Your only option when adding an interface to an Enum is to do so manually, sidestepping all code generation, because EMF doesn't support that particular behaviour.向 Enum 添加接口时,您唯一的选择是手动执行此操作,避开所有代码生成,因为 EMF 不支持该特定行为。 An alternative you might want to consider is to have one or more static utility methods that provide that interface by way of "currying" transforming this:您可能要考虑的另一种方法是拥有一个或多个静态实用程序方法,通过“柯里化”转换此接口来提供该接口:

interface Foo {
    void bar();
    Baz baz(Quux quux);
}
enum Gen implements Foo { ... }

into:进入:

class GenFoo {
    static void bar(Foo foo);
    static Baz baz(Foo foo, Quux quux);
}

This idea is basically what C# has implemented as "Extension methods", but they have language support for that and Java doesn't :)这个想法基本上是 C# 作为“扩展方法”实现的,但他们有语言支持,而 Java 没有:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM