Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Texo] Autogenerated addTo... fails silently
[Texo] Autogenerated addTo... fails silently [message #1091575] Wed, 21 August 2013 17:33 Go to next message
Mike Cooper is currently offline Mike CooperFriend
Messages: 46
Registered: February 2011
Member
Martin,

The auto-generated addTo...() method for List values will fail to add an item
silently if it already exists:

	public void addToPasswords(Password passwordsValue) {
		if (!passwords.contains(passwordsValue)) {
			passwords.add(passwordsValue);
		}
	}


This can cause unexpected results for the caller who expects the value always to be added. The caller typically expects the given value to be added and then be present in the last item in the list. If the item is present but not the last item, these
expectation will fail.

It would be best to either throw an exception when the item is present and not added or to have the method return true when the item is added or false if its not added. The later would be more compatible with existing users of this method.

- mike
Re: [Texo] Autogenerated addTo... fails silently [message #1091730 is a reply to message #1091575] Wed, 21 August 2013 22:42 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hi Mike,
This is controlled by the unique attribute on the ereference/eattribute. Can you try setting it to false? Then it is
allowed to add a duplicate value to the collection.

I am a abit reluctant to add the boolean return as it means that current code is not backward compatibility. You can
always enter a bugziilla for it, will think about it!

gr. Martin

On 08/21/2013 07:33 PM, Mike Cooper wrote:
> Martin,
>
> The auto-generated addTo...() method for List values will fail to add an item
> silently if it already exists:
>
>
> public void addToPasswords(Password passwordsValue) {
> if (!passwords.contains(passwordsValue)) {
> passwords.add(passwordsValue);
> }
> }
>
>
> This can cause unexpected results for the caller who expects the value always to be added. The caller typically expects
> the given value to be added and then be present in the last item in the list. If the item is present but not the last
> item, these
> expectation will fail.
>
> It would be best to either throw an exception when the item is present and not added or to have the method return true
> when the item is added or false if its not added. The later would be more compatible with existing users of this method.
>
> - mike
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Texo] Autogenerated addTo... fails silently [message #1092334 is a reply to message #1091730] Thu, 22 August 2013 16:32 Go to previous messageGo to next message
Mike Cooper is currently offline Mike CooperFriend
Messages: 46
Registered: February 2011
Member
Martin,

A boolean return seems to mean to be completely backwards compatible since its void now. You should also return the boolean result from the Collection.add method. So basically something like

public boolean addToPasswords(Password item)
{
   if (!passwords.contains(item)) {
      return passwords.add(item);
   } else {
      return false;
   }
 }


I think this should be the default rather than something you manually have to enable in the eattributes. Since this method was previously void developers aren't checking the return value at all now.

- mike
Re: [Texo] Autogenerated addTo... fails silently [message #1092391 is a reply to message #1092334] Thu, 22 August 2013 18:30 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hi Mike,
Yes I thought of this, but there could be people that have manually changed this method. On the other hand they will get
compile errors when regenerating, these are easy to solve.

Can you enter a bugzilla for this change?

gr. Martin

On 08/22/2013 06:32 PM, Mike Cooper wrote:
> Martin,
>
> A boolean return seems to mean to be completely backwards compatible since its void now. You should also return the
> boolean result from the Collection.add method. So basically something like
>
>
> public boolean addToPasswords(Password item)
> {
> if (!passwords.contains(item)) {
> return passwords.add(item);
> } else {
> return false;
> }
> }
>
>
> I think this should be the default rather than something you manually have to enable in the eattributes. Since this
> method was previously void developers aren't checking the return value at all now.
> - mike
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:[Teneo] Example download
Next Topic:How can i have the string array attribute on Ecore?
Goto Forum:
  


Current Time: Thu Dec 18 13:44:35 GMT 2014

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

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