gpt4 book ai didi

java - EMF eclipse : enumeration with custom fields (properties)

转载 作者:搜寻专家 更新时间:2023-11-01 03:53:03 24 4
gpt4 key购买 nike

好的,所以在 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 中较早 checkin 的版本进行比较,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

最佳答案

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.

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

更多信息可以搜索Budinsky等人写的Eclipse Modeling Framework一书。我引用书中所说的内容(第 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.

关于java - EMF eclipse : enumeration with custom fields (properties),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19414137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com