Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » UML2 » How to add child nodes using the reflective API of EMF?(How to add child nodes using the reflective API of EMF?)
How to add child nodes using the reflective API of EMF? [message #630716] Mon, 04 October 2010 16:41 Go to next message
Adrian Aichner is currently offline Adrian AichnerFriend
Messages: 14
Registered: March 2010
Junior Member
Greetings!

I am able to add nodes to an EMF model generated from XSD:

EPackage ep = poClass.getEPackage();
EClass ctNew = (EClass) ep.getEClassifier("ChangeType");
EObject ctObject = ep.getEFactoryInstance().create(ctNew);
ctObject.eSet(ctObject.eClass().getEStructuralFeature("comment "), "...");

I am also able to add a node to a single-valued feature:
EClass cltNew = (EClass) ep.getEClassifier("ChangeLogType");
clt = ep.getEFactoryInstance().create(cltNew);
po.eSet(poClass.getEStructuralFeature("changeLog"), clt);

I can also obtain the list of change entries under the many-values change node:

EList<EObject> changes = clt.eClass().getEStructuralFeature("change").eContents();

Here is my problem:

changes.add(ctObject);

gives

Caused by: java.lang.UnsupportedOperationException
at org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImp l.add(EContentsEList.java:779)
at java.util.AbstractSequentialList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)

which I had planned to use as follows:

clt.eSet(clt.eClass().getEStructuralFeature("changeType"), changes);

Is there a way to add child nodes using the reflective API of EMF?

I need to avoid using specific types of the generated model in this section of code, since this code should be usable by multiple separate generated models.

Thanks in advance for any advice.

Adrian

Re: How to add child nodes using the reflective API of EMF? [message #630923 is a reply to message #630716] Tue, 05 October 2010 15:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Adrian,

Comments below.

Adrian wrote:
> Greetings!
>
> I am able to add nodes to an EMF model generated from XSD:
>
> EPackage ep = poClass.getEPackage();
> EClass ctNew = (EClass) ep.getEClassifier("ChangeType");
> EObject ctObject = ep.getEFactoryInstance().create(ctNew);
> ctObject.eSet(ctObject.eClass().getEStructuralFeature("comment "),
> "...");
>
> I am also able to add a node to a single-valued feature:
> EClass cltNew = (EClass) ep.getEClassifier("ChangeLogType");
> clt = ep.getEFactoryInstance().create(cltNew);
> po.eSet(poClass.getEStructuralFeature("changeLog"), clt);
>
> I can also obtain the list of change entries under the many-values
> change node:
>
> EList<EObject> changes =
> clt.eClass().getEStructuralFeature("change").eContents();
This list is a read only view of the containment of the changes
structure feature itself. That can't possibly be something you'd want
to modify.
>
> Here is my problem:
>
> changes.add(ctObject);
>
> gives
>
> Caused by: java.lang.UnsupportedOperationException
> at org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImp
> l.add(EContentsEList.java:779)
> at java.util.AbstractSequentialList.add(Unknown Source)
> at java.util.AbstractList.add(Unknown Source)
>
> which I had planned to use as follows:
>
> clt.eSet(clt.eClass().getEStructuralFeature("changeType"), changes);
Use eGet to get a list and then modify that list directly.
>
> Is there a way to add child nodes using the reflective API of EMF?
>
> I need to avoid using specific types of the generated model in this
> section of code, since this code should be usable by multiple separate
> generated models.
>
> Thanks in advance for any advice.
>
> Adrian
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to add child nodes using the reflective API of EMF? [message #631176 is a reply to message #630923] Wed, 06 October 2010 13:22 Go to previous messageGo to next message
Adrian Aichner is currently offline Adrian AichnerFriend
Messages: 14
Registered: March 2010
Junior Member
Thanks for your help, Ed!

Following code works now, but gives me the ClassCastException shown below.

Would you have one more hint how I avoid that exception?

Thanks!

Adrian

EStructuralFeature cltFeature = poClass.getEStructuralFeature("changeLog");

EList<Object> changes = (EList<Object>) clt.eGet(clt.eClass().getEStructuralFeature("change"), true);
changes.add(ctObject);
// FIXME causes ClassCast Exception, but adding a change actually works
clt.eSet(cltFeature, changes);


Caused by: java.lang.ClassCastException: org.eclipse.emf.ecore.util.FeatureMapUtil$FeatureEList cannot be cast to org.eclipse.emf.ecore.util.FeatureMap$Internal$Wrapper
at org.eclipse.emf.ecore.util.BasicFeatureMap.set(BasicFeatureM ap.java:2564)
at my.package.path.impl.ChangeLogTypeImpl.eSet(ChangeLogTypeImp l.java:136)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjec tImpl.java:1081)
Re: How to add child nodes using the reflective API of EMF? [message #631304 is a reply to message #631176] Wed, 06 October 2010 22:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Adrian,

Comments below.


Adrian wrote:
> Thanks for your help, Ed!
>
> Following code works now, but gives me the ClassCastException shown
> below.
>
> Would you have one more hint how I avoid that exception?
>
> Thanks!
>
> Adrian
>
> EStructuralFeature cltFeature =
> poClass.getEStructuralFeature("changeLog");
>
> EList<Object> changes = (EList<Object>)
> clt.eGet(clt.eClass().getEStructuralFeature("change"), true);
> changes.add(ctObject);
You're already directly editing the state of the object
> // FIXME causes ClassCast Exception, but adding a change actually works
> clt.eSet(cltFeature, changes);
It's of course hard to give advice without knowing more about the model...
>
>
> Caused by: java.lang.ClassCastException:
> org.eclipse.emf.ecore.util.FeatureMapUtil$FeatureEList cannot be cast
> to org.eclipse.emf.ecore.util.FeatureMap$Internal$Wrapper
> at org.eclipse.emf.ecore.util.BasicFeatureMap.set(BasicFeatureM
> ap.java:2564)
> at my.package.path.impl.ChangeLogTypeImpl.eSet(ChangeLogTypeImp
> l.java:136)
> at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eSet(BasicEObjec
> tImpl.java:1081)
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to add child nodes using the reflective API of EMF? [message #631428 is a reply to message #631304] Thu, 07 October 2010 14:16 Go to previous message
Adrian Aichner is currently offline Adrian AichnerFriend
Messages: 14
Registered: March 2010
Junior Member
Thanks for your second followup, Ed!

I got this figured out now.

Instead of

EList<Object> changes = (EList<Object>) clt.eGet(clt.eClass().getEStructuralFeature("change"), true);
changes.add(ctObject);
// FIXME causes ClassCast Exception, but adding a change actually works
clt.eSet(cltFeature, changes);


I only need

((EList<EObject>) clt.eGet(clt.eClass().getEStructuralFeature("change"), true)).add(ctObject);


to add the ctObject node as an additional child at the end of the "change" feature list.

This works perfectly now.

Previously I incorrectly tried to eSet the "changeLog" parent node feature to the list of changes.

Thanks again,
Adrian
Previous Topic:modeling n-ary association
Next Topic:HOW to register UML resources?
Goto Forum:
  


Current Time: Fri Mar 29 05:58:31 GMT 2024

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

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

Back to the top