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