Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to get the eReferences of a referenced class?
How to get the eReferences of a referenced class? [message #502860] Thu, 10 December 2009 01:24 Go to next message
No real name is currently offline No real nameFriend
Messages: 62
Registered: July 2009
Member
Hi,

I am currently playing around with eReferences.
From an existing eReference I try to recover all eReferences that the
class of the element has, which is referenced in the existing
eReference. Note that I do NOT have an instance of the referenced class,
only the eReference and the type of the element it is referencing.

Currently, my code looks like this:

try {
Class<?> clazz = paramMapping.get(eReference.getEType().getName());
EditPart editPart = (EditPart)clazz.newInstance();
eClass = ((View)editPart.getModel()).getElement().eClass();
}
catch (Exception e) {
e.printStackTrace();
}
if (eClass != null) {
for (EReference eRef : eClass.getEAllContainments()) {
if (referenceIsRequired(eRef, typeParam)) {
return true;
}
}
}

First, I retrieve the name of the type of the reference. Let's assume
it's name is 'Input'. Then I get the exact class name with all package
names by accessing the mapping 'paramMapping'. This gives me the class
'org.example.demo.InputEditPart.class'. After that I try to instantiate
the class, which would give me an edit part. After that I could just
retrieve the corresponding model element and its class and get the
references from it.

However, I keep getting an InstantiationException when trying to
instantiate with 'newInstance()'.

Does anyone know how to get this to work? Or is there even an easier way
to retrieve the eReferences of an element referenced by an eReference?
I know, my current approach is not very straight-forward.

Thanks in advance,
Matthias
Re: How to get the eReferences of a referenced class? [message #502951 is a reply to message #502860] Thu, 10 December 2009 12:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30620
Registered: July 2009
Senior Member
Matthias,

Comments below.

Matthias Schmeling wrote:
> Hi,
>
> I am currently playing around with eReferences.
> From an existing eReference I try to recover all eReferences that the
> class of the element has, which is referenced in the existing
> eReference. Note that I do NOT have an instance of the referenced
> class, only the eReference and the type of the element it is referencing.
>
> Currently, my code looks like this:
>
> try {
> Class<?> clazz = paramMapping.get(eReference.getEType().getName());
> EditPart editPart = (EditPart)clazz.newInstance();
> eClass = ((View)editPart.getModel()).getElement().eClass();
I have no idea what this mapping is doing, but likely
eReference.getEType() == eReference.getEReferenceType() == eClass
> }
> catch (Exception e) {
> e.printStackTrace();
> }
> if (eClass != null) {
> for (EReference eRef : eClass.getEAllContainments()) {
> if (referenceIsRequired(eRef, typeParam)) {
Not sure what this is doing.
> return true;
> }
> }
> }
>
> First, I retrieve the name of the type of the reference. Let's assume
> it's name is 'Input'. Then I get the exact class name with all package
> names by accessing the mapping 'paramMapping'. This gives me the class
> 'org.example.demo.InputEditPart.class'. After that I try to
> instantiate the class, which would give me an edit part. After that I
> could just retrieve the corresponding model element and its class and
> get the references from it.
>
> However, I keep getting an InstantiationException when trying to
> instantiate with 'newInstance()'.
Sounds more like a GMF question. Do you really need the EditPart. You
don't need it to determine the EClass that is the EReference's eType.
>
> Does anyone know how to get this to work? Or is there even an easier
> way to retrieve the eReferences of an element referenced by an
> eReference?
> I know, my current approach is not very straight-forward.
I don't really follow your goal given I don't understand what
referenceIsRequired(eRef, typeParam) is trying to determine.
>
> Thanks in advance,
> Matthias
[SOLVED] Re: How to get the eReferences of a referenced class? [message #503108 is a reply to message #502951] Thu, 10 December 2009 16:32 Go to previous message
No real name is currently offline No real nameFriend
Messages: 62
Registered: July 2009
Member
Hi and thanks for your quick reply,

Ed Merks wrote:
> Matthias,
>
> Comments below.
>
> Matthias Schmeling wrote:
>> Hi,
>>
>> I am currently playing around with eReferences.
>> From an existing eReference I try to recover all eReferences that the
>> class of the element has, which is referenced in the existing
>> eReference. Note that I do NOT have an instance of the referenced
>> class, only the eReference and the type of the element it is referencing.
>>
>> Currently, my code looks like this:
>>
>> try {
>> Class<?> clazz = paramMapping.get(eReference.getEType().getName());
>> EditPart editPart = (EditPart)clazz.newInstance();
>> eClass = ((View)editPart.getModel()).getElement().eClass();
> I have no idea what this mapping is doing, but likely
> eReference.getEType() == eReference.getEReferenceType() == eClass

I tried that at first, but in some cases it didn't work for me, so I
tried to work around that ith a mapping which returns the right class,
when I feed it with the classes name.

>> }
>> catch (Exception e) {
>> e.printStackTrace();
>> }
>> if (eClass != null) {
>> for (EReference eRef : eClass.getEAllContainments()) {
>> if (referenceIsRequired(eRef, typeParam)) {
> Not sure what this is doing.
>> return true;
>> }
>> }
>> }
>>
>> First, I retrieve the name of the type of the reference. Let's assume
>> it's name is 'Input'. Then I get the exact class name with all package
>> names by accessing the mapping 'paramMapping'. This gives me the class
>> 'org.example.demo.InputEditPart.class'. After that I try to
>> instantiate the class, which would give me an edit part. After that I
>> could just retrieve the corresponding model element and its class and
>> get the references from it.
>>
>> However, I keep getting an InstantiationException when trying to
>> instantiate with 'newInstance()'.
> Sounds more like a GMF question. Do you really need the EditPart. You
> don't need it to determine the EClass that is the EReference's eType.

No, I suppose I don't need the EditPart. I figured out that
newInstance() doesn't make any sense anyway, because even if I
instantiate the class, the EditPart won't have a model element. I mixed
things up and thought the method would give me the required class of the
element rather than an element itself.

>>
>> Does anyone know how to get this to work? Or is there even an easier
>> way to retrieve the eReferences of an element referenced by an
>> eReference?
>> I know, my current approach is not very straight-forward.
> I don't really follow your goal given I don't understand what
> referenceIsRequired(eRef, typeParam) is trying to determine.

Sorry, I didn't explain it properly. The method is trying to determine
if either the reference eRef is supposed to hold elements of the class
with the name typeParam or if it points to elements that have references
which do.

That I tried to use the EditPart was really a detour. I just tried again
to use getEType. And now I know what went wrong: There was no method
'getEAllContainments()', because it's an EClassifer. But after casting
it to EClass, I could access the method and got all the containment
references.

Thanks, Ed. I wouldn't have tried it again, if you hadn't pointed me to
it :)

Regards,
Matthias

>>
>> Thanks in advance,
>> Matthias
Previous Topic:Legacy Model Handler
Next Topic:[CDO] Long EList:s and caching
Goto Forum:
  


Current Time: Mon Nov 11 19:39:48 GMT 2019

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

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

Back to the top