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?
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 ...