How to identify a map in Xcore

According to the EMF FAQ , you can create a map in EMF:

EMap is basically a list of instances of java.util.Map $ Entry. Therefore, to create a Map, you must first model your entry on the map, follow these steps:

  • Create an EClass named [Type1] To [Type2] Map, where [Type1] represents the key type and [Type2] represents the value type.
  • Set the instance class property of the newly created EClass for java.util.Map $ Entry.
  • Create an EAttribute or EReference named "key" and set it to EDataType or EClass.
  • Create an EAttribute or EReference named "value" and set it to EDataType or EClass.

Now, when you create an EReference somewhere that uses this class map entry as its EClass, the EMF code generator will detect this special one and generate a correctly typed EMAP receiver / setter instead of you a normal elf receiver / setter.

Can I use this with Xcore models? I'm not sure if step # 2 is complete in Xcore or supports cards in general.

+5
source share
1 answer

It works for me.

DataPoints.xcore :

 ... class KeyValuePair wraps java.util.Map$Entry { String key String value } class KeyValueList { contains KeyValuePair[] entries } 

The above result is in the KeyValueListImpl class with the getEntries method, which looks like this:

 public EMap<String, String> getEntries() { if (entries == null) { entries = new EcoreEMap<String,String>(DataPointsPackage.Literals.KEY_VALUE_PAIR, KeyValuePairImpl.class, this, DataPointsPackage.KEY_VALUE_LIST__ENTRIES); } return entries; } 
+4
source

Source: https://habr.com/ru/post/1262053/


All Articles