EMF Eclipse: enumeration with custom fields (properties)

流过昼夜 提交于 2019-12-23 12:07:27

问题


Ok, so in Java this is possible:

import org.eclipse.emf.common.util.Enumerator;

public enum MyEnum implements Enumerator {
   LITERAL1(0, "Name", "Literal", "custom1", "custom2", "custom3"),
   LITERAL2(0, "Name", "Literal", "custom1", "custom2", "custom3"),
   LITERAL3(0, "Name", "Literal", "custom1", "custom2", "custom3"),
   LITERAL4(0, "Name", "Literal", "custom1", "custom2", "custom3");

   public static final int LITERAL1_VALUE = 0;
   public static final int LITERAL2_VALUE = 1;
   public static final int LITERAL3_VALUE = 2;
   public static final int LITERAL4_VALUE = 3;

   private static final MyEnum[] VALUES_ARRAY =
        new MyEnum[] {
           LITERAL1,
           LITERAL2,
           LITERAL3,
           LITERAL4,
   };

   public static final List<MyEnum> VALUES =
        Collections.unmodifiableList(Arrays.asList(VALUES_ARRAY));

   private final int value;
   private final String name;
   private final String literal;
   private final String custom1;
   private final String custom2;
   private final String custom3;
   private MyEnum(int value, String name, String literal, 
                 String custom1, String custom2, String custom3) {
        this.value = value;
        this.name = name;
        this.literal = literal;
        this.custom1 = custom1;
        this.custom2 = custom2;
        this.custom3 = custom3;
   }

    /*Getters for all of them*/

This is what's called an extended enum. I know it works - I tried and used it lots before. I know there could be discussion if this is what you should do with an enumeration - I think yes, as you still have your defined constants, but they just contain some more information (which is still sort of constant). (Also: I looked at this one, Custom fields on java enum not getting serialized, and I think they also follow my thinking in how to generate custom properties on enums).

Now, how on earth am I supposed to generate something like this from an Eclipse EMF model? I don't even know where to add extra properties to my enums in the .ecore model editor... I tried adding the extra properties as an annotation to ExtendedMetaData, which contains keys for all the custom properties. However when generating a .genmodel file that doesn't change the file (I know as I'm holding it against an earlier checked-in version in SVN, and SVN tells me nothing's changed). Ofcourse that also makes that there's no change in the generated model code.

Anyone? I know I can change generated model code by hand, but in the event I might change something to the model I'd lose those edits, that's obviously not what I'd want.

Thanks!


Update: Just to be all clear, this is how my .ecore looks like in the model editor:

MyEnum (EEnum)
    LITERAL1 (EEnum Literal)
        ExtendedMetaData (EAnnotation)
            custom1 -> custom1
            custom2 -> custom2
            custom3 -> custom3
    LITERAL2 (EEnum Literal)
        ExtendedMetaData (EAnnotation)
            custom1 -> custom1
            custom2 -> custom2
            custom3 -> custom3
    LITERAL3 (EEnum Literal)
        ExtendedMetaData (EAnnotation)
            custom1 -> custom1
            custom2 -> custom2
            custom3 -> custom3
    LITERAL4 (EEnum Literal)
        ExtendedMetaData (EAnnotation)
            custom1 -> custom1
            custom2 -> custom2
            custom3 -> custom3

回答1:


Anyone? I know I can change generated model code by hand, but in the event I might change something to the model I'd lose those edits, that's obviously not what I'd want.

In fact, you can add your extended enum the way you always do. When your genmodel generates code from your model it adds a tag @generate to know which pieces of code have been created by it. If you add a piece of code, it would not have this flag. Then, if you need to update your model and so your generated code, EMF just modifies the pieces of code that have the @generated tag. In this way it will respect your code insertion and you will not lose what you have done.

For more information, you can search on the Eclipse Modeling Framework book wrote by Budinsky and company. I quote what the book says (p. 25):

You are expected to edit the generated classes to add methods and instance variables. You can always regenerate from the model as needed and your addition will be preserved during the regeneration. [...] Any method that doesn't have this @generated tag (that is, anything you add by hand) will be left alone during regeneration. If you already have a method in a class that conflicts with a generated method, then your version will take precedence and the generated one will be discarded.



来源:https://stackoverflow.com/questions/19414137/emf-eclipse-enumeration-with-custom-fields-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!