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

tes*_*ser 5 java eclipse enums eclipse-emf-ecore emf

好的,所以在 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*/
Run Code Online (Sandbox Code Playgroud)

这就是所谓的扩展枚举。我知道它有效 - 我之前尝试过并使用过很多次。我知道如果这是你应该用枚举做的事情,可能会有讨论 - 我认为是的,因为你仍然有你定义的常量,但它们只包含更多信息(这仍然是常量)。(另外:我看了这个,Java 枚举上的自定义字段没有被序列化,我认为它们也遵循我对如何在枚举上生成自定义属性的想法)。

现在,我到底应该如何从 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
Run Code Online (Sandbox Code Playgroud)

小智 1

任何人?我知道我可以手动更改生成的模型代码,但如果我可能对模型进行某些更改,我就会丢失这些编辑,这显然不是我想要的。

事实上,您可以像往常一样添加扩展枚举。当您的 genmodel 从模型生成代码时,它会添加一个标签@generate来了解它创建了哪些代码片段。如果你添加一段代码,它就不会有这个标志。然后,如果您需要更新模型以及生成的代码,EMF 只需修改具有该标签的代码片段@generated。这样,它将尊重您的代码插入,并且您不会丢失您所做的事情。

想要了解更多信息,可以搜索Budinsky等人编写的《Eclipse Modeling Framework》一书。我引用书上的话(第25页):

您需要编辑生成的类以添加方法和实例变量。您始终可以根据需要从模型中重新生成,并且您的添加将在重新生成过程中保留。[...] 任何没有此@generated标签的方法(即您手动添加的任何内容)都将在再生过程中被保留。如果类中已存在与生成的方法冲突的方法,则您的版本将优先,而生成的版本将被丢弃。