This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I've an abstract class AbstractModule that implement the interface ModuleInterface.
Now, I build some different class, Module1, Module2, ecc that extends AbstractModule.
I use this function to load and instantiate the class:
In the core of the program i use
where modulo is of type AbstractModule.
The problem is in the row signed by the arrow or in the calling of the loader over there, i recive the exception:
SEVERE: java.lang.ClassCastException: com.bss.framework.bssmoduloutenti.ModuloGrafico cannot be cast to com.bss.framework.astratte.AbstractModule
at com.bss.framework.utils.BssLoader.caricaModulo(BssLoader.java:92) <-- this is the arrowed key
at com.bss.framework.servizi.SessioneUtente.setModuloCorrente(SessioneUtente.java:178) <--- this is the calling
How can I solve it? There is something wrong?
First of all, does com.bss.framework.bssmoduloutenti.ModuloGrafico indeed extend com.bss.framework.astratte.AbstractModule?
Furthermore, the problem might be that you load the ModuloGrafico class using another classloader than what AbstractModule was loaded with. I don't know the exact details, but as far as I know classloaders are separated in such a way that a class loaded with one classloader is in a separate space than classes loaded with a different classloader.
It's just a guess and I'm not sure if it will work, but I think it might work if you initialize your URLClassLoader differently, setting the classloader of AbstraceModule as the parent: