EMF Eclipse:具有自定义字段的枚举(属性) [英] EMF Eclipse: enumeration with custom fields (properties)

查看:156
本文介绍了EMF Eclipse:具有自定义字段的枚举(属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

public enum MyEnum实现枚举器{
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;
}

/ *所有人的收件人* /

这就是所谓的扩展枚举。我知道它的作品 - 我尝试了很久以前使用它。我知道可能会有一个讨论,如果这是你应该做的枚举 - 我认为是的,因为你仍然有你定义的常量,但它们只包含一些更多的信息(这仍然是一个常数)。 (另外:我看了一下, 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


解决方案


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


事实上,您可以按照您的方式添加扩展枚举。当genmodel从你的模型生成代码时,会添加一个标签 @generate 来知道它已经创建了哪些代码段。如果你添加一段代码,它将不会有这个标志。那么,如果你需要更新你的模型,所以你生成的代码,EMF只是修改的代码块有 @generated 标签。这样它会尊重你的代码插入,你不会失去你所做的一切。



有关更多信息,您可以搜索由Budinsky和公司编写的Eclipse Modeling Framework书籍。我引用了这本书所说的话(p。25):


你希望编辑生成的类来添加方法和实例变量。您可以随时根据需要从模型重新生成,并在再生过程中保留添加。 [...]没有这个 @generated 标签的任何方法(即手工添加的任何内容)将在重新生成期间被单独使用。如果您已经有一个方法在类中与生成的方法冲突,那么您的版本将优先,生成的方法将被丢弃。



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

解决方案

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.

这篇关于EMF Eclipse:具有自定义字段的枚举(属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆