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

1.5k views Asked by At

Is it possible to have an enumeration in a EMF Ecore model implement an interface ? It is possible in Java to have something like : 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).

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

Alternatively, is there a way to have the implements (and only it) not being overwritten by EMF's code generation ?

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.

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

1

There are 1 answers

0
Vogel612 On

An Enum is represented by an instance of EEnum. An interface is represented by an instance of EClass that has the value of Interface set to true.

The interfaces to an EClass are then available at getEAllSuperTypes().

Note that EEnum and EClass are distinct subinterfaces of EModelElement and therefore do not share any members beyond those exposed in EModelElement.
This is because java is special in its treatment of Enums, implementing them as special classes. EMF is intended to allow for the more "common" understanding of Enums where they can not expose interfaces.

As such it's not possible in EMF to have Enums implement an interface.
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. 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 :)