Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » ClassCastException on XMI import/export(ClassCastException on XMI import/export)
ClassCastException on XMI import/export [message #820859] Wed, 14 March 2012 17:02 Go to next message
Simon Fremantle is currently offline Simon FremantleFriend
Messages: 4
Registered: March 2012
Junior Member
I export EMF objects to and from XMI for persistence using XMLResource. Most of the time this works very well, but every now and then an error appears, such as the following examples:

mypackage.myclass1 cannot be cast to mypackage.myclass2 at mypackage.myclass3.getMyclass2

java.lang.String cannot be cast to java.util.List at mypackage.myclass2.eIsSet

java.lang.Object cannot be cast to java.util.List at mypackage.myclass2.eIsSet

I cannot see any pattern. Nor can I reproduce the problem.

I am using Galileo SR2, but cannot see a reference to this problem in any bug fixes since that date so doubt it is related to the EMF version.

However, the EMF objects are used in a Web application, which is highly concurrent, so I wonder if the XMLResource load/save options I am using are unsuitable. I based them on EMFPerformanceTips.html from eclipse.org - see below for my code.

Could one of these options be causing the problem? Any advice gratefully received!

Simon

// Resource load and save options
private static final Map resourceLoadOptions = new HashMap();
private static final Map resourceSaveOptions = new HashMap();
private static final XMLParserPool xmlParserPool = new XMLParserPoolImpl();
private static final Map nameToFeatureMap = new HashMap();
private static final List lookupTable = new ArrayList();

// Initialize load and save options
static {
resourceLoadOptions.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION,
Boolean.TRUE);
resourceLoadOptions.put(XMLResource.OPTION_USE_PARSER_POOL,
xmlParserPool);
resourceLoadOptions.put(XMLResource.OPTION_USE_XML_NAME_TO_FEATURE_MAP,
nameToFeatureMap);

resourceSaveOptions.put(XMLResource.OPTION_ENCODING, XML_ENCODING);
resourceSaveOptions.put(XMLResource.OPTION_USE_CACHED_LOOKUP_TABLE,
lookupTable);
resourceSaveOptions.put(XMLResource.OPTION_CONFIGURATION_CACHE,
Boolean.TRUE);
resourceSaveOptions.put(XMLResource.OPTION_FORMATTED, Boolean.FALSE);
}
Re: ClassCastException on XMI import/export [message #821241 is a reply to message #820859] Thu, 15 March 2012 04:53 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Simon,

The nameToFeatureMap and the lookup table in lookupTable are used to
cache information computed about the model so they can be updated while
parsing (for the former) and while saving (for the latter). You could
use thread local instances to avoid sharing them across threads. It
appears you're setting them up in some static singleton way but of
course a thread local solution would require you to set up these
particular options on the thread on which you save or load. You could
do it in the factory if the thread on which you create the resource is
also the one where you load and save.



On 14/03/2012 6:02 PM, Simon Fremantle wrote:
> I export EMF objects to and from XMI for persistence using
> XMLResource. Most of the time this works very well, but every now and
> then an error appears, such as the following examples:
>
> mypackage.myclass1 cannot be cast to mypackage.myclass2 at
> mypackage.myclass3.getMyclass2
>
> java.lang.String cannot be cast to java.util.List at
> mypackage.myclass2.eIsSet
>
> java.lang.Object cannot be cast to java.util.List at
> mypackage.myclass2.eIsSet
>
> I cannot see any pattern. Nor can I reproduce the problem.
>
> I am using Galileo SR2, but cannot see a reference to this problem in
> any bug fixes since that date so doubt it is related to the EMF version.
>
> However, the EMF objects are used in a Web application, which is
> highly concurrent, so I wonder if the XMLResource load/save options I
> am using are unsuitable. I based them on EMFPerformanceTips.html from
> eclipse.org - see below for my code.
>
> Could one of these options be causing the problem? Any advice
> gratefully received!
>
> Simon
>
> // Resource load and save options
> private static final Map resourceLoadOptions = new HashMap();
> private static final Map resourceSaveOptions = new HashMap();
> private static final XMLParserPool xmlParserPool = new
> XMLParserPoolImpl();
> private static final Map nameToFeatureMap = new HashMap();
> private static final List lookupTable = new ArrayList();
>
> // Initialize load and save options
> static {
>
> resourceLoadOptions.put(XMLResource.OPTION_DEFER_IDREF_RESOLUTION,
> Boolean.TRUE);
> resourceLoadOptions.put(XMLResource.OPTION_USE_PARSER_POOL,
> xmlParserPool);
> resourceLoadOptions.put(XMLResource.OPTION_USE_XML_NAME_TO_FEATURE_MAP,
>
> nameToFeatureMap);
>
> resourceSaveOptions.put(XMLResource.OPTION_ENCODING,
> XML_ENCODING);
>
> resourceSaveOptions.put(XMLResource.OPTION_USE_CACHED_LOOKUP_TABLE,
> lookupTable);
> resourceSaveOptions.put(XMLResource.OPTION_CONFIGURATION_CACHE,
> Boolean.TRUE);
> resourceSaveOptions.put(XMLResource.OPTION_FORMATTED,
> Boolean.FALSE);
> }


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ClassCastException on XMI import/export [message #821367 is a reply to message #821241] Thu, 15 March 2012 09:04 Go to previous messageGo to next message
Simon Fremantle is currently offline Simon FremantleFriend
Messages: 4
Registered: March 2012
Junior Member
Ed

Thank you very much. This explains the problem entirely. I'll use ThreadLocal instances as you suggest.

Regards
Simon
Re: ClassCastException on XMI import/export [message #822423 is a reply to message #821367] Fri, 16 March 2012 15:51 Go to previous messageGo to next message
Simon Fremantle is currently offline Simon FremantleFriend
Messages: 4
Registered: March 2012
Junior Member
Thanks again - that worked to some extent. However, I still get errors of the form below when using EcoreUtil.copy(). I can't see how to use EcoreUtil in a ThreadLocal fashion, but is there a related problem here?

Regards
Simon

java.lang.ClassCastException: java.lang.Object cannot be cast to myClass1
at myClass2.getMyClass1(myClass2.java:135)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1013)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1005)
at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1000)
at org.eclipse.emf.ecore.util.EcoreUtil$Copier.copyAttribute(EcoreUtil.java:619)
at org.eclipse.emf.ecore.util.EcoreUtil$Copier.copy(EcoreUtil.java:479)
at org.eclipse.emf.ecore.util.EcoreUtil.copy(EcoreUtil.java:360)
Re: ClassCastException on XMI import/export [message #822747 is a reply to message #822423] Sat, 17 March 2012 07:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Simon,

What's a myClass1? Is it really not in a package? Is it really a class
with a name that doesn't start with an upper case letter? Is there a
dynamic model involved (as opposed to a generated one)? It's
apparently an attribute (rather than a reference). What's the type of
the field for the attribute? Are all your threads working with
separate resource sets? Is OPTION_EXTENDED_META_DATA involved?



On 16/03/2012 4:51 PM, Simon Fremantle wrote:
> Thanks again - that worked to some extent. However, I still get
> errors of the form below when using EcoreUtil.copy(). I can't see how
> to use EcoreUtil in a ThreadLocal fashion, but is there a related
> problem here?
>
> Regards
> Simon
>
> java.lang.ClassCastException: java.lang.Object cannot be cast to myClass1
> at myClass2.getMyClass1(myClass2.java:135)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1013)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1005)
> at
> org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1000)
> at
> org.eclipse.emf.ecore.util.EcoreUtil$Copier.copyAttribute(EcoreUtil.java:619)
> at
> org.eclipse.emf.ecore.util.EcoreUtil$Copier.copy(EcoreUtil.java:479)
> at org.eclipse.emf.ecore.util.EcoreUtil.copy(EcoreUtil.java:360)


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ClassCastException on XMI import/export [message #826559 is a reply to message #822747] Thu, 22 March 2012 08:12 Go to previous message
Simon Fremantle is currently offline Simon FremantleFriend
Messages: 4
Registered: March 2012
Junior Member
Ed

The "myClassN" examples are all true Java classes, first letter capitalized in the code, in packages. The model is generated, and the examples refer to attributes not references. The error occurred with various field types, both native Java and EMF. The threads have separate resource sets. OPTION_EXTENDED_META_DATA is not involved.

Having said all this, the subsequent EcoreUtil.copy() error seems to have been due to data that had been incorrectly stored before making the load and save option caches ThreadLocal. We cleared the database, and the EcoreUtil.copy() errors went away.

So thank you very much indeed for your help, which has allowed us to resolve this problem.

Regards
Simon
Previous Topic:How to add a Super type to a Eclass programmatically?
Next Topic:Need Key-Shortcuts at caontext menu
Goto Forum:
  


Current Time: Thu Apr 25 05:46:52 GMT 2024

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

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

Back to the top