Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Serializing attribute defined ad EDataType
Serializing attribute defined ad EDataType [message #1805862] Thu, 25 April 2019 17:52 Go to next message
Jérôme Fouletier is currently offline Jérôme FouletierFriend
Messages: 39
Registered: September 2010
Location: France
Member
I have defined a EDataType using a class MyDataType which implements Serializable.
In my ecore file, I have set an EAttribute's type to that EDataType, it is serializable by default.
I am using the XMI resource factory to serialize.

How do I tell the code generator to generate serializing code that uses java serialization of MyDataType? Is it done by overriding the generated factory? Or are there generator configurations that would take care of that?

Thanks
Re: Serializing attribute defined ad EDataType [message #1805864 is a reply to message #1805862] Thu, 25 April 2019 18:23 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You have to implement

XXXFactoryImpl.createMyDataTypeFromString(EDataType eDataType, String aValue)
XXXFactoryImpl.convertMyDataTypeToString(EDataType eDataType, Object instanceValue)

Regards

Ed Willink
Re: Serializing attribute defined ad EDataType [message #1805866 is a reply to message #1805864] Fri, 26 April 2019 03:25 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Note as well that you can annotate the model with the code to be inserted in the generated factory's methods. E.g., the following
  <eClassifiers xsi:type="ecore:EDataType" name="URI" instanceClassName="org.eclipse.emf.common.util.URI">
    <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
      <details key="convert" value="return it == null ? null : it.toString();"/>
      <details key="create" value="return it == null ? null : URI.createURI(it);"/>
    </eAnnotations>
  </eClassifiers>
will generate the following:
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public URI createLocation(final String it) {
		return it == null ? null : URI.createURI(it);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public URI createLocationFromString(EDataType eDataType, String initialValue) {
		return createLocation(initialValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String convertLocation(final URI it) {
		return it == null ? null : it.toString();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String convertLocationToString(EDataType eDataType, Object instanceValue) {
		return convertLocation((URI) instanceValue);
	}
Such annotations are easy to create in recent versions of EMF because the context menu actions to create annotations will suggestion a "GenModel" annotation in the menu and the properties view shows exactly which properties you can set in that annotation based on the context of where it is contained.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Serializing attribute defined ad EDataType [message #1806644 is a reply to message #1805866] Mon, 13 May 2019 06:34 Go to previous messageGo to next message
Amber Buzacott is currently offline Amber BuzacottFriend
Messages: 1
Registered: April 2019
Junior Member
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 upsers

[Updated on: Tue, 14 May 2019 10:39]

Report message to a moderator

Re: Serializing attribute defined ad EDataType [message #1806678 is a reply to message #1806644] Mon, 13 May 2019 18:04 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
No, depending on what instance class you are wrapping, you may well need to write code that does the conversion between a string representation and the value. The reflective implementation will look for constructors and conversion methods on the class, but that may not suffice, and definitely won't the the most performance efficient implementation.

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[Eclipse 2019-03 EMF.Editor] : regression ?
Next Topic:IllegalValueException in web.xml with variable
Goto Forum:
  


Current Time: Tue Apr 23 09:18:24 GMT 2024

Powered by FUDForum. Page generated in 0.03124 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top