[Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751230] |
Thu, 05 January 2017 16:05  |
Eclipse User |
|
|
|
Hi all,
there seems to be a problem with using the instanceof / typeof as well as casts ("as") operators in Xcore when working with dynamic instances.
Consider the following example:
package zoo
import org.eclipse.emf.common.util.BasicEList
import org.eclipse.emf.common.util.EList
class Zoo{
contains Animal[] animals
refers derived volatile unsettable Reptile[] poisounousReptiles get{
var EList<Reptile> oldReptiles = new BasicEList<Reptile>()
for(animal:animals){
if (animal instanceof Reptile){
if(animal.poisounous)
oldReptiles.add(animal)
}
}
return oldReptiles
}
refers derived volatile unsettable Reptile[] poisounousReptiles2 get{
animals.filter(typeof(Reptile)).filter(r | r.poisounous).toEList
}
}
abstract class Animal{
String name
}
abstract class Mammal extends Animal{}
abstract class Reptile extends Animal{
boolean poisounous
}
class Elephant extends Mammal{}
class Snake extends Reptile{}
The uses of instanceof and typeof do not work when loading a dynamic model. When loading a model based on generated Java classes, there is no problem.
When I look at the generated Java code, the problem becomes obvious. An instanceof operation translates directly to a Java instanceof:
...
if ((animal instanceof Reptile)) {
...
When working with a dynamic model, animal will be a DynamicEObject and Reptile will still be a Java interface that is obviously not implemented by DynamicEObject, so the simple instanceof does not do it.
I could of course check that the animal.eClass.name == "Reptile", but then it turns out that casting, which on the Java side will be translated to
... ((Reptile)animal).IsPoisounous() ...
will have the same problem.
Are there any solution to this? Is this a known limitation? (I could not find any related post,..)
Thanks
Joel
|
|
|
|
Re: [Xcore] instanceof / typeof / casting problem when working with dynamic Instances [message #1751252 is a reply to message #1751249] |
Fri, 06 January 2017 05:29   |
Eclipse User |
|
|
|
German,
comments below.
German Vega wrote on Fri, 06 January 2017 10:06
How are you loading your dynamic model?
see the example project that was attached to my initial post. Just open the MyZoo.xmi in the Sample Reflective Editor and see that the derived values will not be shown. In my application I'm loading the models in the same way.
If you launch another eclipse runtime workbench, so that the generated code will be loaded for the Zoo model, and you import the zoo project there as well, and then open the MyZoo model, the derived values will be shown.
German Vega wrote on Fri, 06 January 2017 10:06
If you use the generated package, you should not have instances of DynamicEObject but rather instances of the actual generated implementations (i.e. ReptileImpl).
For our application it is important that we can work with dynamic instances.
German Vega wrote on Fri, 06 January 2017 10:06
Are you executing an eclipse plugin or an standalone application?
All within Eclipse
Quote:
I had similar problems in my standalone application, and were related to wrong manual package registration.
Thanks
Joel
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03874 seconds