Skip to main content



      Home
Home » Modeling » EMF » get all contained objects by EReference name
get all contained objects by EReference name [message #693816] Thu, 07 July 2011 05:00 Go to next message
Eclipse UserFriend
Hello,
let's assume we have an Ecore model with a class ERoot. ERoot has two containment references "children1" of class EChild1 and "children2" of class EChild2.

With the following code I reconstruct the two names "children1" and "children2" for the references of the root object (root is an ERootImpl):
EList<Ereference> contains=root.eClass().getEAllContainments();
for (EReference ref: contains){
  System.err.println(ref.getName());
}

But how can I get the EObjects referenced through "children{1,2}" inside the for-loop? Of course, I could use Java reflection, create the method name "getChildren1" from the name "children1" and invoke the method by this name string on the object root. But I would like to avoid using Java reflection. Instead of the methods root.get<ReferenceName>() I would need something like root.getObjectsByReferenceName("children1").

Does EMF offer a method to find all EObjects referenced by a certain reference name or by a certain EReference? Can you point me to some article which answers questions like this?

Best,
Mirko
Re: get all contained objects by EReference name [message #694026 is a reply to message #693816] Thu, 07 July 2011 12:33 Go to previous message
Eclipse UserFriend
Mirko,

Comments below.

On 07/07/2011 2:00 AM, forums-noreply@eclipse.org wrote:
> Hello,
> let's assume we have an Ecore model with a class ERoot. ERoot has two
> containment references "children1" of class EChild1 and "children2" of
> class EChild2.
>
> With the following code I reconstruct the two names "children1" and
> "children2" for the references of the root object (root is an ERootImpl):
>
> EList<Ereference> contains=root.eClass().getEAllContainments();
> for (EReference ref: contains){
> System.err.println(ref.getName());
> }
> But how can I get the EObjects referenced through "children{1,2}"
> inside the for-loop?
root.eGet(ref).
> Of course, I could use Java reflection, create the method name
> "getChildren1" from the name "children1" and invoke the method by this
> name string on the object root. But I would like to avoid using Java
> reflection. Instead of the methods root.get<ReferenceName>() I would
> need something like root.getObjectsByReferenceName("children1").
Given you know the name of the feature, do you really not know it's an ERoot
>
> Does EMF offer a method to find all EObjects referenced by a certain
> reference name or by a certain EReference? Can you point me to some
> article which answers questions like this?
Look at EObject.eGet. You might also be interested in the
EcoreUtil.CrossReferencers.
>
> Best,
> Mirko
Previous Topic:[Validation] Standalone validation
Next Topic:Overriding persistence behaviour of an EMF resource
Goto Forum:
  


Current Time: Sun Jul 20 12:06:02 EDT 2025

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

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

Back to the top