Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » reflection
reflection [message #427962] Sat, 07 March 2009 10:15 Go to next message
Ali Koudri is currently offline Ali KoudriFriend
Messages: 118
Registered: July 2009
Senior Member
Hi All,

How can I use reflection in emf in order to add an item into a
collection ? I tried the piece of code below but if fails.

//retrieve the feature I want to modify
EStructuralFeature feature = getFeature(featureName);
//retrieve the metaclass I want to instanciate
EClass metaClass = (EClass) modal.getEClassifier(metaClassName);
//instanciate the metaclass
Ebject o = myFactory.create(metaClass);
//set the feature
if (feature.getUpperBound == 1)
element.eSet(feature, o);
else
((EList<EObject>)feature).add(o);

I can't see what's wrong. Any Idea ?
thx for help.
Re: reflection [message #427963 is a reply to message #427962] Sat, 07 March 2009 11:01 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
You must reflectively get the list that represents your reference
feature out of your object, then use that list normally:

EObject object = ...
EStructuralFeature feature =
object.eClass().getEStructuralFeature(featureName);
EList list = (EList)object.eGet(feature);
list.add(...);

Cheers
/Eike

----
http://thegordian.blogspot.com



exquisitus schrieb:
> Hi All,
>
> How can I use reflection in emf in order to add an item into a
> collection ? I tried the piece of code below but if fails.
>
> //retrieve the feature I want to modify
> EStructuralFeature feature = getFeature(featureName);
> //retrieve the metaclass I want to instanciate
> EClass metaClass = (EClass) modal.getEClassifier(metaClassName);
> //instanciate the metaclass
> Ebject o = myFactory.create(metaClass);
> //set the feature
> if (feature.getUpperBound == 1)
> element.eSet(feature, o);
> else
> ((EList<EObject>)feature).add(o);
>
> I can't see what's wrong. Any Idea ?
> thx for help.


Re: reflection [message #427964 is a reply to message #427963] Sat, 07 March 2009 12:33 Go to previous message
Ali Koudri is currently offline Ali KoudriFriend
Messages: 118
Registered: July 2009
Senior Member
Got it !
thx a lot.

Eike Stepper a écrit :
> You must reflectively get the list that represents your reference
> feature out of your object, then use that list normally:
>
> EObject object = ...
> EStructuralFeature feature =
> object.eClass().getEStructuralFeature(featureName);
> EList list = (EList)object.eGet(feature);
> list.add(...);
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
>
>
>
> exquisitus schrieb:
>> Hi All,
>>
>> How can I use reflection in emf in order to add an item into a
>> collection ? I tried the piece of code below but if fails.
>>
>> //retrieve the feature I want to modify
>> EStructuralFeature feature = getFeature(featureName);
>> //retrieve the metaclass I want to instanciate
>> EClass metaClass = (EClass) modal.getEClassifier(metaClassName);
>> //instanciate the metaclass
>> Ebject o = myFactory.create(metaClass);
>> //set the feature
>> if (feature.getUpperBound == 1)
>> element.eSet(feature, o);
>> else
>> ((EList<EObject>)feature).add(o);
>>
>> I can't see what's wrong. Any Idea ?
>> thx for help.
Previous Topic:The carriage-return (#xD) character saved as "&#xD;\r"
Next Topic:Problem persisting ChangeDescription (Validation problem)
Goto Forum:
  


Current Time: Tue Apr 23 06:41:13 GMT 2024

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

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

Back to the top