Search code examples
emfacceleoecore

How to model/generate Java 8 Optional attributes with Eclipse EMF


I want to model a class with a field of type Optional<String> but can't find a EOptional EType or equivalents options to maker the attribute optional.


Solution

  • There is no EOptional in EMF. As you are probably writing your own Acceleo generator (from what I understand), you can deal with EAttribute/EOperation cardinality. If the lowerBound is 0, it could means that the field is optional and generate the code as such.

    Another solution would be to create your own Optional EDataType with the right instanceClassName like EMF 'external types' (see here for some details)