• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

ClassCastException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Listers,

I'm debugging application about ClassCastException and end up with the following problem:
I have class ServletContextImpl which extend ServletContext
and a method getServletContext of ServletContext class return ServletContext type.

When I try the cast (ServletContextImpl)getServletContext() I get ClassCast Exception even the output of the call getServletContext().getClass().getName() return actually ServletContextImpl.

I don't understand the operator cast or instanceof check what to decide the object IS A X or not.

Any help to debug my problem would be very appreciated.
PS. I know the application customize the ClassLoader. Is there any relationship whith the cast?

Have nice a day


Ben
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ben]: PS. I know the application customize the ClassLoader. Is there any relationship whith the cast?

Yes. Assuming that getClass().getName() returns the same package name as well as class name, this is probably the problem. If the exact same class is loaded by two different class loaders (and not by some shared ancestor class loader which defines the requested class), then it's really two different classes as far as Java is concerned. Application servers will do this sort of thing to prevent different applications from affecting each other - static variables in one class, loaded by one application, won't affect the same variables in the same class loaded by a different application. you can call getClass().getClassLoader() to identify which classloader loaded a class, and compare to ServletContextImpl.class.getClassLoader().

Ultimately the best solution here is generally to not cast to ServletContextImpl, but instead just use ServletContext. If you really, really need to call other methods, you might be able to get at them using reflection - but I would avoid this if possible.
 
Ben Mati
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Jim.

I'm following your suggestion to add some methods to the interfaces to use the polymorphism to avoid the cast and it seems working fine.

Thanks again for input it was very helpfull.


Have nice a day,


Ben
 
reply
    Bookmark Topic Watch Topic
  • New Topic