The Artima Developer Community
Sponsored Link

Java Answers Forum
dynamic class re-loading: class casting problem

1 reply on 1 page. Most recent reply: Mar 30, 2004 5:01 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Richard Stahl

Posts: 1
Nickname: riso
Registered: Mar, 2004

dynamic class re-loading: class casting problem Posted: Mar 30, 2004 6:38 AM
Reply to this message Reply
Advertisement
hello,

i would like to reload my classes on user request with an up-to-date version of a given class, i.e., i have something like recompiling class loader which checks validity of class file and source file.

class reloading works fine yet i get an ClassCastException when casting the new instance ...

Class c = cloader.loadClass("Tester");
Object o = c.newInstance();

// this throws a ClassCastException
TesterInterface t = (TesterInterface) o;

i think it is a problem of name spaces for different class loaders. i have read on the net that using interface (TesterInterface) is possible to avoid this issue. i have tried it out and it does not work.

could you please let me know what could be the problem or how to make it working?

thanks a lot.

best regards,
riso


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: dynamic class re-loading: class casting problem Posted: Mar 30, 2004 5:01 PM
Reply to this message Reply
Richard,

Yes, a class is defined by its (full) name (meaning package included) but also by the class loader used to load the class definition... You can then have the following nasty code

DataSource ds = null; // First definition of the class in the program
... // doing some other things
ds = (DataSource) getDataSourceFromJNDi (DS_NAME);

the last line throws of cours a ClassClastException.
So this should reply tothe first part of your post.

For the second part of your question, I have never personnaly written a CustomClassLoader. If you could post the reference you used I could try to have a look at it one of these eve's.

In the references I got, there is nothing relevant. As far as I can remember Mark Grand talked about the Classloader in one of his books (see the O'Reilly reference).

Now to finish off this post :
I think the way to avoid Exception is to use the Dynamic Proxy pattern (for which Mark Grand wrote something on the JavaWorld web site).

Cheers,

\T,


---

Here below a set of reference you may find worth ...

http://www.hta-bi.bfh.ch/~hrm/JavaTechnologiesCourse/
And more specifically a review of all (most) the possible problems : http://www.hta-bi.bfh.ch/~hrm/JavaTechnologiesCourse/Examples/ClassLoader/LoadClass/

Theorical : http://mindprod.com/jgloss/classloader.html

Mark Grant's book @ O'Reilly : http://www.oreilly.com/catalog/javafund/toc.html

JavaWorld references :
Classloader things : http://www.javaworld.com/javaworld/jw-04-2000/jw-0421-zipclass.html
Dynamic proxies : http://www.javaworld.com/javaworld/jw-11-2000/jw-1110-proxy.html

Flat View: This topic has 1 reply on 1 page
Topic: need help ! Previous Topic   Next Topic Topic: Help with jasper reports

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use