Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » ClassCastException when Loading with dynamic EMF
ClassCastException when Loading with dynamic EMF [message #508794] Wed, 20 January 2010 10:58 Go to next message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
I have a very simple EMF model, A Parent which has a non containment reference to a Child. The child has an EString attribute called Name.

The Parent EClass is in a 'parent' package in a Parent.ecore, and the Child EClass in in a 'child' package in a Child.ecore.

The Parent EObject instance is persisted to parent.xmi and Child EObject instance is persisted to child.xmi.

Creating the files with Dynamic EMF does not cause problems. Loading is also fine.
But when I attempt to call parentObject.eGet(childReferenceStructuralFeature) after loading the parent.xmi, the folloing class cast exception is thrown.

<TESTED WITH EMF 2.5 200906151043>
java.lang.ClassCastException: The value of type 'class org.eclipse.emf.ecore.impl.DynamicEObjectImpl' must be of type 'org.eclipse.emf.ecore.impl.EClassImpl@be99f8 (name: Child) (instanceClassName: null) (abstract: false, interface: false)'
at org.eclipse.emf.ecore.impl.EStructuralFeatureImpl$InternalSe ttingDelegateSingleEObject.dynamicGet(EStructuralFeatureImpl .java:2318)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1029)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1013)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1005)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec tImpl.java:1000)
at dynamicemftest.minimalistic.ParentChildExample.getFeature(Pa rentChildExample.java:173)
at dynamicemftest.minimalistic.ParentChildExample.loadExample(P arentChildExample.java:80)
at dynamicemftest.minimalistic.ParentChildExample.run(ParentChi ldExample.java:31)
at dynamicemftest.actions.SampleAction.run(SampleAction.java:50 )

I have examined the line EStructuralFeatureImpl .java:2318, and it appears that the resolved Proxy Object is an EObject whose EClass is of type : Child and the EStructuralFeatureImpl's global variable eClass is of the same type, yet the eClass.isInstance(resolvedEObject) condition fails


I have a lightweight example here with one java file and the two ecores to demonstrate the problem:
http://sites.google.com/site/ilovekerrywaves/Home/DynamicEMF Example.zip

If I change the reference to child to a containment reference, and comment out the line which persists the Child into its own resource, then everything works fine.

Also, if the Child EClass is moved to the same package as the Parent, then everything works fine also.

The example on fails specifically in the circumstances outlined above.

Has anyone seen this before ?

[Updated on: Wed, 20 January 2010 11:22]

Report message to a moderator

Re: ClassCastException when Loading with dynamic EMF [message #508808 is a reply to message #508794] Wed, 20 January 2010 12:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Aiden,

Comments below.

Aiden Grandfield wrote:
> I have a very simple EMF model, A Parent which has a non containment
> reference to a Child.
Not so much a Parent in the case of non-containment.
> The child has an EString attribute called Name.
>
> The Parent EClass is in a 'parent' package in a Parent.ecore, and the
> Child EClass in in a 'child' package in a Child.ecore.
>
> The Parent EObject instance is persisted to parent.xmi and Child
> EObject instance is persisted to child.xmi.
>
> Creating the files with Dynamic EMF does not cause problems. Loading
> is also fine. But when I attempt to call
> parentObject.eGet(childReferenceStructuralFeature) after loading the
> parent.xmi, the folloing class cast exception is thrown.
An instance of a dynamic model won't conform to the static generated
version of that same model...
>
> <TESTED WITH EMF 2.5 200906151043>
> java.lang.ClassCastException: The value of type 'class
> org.eclipse.emf.ecore.impl.DynamicEObjectImpl' must be of type
> mailto:'org.eclipse.emf.ecore.impl.EClassImpl@be99f8 (name: Child)
> (instanceClassName: null) (abstract: false, interface: false)'
> at org.eclipse.emf.ecore.impl.EStructuralFeatureImpl$InternalSe
> ttingDelegateSingleEObject.dynamicGet(EStructuralFeatureImpl .java:2318)
> at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec
> tImpl.java:1029)
> at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec
> tImpl.java:1013)
> at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec
> tImpl.java:1005)
> at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjec
> tImpl.java:1000)
> at dynamicemftest.minimalistic.ParentChildExample.getFeature(Pa
> rentChildExample.java:173)
> at dynamicemftest.minimalistic.ParentChildExample.loadExample(P
> arentChildExample.java:80)
> at dynamicemftest.minimalistic.ParentChildExample.run(ParentChi
> ldExample.java:31)
> at dynamicemftest.actions.SampleAction.run(SampleAction.java:50 )
>
> I have examined the line EStructuralFeatureImpl .java:2318, and it
> appears that the resolved Proxy Object is an EObject whoe EClass is of
> type : Child and the EStructuralFeatureImpl's global variable eClass
> is of the same type, yet the eClass.isInstance(resolvedEObject)
> condition fails because they are not the same Java object instance.
So there must be two different EClasses involved.
> I have a lightweight example here with one java file and the two
> ecores to demonstrate the problem:
> http://sites.google.com/site/ilovekerrywaves/Home/DynamicEMF Example.zip
It's full of hard coded paths so that didn't help so much.
>
> If I change the reference to child to a containment reference, and
> comment out the line which persists the Child into its own resource,
> then everything works fine.
>
> Also, if the Child EClass is moved to the same package as the Parent,
> then everything works fine also.
> The example on fails specifically in the circumstances outlined above.
If you look closely at the logic, you'll notice that load each Ecore
into a different resource set so you end up with two versions of
Child.ecore. One referenced by Parent.ecore and another clone loaded
independently in a separate resource set. You need to load all your
Ecore models with a single resource set to maintain their cross references.
>
> Has anyone seen this before ?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ClassCastException when Loading with dynamic EMF [message #508823 is a reply to message #508808] Wed, 20 January 2010 13:37 Go to previous message
Aiden Grandfield is currently offline Aiden GrandfieldFriend
Messages: 9
Registered: July 2009
Junior Member
Thanks a million Ed, this worked perfectly. Using a single ResourceSet to load fixes the problem.

There is one very important thing worth mentioning.

I had already implemented it this way previously and it didn't work. It was out of desperation that I tried a separate ResourceSet to fix the problem. However..... In my original example with the single ResourceSet solution, i had the path to my ecores with double forward slashes

e.g. C://ecores//Parent.ecore
The ResourceSet.getResource() method was able to handle this, so I was unaware that the double forward slashes would cause a problem.
However...,EMF was unable to cross reference the ecores with this path, which gave the exact same ClassCastException

Its just one unlucky co-incidence I guess.
Maybe it would be worth throwing an error to the user at the ResourceSet.createResource() if double forward slashes are present in the file path.

Thanks a million for your help.

[Updated on: Wed, 20 January 2010 13:40]

Report message to a moderator

Previous Topic:[CDO] Methods to retrieve or cascade not yet loaded referencing objects?
Next Topic:How to show an editable properties view
Goto Forum:
  


Current Time: Fri Apr 19 14:29:38 GMT 2024

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

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

Back to the top