【问题标题】:EMF Eclipse: enumeration with custom fields (properties)EMF Eclipse:使用自定义字段(属性)进行枚举
【发布时间】:2013-10-25 04:48:27
【问题描述】:

好的,所以在 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

【问题讨论】:

    标签: java eclipse enums emf eclipse-emf-ecore


    【解决方案1】:

    有人吗?我知道我可以手动更改生成的模型代码,但如果我可能会更改模型的某些内容,我会丢失这些编辑,这显然不是我想要的。

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

    有关更多信息,您可以搜索 Budinsky 和公司编写的 Eclipse 建模框架一书。我引用这本书的内容(第 25 页):

    您需要编辑生成的类以添加方法和实例变量。您始终可以根据需要从模型中重新生成,并且您的添加将在重新生成期间保留。 [...] 任何没有 @generated 标记的方法(即您手动添加的任何内容)都将在重新生成期间单独保留。如果您的类中已经有一个方法与生成的方法冲突,那么您的版本将优先,并且生成的方法将被丢弃。

    【讨论】:

    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2017-04-02
    • 2016-12-11
    相关资源
    最近更新 更多