Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Dynamic checking the EObject type
Dynamic checking the EObject type [message #424155] Fri, 17 October 2008 18:08 Go to next message
Yigal Spinner is currently offline Yigal SpinnerFriend
Messages: 127
Registered: July 2009
Senior Member
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 #424159 is a reply to message #424155] Fri, 17 October 2008 18:36 Go to previous messageGo to next message
Cameron Bateman is currently offline Cameron BatemanFriend
Messages: 481
Registered: July 2009
Senior Member
> I want to write a generic method that will add an EObject to any EList.
...
> So far I have the following:
> EObjectEList<?> mySpecailList;
> EObject newElement;

> if (newElement instanceof
> mySpecailList.getEStructuralFeature().getEType()) {
> mySpecailList.add(newElement);
> }

You might be better trying newElement.eClass() ==
mySpecialList.getEStructuralFeature().getEType())....

Could you expand on your usage scenario? Most ELists are already
explicitly designed to constrain what may be added to them. It seems like
you are working around one of the basic premises; can you elaborate on
why? Are you sure there isn't already an alternative in the framework to
what you want?

If what you want is your own impl of EList that doesn't check the type
being added, you might sub-class BasicEList (or even EObjectEList) and
override validate to simply return its argument.


--Cam
Re: Dynamic checking the EObject type [message #424160 is a reply to message #424155] Fri, 17 October 2008 18:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
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


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic checking the EObject type [message #424161 is a reply to message #424159] Fri, 17 October 2008 18:40 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
Cameron,

I never thought to ask why he wanted it...

Cameron Bateman wrote:
>> I want to write a generic method that will add an EObject to any EList.
> ..
>> So far I have the following:
>> EObjectEList<?> mySpecailList;
>> EObject newElement;
>
>> if (newElement instanceof
>> mySpecailList.getEStructuralFeature().getEType()) {
>> mySpecailList.add(newElement);
>> }
>
> You might be better trying newElement.eClass() ==
> mySpecialList.getEStructuralFeature().getEType())....
>
> Could you expand on your usage scenario? Most ELists are already
> explicitly designed to constrain what may be added to them. It seems
> like you are working around one of the basic premises; can you
> elaborate on why? Are you sure there isn't already an alternative in
> the framework to what you want?
>
> If what you want is your own impl of EList that doesn't check the type
> being added, you might sub-class BasicEList (or even EObjectEList) and
> override validate to simply return its argument.
>
>
> --Cam
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic checking the EObject type [message #424200 is a reply to message #424160] Mon, 20 October 2008 14:50 Go to previous messageGo to next message
Yigal Spinner is currently offline Yigal SpinnerFriend
Messages: 127
Registered: July 2009
Senior Member
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
Re: Dynamic checking the EObject type [message #424202 is a reply to message #424200] Mon, 20 October 2008 16:04 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
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


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:EMF Databinding for JFace's IViewerObservableSet
Next Topic:Re: [Tenco] library editor tutorial
Goto Forum:
  


Current Time: Thu Apr 18 22:10:19 GMT 2024

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

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

Back to the top