Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Calling eGet for invalid structural feature
Calling eGet for invalid structural feature [message #1421404] Thu, 11 September 2014 10:18 Go to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
I want to eGet a given feature on a collection of EObjects, but not all
of the EObjects are instances of the EClass that defines the feature.

So which is preferable?

// This is the shortest, but is this safe? I guess if the feature isn't
defined on the objects (super)type(s) it will just return null..
for (EObject e : someEObjects){
Object o = e.eGet(feature)
}

// check if all structural features include the interesting feature
for (EObject e : someEObjects){
if (e.eClass().getEAllStructuralFeatures().contains(feature)){
Object o = e.eGet(feature)
}
}

// check if the object is an instance of the type that defines the feature
for (EObject e : someEObjects){
if (feature.eContainingClass().isInstance(e)){
e.eGet(feature)
}
}
Re: Calling eGet for invalid structural feature [message #1421505 is a reply to message #1421404] Thu, 11 September 2014 13:03 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. DamusFriend
Messages: 1161
Registered: July 2009
Location: Canada
Senior Member

Hi, Felix,

I express my own opinions, below. Probably not the canonical answer :-)

Christian


On 2014-09-11 10:18:03 +0000, Felix Dorner said:

> I want to eGet a given feature on a collection of EObjects, but not all
> of the EObjects are instances of the EClass that defines the feature.
>
> So which is preferable?
>
> // This is the shortest, but is this safe? I guess if the feature isn't
> defined on the objects (super)type(s) it will just return null..
> for (EObject e : someEObjects){
> Object o = e.eGet(feature)
> }

This throws some kind of RuntimeException.


> // check if all structural features include the interesting feature
> for (EObject e : someEObjects){
> if (e.eClass().getEAllStructuralFeatures().contains(feature)){
> Object o = e.eGet(feature)
> }
> }

That works.


> // check if the object is an instance of the type that defines the feature
> for (EObject e : someEObjects){
> if (feature.eContainingClass().isInstance(e)){
> e.eGet(feature)
> }
> }

This feels best IMO because one really ought to be able to rely on the
EMF Run-time actually making the feature available if the instance
conforms to the feature's defining class.
Re: Calling eGet for invalid structural feature [message #1421543 is a reply to message #1421505] Thu, 11 September 2014 14:03 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
On 11/09/2014 15:03, Christian W. Damus wrote:

>> // This is the shortest, but is this safe? I guess if the feature
>> isn't defined on the objects (super)type(s) it will just return null..
>> for (EObject e : someEObjects){
>> Object o = e.eGet(feature)
>> }
>
> This throws some kind of RuntimeException.

Thanks, I was being too lazy to check or look. Still, it's the sexiest
approach - until you run it :)

>
>> // check if the object is an instance of the type that defines the
>> feature
>> for (EObject e : someEObjects){
>> if (feature.eContainingClass().isInstance(e)){
>> e.eGet(feature)
>> }
>> }
>
> This feels best IMO because one really ought to be able to rely on the
> EMF Run-time actually making the feature available if the instance
> conforms to the feature's defining class.
>
I also think this one is better then.
Re: Calling eGet for invalid structural feature [message #1423837 is a reply to message #1421543] Mon, 15 September 2014 04:11 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 28973
Registered: July 2009
Senior Member
Comments below.

On 11/09/2014 4:03 PM, Felix Dorner wrote:
> On 11/09/2014 15:03, Christian W. Damus wrote:
>
>>> // This is the shortest, but is this safe? I guess if the feature
>>> isn't defined on the objects (super)type(s) it will just return null..
>>> for (EObject e : someEObjects){
>>> Object o = e.eGet(feature)
>>> }
>>
>> This throws some kind of RuntimeException.
>
> Thanks, I was being too lazy to check or look. Still, it's the sexiest
> approach - until you run it :)
>
>>
>>> // check if the object is an instance of the type that defines the
>>> feature
>>> for (EObject e : someEObjects){
>>> if (feature.eContainingClass().isInstance(e)){
>>> e.eGet(feature)
>>> }
>>> }
>>
>> This feels best IMO because one really ought to be able to rely on the
>> EMF Run-time actually making the feature available if the instance
>> conforms to the feature's defining class.
>>
> I also think this one is better then.

Yes, or alternatively checking that
e.eClass().getEAllStructuralFeatures().contains(feature) or for even
better performance, using eObject.eGet(e.eClass().getFeatureID(feature))
*but* checking that the feature ID >= 0.
Re: Calling eGet for invalid structural feature [message #1424103 is a reply to message #1423837] Mon, 15 September 2014 13:09 Go to previous message
Christian W. Damus is currently offline Christian W. DamusFriend
Messages: 1161
Registered: July 2009
Location: Canada
Senior Member

On 2014-09-15 04:11:54 +0000, Ed Merks said:

>
> Yes, or alternatively checking that
> e.eClass().getEAllStructuralFeatures().contains(feature) or for even
> better performance, using
> eObject.eGet(e.eClass().getFeatureID(feature)) *but* checking that the
> feature ID >= 0.

Whoa, that one rather blows my mind.

:-)
Previous Topic:Call for Submissions: Modeling Symposium ECE 2014
Next Topic:Ecore Tools and Ecore digram in eclipse Luna
Goto Forum:
  


Current Time: Thu Nov 23 02:12:23 GMT 2017

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

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