How to add child nodes using the reflective API of EMF? [message #630716] |
Mon, 04 October 2010 12:41  |
Eclipse User |
|
|
|
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 #631428 is a reply to message #631304] |
Thu, 07 October 2010 10:16  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.03584 seconds