Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EClass.eSet method only takes collection?
EClass.eSet method only takes collection? [message #524664] Thu, 01 April 2010 18:11 Go to next message
aravind  is currently offline aravind Friend
Messages: 10
Registered: July 2009
Junior Member
I am dynamically instantiating the model. When I try to set an attribute value like below, I am getting a ClassCastExcepption. Can someone explain what I am doing wrong?

The "id" attribute is defined as EShort type in the EMF.

Segment segment = respFactory.createSegment();
EClass segmentMeta = segment.eClass();
EAttribute attr = (EAttribute) segmentMeta.getEStructuralFeature("id");
segmentMeta.eSet(attr, Short.valueOf("0"));


But when I change the last line as below, it works
segmentMeta.eSet(attr, new ArrayList(Short.valueOf("0")));
Re: EClass.eSet method only takes collection? [message #524672 is a reply to message #524664] Thu, 01 April 2010 13:40 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Aravind,

Your "id" attribute is multi-valued (upper bound more than 1). The
eSet() method replaces the *entire* value of a feature. For
multi-valued features, that requires a list.

Perhaps you actually wanted a single-valued "id" feature? In that case,
you need to fix your Ecore model.

HTH,

Christian


On 01/04/10 02:11 PM, aravind wrote:
> I am dynamically instantiating the model. When I try to set an attribute
> value like below, I am getting a ClassCastExcepption. Can someone
> explain what I am doing wrong?
>
> The "id" attribute is defined as EShort type in the EMF.
>
> Segment segment = respFactory.createSegment();
> EClass segmentMeta = segment.eClass();
> EAttribute attr = (EAttribute) segmentMeta.getEStructuralFeature("id");
> segmentMeta.eSet(attr, Short.valueOf("0"));
>
>
> But when I change the last line as below, it works
> segmentMeta.eSet(attr, new ArrayList(Short.valueOf("0")));
Re: EClass.eSet method only takes collection? [message #524675 is a reply to message #524672] Thu, 01 April 2010 18:36 Go to previous messageGo to next message
aravind  is currently offline aravind Friend
Messages: 10
Registered: July 2009
Junior Member
It is a bug in my code itself. Instead of calling the setter method on the instance object, I was calling the setter on the Eclass.

the below is the correct code
segment.eSet(respPkg.getSegment_Id(), Short.valueOf("0"));

[Updated on: Thu, 01 April 2010 19:00]

Report message to a moderator

Re: EClass.eSet method only takes collection? [message #524689 is a reply to message #524675] Thu, 01 April 2010 19:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Aravind,

Step through the code with the debugger, your best friend, to see what's
going wrong. It seems clear that the feature you're setting is
multi-valued based on the symptoms you've described.


aravind wrote:
> But that is not the case with my model. see the below generated
> interface for Segment
>
> /**
> * Returns the value of the '<em><b>Id</b></em>' attribute.
> * <!-- begin-user-doc -->
> * <p>
> * If the meaning of the '<em>Id</em>' attribute isn't clear,
> * there really should be more of a description here...
> * </p>
> * <!-- end-user-doc -->
> * @return the value of the '<em>Id</em>' attribute.
> * @see #setId(short)
> * @see
> com.equifax.ic.tdp.gen.response.response.ResponsePackage#get Segment_Id()
> * @model
> * @generated
> */
>
> short getId();
>
>
>
> /**
> * Sets the value of the '{@link
> com.equifax.ic.tdp.gen.response.response.Segment#getId <em>Id</em>}'
> attribute.
> * <!-- begin-user-doc -->
> * <!-- end-user-doc -->
> * @param value the new value of the '<em>Id</em>' attribute.
> * @see #getId()
> * @generated
> */
>
> void setId(short value);


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EClass.eSet method only takes collection? [message #524738 is a reply to message #524664] Fri, 02 April 2010 07:26 Go to previous message
German Vega is currently offline German VegaFriend
Messages: 5
Registered: July 2009
Junior Member
Hello

> The "id" attribute is defined as EShort type in the EMF.
>
> Segment segment = respFactory.createSegment();
> EClass segmentMeta = segment.eClass();
> EAttribute attr = (EAttribute) segmentMeta.getEStructuralFeature("id");
> segmentMeta.eSet(attr, Short.valueOf("0"));

You are setting the attribute on the eClass and not the eObject, is this
is a typo, or the actual code?

shouldn't it be:

segment.eSet(attr, Short.valueOf("0"));
Previous Topic:Why is resourceSet.createResource(fileURI) always returning null ?
Next Topic:Dropping @Override from generated source
Goto Forum:
  


Current Time: Thu Mar 28 14:09:37 GMT 2024

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

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

Back to the top