Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an EAttribute whose data type is not an EMF class?

Using EMF, I'd like to define an attribute whose data type (EType) is external to my EMF model. That is, the type I want is a normal hand-coded Java class that is not part of my EMF model. Using AnySimpleType is a last resort. I want more type-safety (and less type casting) than using a generic Object provides.

Eclipse's Properties editor restricts EType to a predefined set of values. I can neither enter my own class name nor find a way to add the class I want to the list of options.

like image 574
Michael Carman Avatar asked Sep 18 '13 14:09

Michael Carman


3 Answers

You can add a data type to eCore models by manually adding an eClassifiers entry to the *.ecore file. (I was unable to find a mechanism to do this through the Eclipse UI.)

For example:

<eClassifiers xsi:type="ecore:EDataType"
 name="MyClass" instanceClassName="my.package.MyClass"/>

This will make the class available as an option for the EType in the Properties editor for EAttributes. It does not appear to make it available for EReferences.

like image 197
Michael Carman Avatar answered Nov 28 '22 08:11

Michael Carman


I am using Eclipse Kepler with EMF 2.9.1.

Adding to Michael's answer, in the Ecore Model Editor you can just right click and choose

New Child -> EData Type

and then fill in your values in the Properties view (Name, Instance Type Name). So you don't have to do add it manually anymore.

like image 37
Lukas Z. Avatar answered Nov 28 '22 08:11

Lukas Z.


Assuming you're using Xcore as your modelling language, then use

import java.util.Date
...
type Date wraps Date

Now Date can be used as a data type for attributes in line with String and int.

You do the same with Ecore, except that you don't have to explicitly import the type first - e.g.

<eClassifiers xsi:type="ecore:EDataType" name="Date"
   instanceClassName="java.util.Date" serializable="false"/>
like image 40
Tonny Madsen Avatar answered Nov 28 '22 09:11

Tonny Madsen