Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Copying objects with eOpposite-references
Copying objects with eOpposite-references [message #495276] Wed, 04 November 2009 13:19 Go to next message
Eclipse UserFriend
Originally posted by: c.krause.cwi.nl

Hi,

since I haven't seen anything about it here in the newsgroup, I'm
posting about a potential issue that I found regarding copying and
comparison of eObjects. Essentially, it's the fact that

EcoreUtil.equals(o, EcoreUtil.copy(o))

does not necessarily always returns true, as one might expect. The issue
appears with features that have an eOpposite. For instance something
like this:

MyClass1 o1 = new MyClass1Impl();
MyClass2 o2 = new MyClass2Impl();

o1.setMyClass2(o2); // eOpposite reference

MyClass1 copy = (MyClass1) EcoreUtil.copy(o1);
System.out.println(EcoreUtil.equals(o1, copy)); // says "false"


I don't know whether this belongs to the general folk wisdom so I posted
it here for people that are not aware of this.

Ciao,
Christian
Re: Copying objects with eOpposite-references [message #495354 is a reply to message #495276] Wed, 04 November 2009 15:45 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Christian,

Comments below.

Christian Krause wrote:
> Hi,
>
> since I haven't seen anything about it here in the newsgroup, I'm
> posting about a potential issue that I found regarding copying and
> comparison of eObjects. Essentially, it's the fact that
>
> EcoreUtil.equals(o, EcoreUtil.copy(o))
>
> does not necessarily always returns true, as one might expect.
Yes, that' possible with bidirectional references.
> The issue appears with features that have an eOpposite. For instance
> something like this:
>
> MyClass1 o1 = new MyClass1Impl();
> MyClass2 o2 = new MyClass2Impl();
>
> o1.setMyClass2(o2); // eOpposite reference
>
> MyClass1 copy = (MyClass1) EcoreUtil.copy(o1);
> System.out.println(EcoreUtil.equals(o1, copy)); // says "false"
>
>
> I don't know whether this belongs to the general folk wisdom so I
> posted it here for people that are not aware of this.
The reason is that copying should never modify the original objects so a
bidirectional reference should never refer to an original object because
that original would point at a copy and therefore would be modified.
>
> Ciao,
> Christian


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Loss of base type validation when restriction specified
Next Topic:[CDO] Referencing between models
Goto Forum:
  


Current Time: Tue Apr 16 12:13:06 GMT 2024

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

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

Back to the top