如何使用 Eclipse EMF 创建通用列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11543272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:37:20  来源:igfitidea点击:

How to create a generic list with Eclipse EMF?

eclipseeclipse-emf

提问by Yves_T

I want to create a class with Eclipse EMF that contains a Listwith Stringobjects. I see that Ecore has an EListbut I can't change the generic type of the list.

我想用 Eclipse EMF 创建一个包含ListwithString对象的类。我看到 Ecore 有一个EList但我无法更改列表的通用类型。

Any idea how to do this?

知道如何做到这一点吗?

回答by Max Hohenegger

If you want to generate code that gives you an EList<String>, then add a new EAttributeto an EClass, give it the ETypeEString, and set its "Upper Bound" property to '-1'.

如果要生成提供 的代码EList<String>,则向 中添加新EAttributeEClassETypeEString为其提供,并将其“上限”属性设置为“-1”。

If you want to create such a list programmatically, you could use the BasicEListfor example (org.eclipse.emf.common.util.BasicEList<E>):

如果要以编程方式创建这样的列表,可以使用BasicEList例如 ( org.eclipse.emf.common.util.BasicEList<E>):

EList<String> stringList = new BasicEList<String>();

If you want to see your other options, open the type hierarchy on: org.eclipse.emf.common.util.AbstractEList<E>

如果您想查看其他选项,请在以下位置打开类型层次结构: org.eclipse.emf.common.util.AbstractEList<E>

回答by CuongHuyTo

Not sure if your question was answered, and what you actually want to do.

不确定您的问题是否得到解答,以及您真正想要做什么。

If you want to generate Java code from an .ecore file, then I provide here an example using the Eclipse Juno's Sample Ecore Model Editor of EMF (right click on the .ecore file).

如果您想从 .ecore 文件生成 Java 代码,那么我在此处提供了一个使用 EMF 的 Eclipse Juno 示例 Ecore 模型编辑器的示例(右键单击 .ecore 文件)。

Maybe it's not directly what you want, but this might be helpful for someone else.

也许这不是您想要的,但这可能对其他人有帮助。

Suppose you want a method like this in your generated Java class MyClass:

假设您想要在生成的 Java 类 MyClass 中使用这样的方法:

<T extends String> EList<T> getListOfType(Class<T> T)

In your Sample Ecore Model Editor you want to achieve How your method looks in the Ecore Editorby

在您的示例 Ecore 模型编辑器中,您希望通过以下方式实现您的方法在 Ecore 编辑器中外观

  • add to MyClass a "New Child" of EOperation, name it getListOfType
  • add to getListOfType a "New Child" of ETypeParameter, name it T
  • add to T a "New Child" of EGeneric Bound Type, you would see a "T extends ?" instead of "T"
  • click the arrow to "T extends ?", click on "?", in "Property" window choose within the drop down menu of EClassifier an EString, now you would see "T extends EString"

  • add to getListOfType a "New Child" of EGeneric Return Type

  • click on the newly create "?" of return type, choose within a drop down menu of EClassifier an EEList
  • open the arrow of EEList, in the Property window choose within a drop down menu of EType Parameter a "T extends EString"

  • add to getListOfType a "New Child" of "EParameter"

  • in the property window of the newly created parameter "null", choose Name as "clazz", EType as "EJavaClass"
  • in the property window of the new "?" (two level below the node "clazz: EJavaClass"), choose EType Parameter as "T extends EString", now "clazz: EJavaClass" becomes "clazz: EJavaClass"
  • 将 EOperation 的“新子级”添加到 MyClass 中,将其命名为 getListOfType
  • 向 getListOfType 添加 ETypeParameter 的“新子项”,将其命名为 T
  • 将 EGeneric 绑定类型的“新子项”添加到 T,您会看到“T 扩展?” 而不是“T”
  • 点击箭头“T extends ?”,点击“?”,在“Property”窗口的EClassifier下拉菜单中选择一个EString,现在你会看到“T extends EString”

  • 向 getListOfType 添加 EGeneric 返回类型的“新子项”

  • 单击新创建的“?” 返回类型,在 EClassifier 的下拉菜单中选择一个 EEList
  • 打开EEList的箭头,在属性窗口的EType参数下拉菜单中选择“T extends EString”

  • 将“EParameter”的“新子项”添加到 getListOfType

  • 在新创建的参数“null”的属性窗口中,选择Name为“clazz”,EType为“EJavaClass”
  • 在新的“?”的属性窗口中 (节点“clazz:EJavaClass”下两级),选择EType参数为“T extends EString”,现在“clazz:EJavaClass”变成“clazz:EJavaClass”

Now youre .ecore file is ready to be used to generate a java class.

现在您的 .ecore 文件已准备好用于生成 Java 类。

How your method looks in the Ecore Editor

您的方法在 Ecore 编辑器中的外观