Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a generic list with Eclipse EMF?

I want to create a class with Eclipse EMF that contains a List with String objects. I see that Ecore has an EList but I can't change the generic type of the list.

Any idea how to do this?

like image 741
Yves_T Avatar asked Jul 18 '12 14:07

Yves_T


2 Answers

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

If you want to create such a list programmatically, you could use the BasicEList for example (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>

like image 66
Max Hohenegger Avatar answered Sep 20 '22 11:09

Max Hohenegger


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).

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:

<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 Editor by

  • 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"

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

How your method looks in the Ecore Editor

like image 24
CuongHuyTo Avatar answered Sep 21 '22 11:09

CuongHuyTo