Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Setting several primitive values for structural feature
Setting several primitive values for structural feature [message #1742938] Thu, 08 September 2016 16:50 Go to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Hi,

Anyone that can help me with this?

I have a list of java.lang.Integer that I want to set as values for a structural feature using reflection. The problem is that the oSet Method in the EObject class only accepts an EList, i.e.

object.eSet( structuraFeature, "integers" );

I hope this makes sense.

Re: Setting several primitive values for structural feature [message #1742955 is a reply to message #1742938] Thu, 08 September 2016 20:46 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Is it a multi-valued feature? In that case you should use eGet(feature) to fetch the list, and then modify the list as you see fit.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Setting several primitive values for structural feature [message #1742957 is a reply to message #1742938] Thu, 08 September 2016 21:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Is it a multi-valued feature? In that case you should use eGet(feature) to fetch the list, and then modify the list as you see fit.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Setting several primitive values for structural feature [message #1743010 is a reply to message #1742957] Fri, 09 September 2016 13:01 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Ah, thanks a bunch!
Re: Setting several primitive values for structural feature [message #1743018 is a reply to message #1742957] Fri, 09 September 2016 13:49 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Doesn't seem to work? The types are correct.

EList<EObject> list = (EList<EObject>)ooo.eGet( structuralFeature );

SimpleAnyType wrapper = XMLTypeFactory.eINSTANCE.createSimpleAnyType();
wrapper.setInstanceType( dataType );
wrapper.setValue( new Integer( 1 ) );
list.add( wrapper );

Exception in thread "main" java.lang.ArrayStoreException: org.eclipse.emf.ecore.xml.type.impl.SimpleAnyTypeImpl
at org.eclipse.emf.common.util.BasicEList.assign(BasicEList.java:124)
at org.eclipse.emf.common.util.BasicEList.addUnique(BasicEList.java:424)
Re: Setting several primitive values for structural feature [message #1743044 is a reply to message #1743018] Fri, 09 September 2016 20:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Peter,

I can't guess what type of list this is. Of course the (EList<EObject>) cast is unchecked as the compiler warns you, so if it's not an EObject list you'll get ArrayStoreExcetions when you try to add the wrong type of object to it. You originally asserted it was a list of Integer, so I'm not sure why you aren't casting to EList<Integer> and adding the Integers to that...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Setting several primitive values for structural feature [message #1743459 is a reply to message #1743044] Thu, 15 September 2016 14:58 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Thanks for your reply!

Well, I guess I'm confused regarding the wrapper. What I'd like to do is to add a
value to a multi-valued integer list. So, from the code I have the structural feature that has a multi-valued Integer type. So this compiles and executes:

EList<EInteger> list = (EList<EInteger>)o.eGet( structuralFeature );
list.add( 1 );
o.eSet( sf, list );

However, when I inspect the model in the graphical editor, the list is still empty?
Therefore I thought I'd had to use a wrapper? Not sure how.

Re: Setting several primitive values for structural feature [message #1743464 is a reply to message #1743459] Thu, 15 September 2016 16:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Of course if you used the debugger (he's your best friend, don't neglect him) you'd see that the o.eSet is not needed and in fact is bad. The generated eSet is like this:

        getEAnnotations().clear();
        getEAnnotations().addAll((Collection<? extends EAnnotation>)newValue);

So you're adding to the list, clearing the list, and then adding all the things in the list (which is now empty), to the list.

So you can use eSet on a multi-valued feature to replace the contents of the list with new contents, but you don't need to do it that way, you can just get the list, add to it, and you're done.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Setting several primitive values for structural feature [message #1743605 is a reply to message #1743464] Fri, 16 September 2016 20:38 Go to previous message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Ah... Thanks for your help!
Previous Topic:EOpposite
Next Topic:Forbid to set both Attributes
Goto Forum:
  


Current Time: Thu Apr 18 16:16:54 GMT 2024

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

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

Back to the top