EMF Eclipse:带有自定义字段(属性)的枚举

5

好的,在Java中这是可能的:

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*/

这就是所谓的扩展枚举。我知道这很有效-我以前尝试并经常使用它。我知道可能会有讨论,是否应该在枚举中这样做-我认为是的,因为您仍然拥有定义的常量,但它们只包含一些更多的信息(这仍然是某种形式的常量)。 (此外:我看了一下这个链接 Custom fields on java enum not getting serialized,我认为他们也遵循我的思路来生成枚举上的自定义属性)。
现在,我应该如何从Eclipse EMF模型生成这样的东西?我甚至不知道在.ecore模型编辑器中添加额外属性的位置...我尝试将额外属性作为注释添加到ExtendedMetaData中,其中包含所有自定义属性的键。但是,在生成不改变文件的.genmodel文件时(我知道,因为我将其与SVN中较早的已检入版本进行比较,而SVN告诉我没有更改),当然还会使生成的模型代码不发生变化。
有人能帮忙吗?我知道我可以手动更改生成的模型代码,但如果我更改了模型,我会失去那些编辑,这显然不是我想要的。

谢谢!


更新:为了更加清晰,这是我在模型编辑器中.ecore的样子:
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个回答

1
有人可以帮忙吗?我知道我可以手动更改生成的模型代码,但是如果我对模型进行了更改,我会失去那些编辑,这显然不是我想要的。

实际上,您可以像以往一样添加扩展枚举。 当 genmodel 从您的模型生成代码时,它会添加一个标签@generate ,以了解哪些代码片段是由它创建的。 如果您添加一段代码,则它将没有此标记。 然后,如果您需要更新模型,因此需要更新生成的代码,EMF仅修改具有@generated标记的代码片段。 这样它将尊重您的代码插入,并且您不会失去您所做的内容。

如需更多信息,您可以在Budinsky等人撰写的Eclipse建模框架书籍中搜索。 我引用书中的内容(第25页):

你需要编辑生成的类添加方法和实例变量。在需要时,你可以根据模型重新生成,并且你的添加将在重新生成期间被保留。任何没有这个 @generated 标签的方法(也就是说,任何手动添加的内容)在重新生成期间都不会改变。如果你已经在一个类中有一个与生成的方法冲突的方法,那么你的版本将优先并且生成的方法将被丢弃。

1
这篇帖子也可能会很有帮助 http://stackoverflow.com/questions/12177465/enum-data-type-in-emf。 - lmove

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接