Dynamic checking the EObject type [message #424155] |
Fri, 17 October 2008 14:08  |
Eclipse User |
|
|
|
I want to write a generic method that will add an EObject to any EList.
Since I want to make sure that the EObject is of the correct type, I
would like to do a dynamic "instanceof" on the EObject and validated it
against the EList types.
So far I have the following:
EObjectEList<?> mySpecailList;
EObject newElement;
if (newElement instanceof
mySpecailList.getEStructuralFeature().getEType()) {
mySpecailList.add(newElement);
}
(of course the above code has compile errors, but it shows the basic
idea I'm trying to accomplish).
I tried to compare the class names, but the newElement class has the
"Impl" package and extension, while the EType does not.
I do not want to manipulate the string because it will not be generic
(in case the EMF generation will use different packages or extensions).
Does anyone knows how to do this?
Thanks
Yigal
|
|
|
|
|
|
|
Re: Dynamic checking the EObject type [message #424202 is a reply to message #424200] |
Mon, 20 October 2008 12:04  |
Eclipse User |
|
|
|
Yigal,
Notification.getFeature should help you identify which feature the value
was added to...
Yigal wrote:
> Ed Thank you again (for so many times that you help).
> Your code works exactly as I needed. Just to clarify my question:
> 1. I did not want to write customize Elist.
> 2. I have a generic listener on th eObject.'
>
> The listener gets notification for a new eObject added to a list in
> the eObject. However, since there are several lists in the object,
> for each one the listener gets a notification. When I created the
> listener I pass the list that it should be concern about. The code
> you helped me with allows me to identify if the current object
> notification belongs to the list in the current listener or pass on
> the execution.
>
> Thanks Yigal
>
>
> Ed Merks wrote:
>> Yigal,
>>
>> Comments below.
>>
>> Yigal wrote:
>>> I want to write a generic method that will add an EObject to any EList.
>> Like list.add(o)? :-P
>>> Since I want to make sure that the EObject is of the correct type, I
>>> would like to do a dynamic "instanceof" on the EObject and validated
>>> it against the EList types.
>> Kind of like
>> ((EStructuralFeature.Setting)list).getEStructuralFeature().g etEType().isInstance(o)?
>>
>>>
>>> So far I have the following:
>>> EObjectEList<?> mySpecailList;
>> I'd recommend not assuming that a specific implementation class is
>> being used...
>>> EObject newElement;
>>>
>>> if (newElement instanceof
>>> mySpecailList.getEStructuralFeature().getEType()) {
>>> mySpecailList.add(newElement);
>>> }
>> Surprising how close that is to what's available hey? :-P
>>>
>>> (of course the above code has compile errors, but it shows the basic
>>> idea I'm trying to accomplish).
>>>
>>> I tried to compare the class names, but the newElement class has the
>>> "Impl" package and extension, while the EType does not.
>>> I do not want to manipulate the string because it will not be
>>> generic (in case the EMF generation will use different packages or
>>> extensions).
>>>
>>> Does anyone knows how to do this?
>>>
>>> Thanks
>>> Yigal
|
|
|
Powered by
FUDForum. Page generated in 1.45568 seconds