Hello -
I originally posted this on comp.lang.java.programmer, but the responses I
got seem to indicate that the problem has more to do with this being a
servlet, so I repost it here...
I am writing a servlet that acts as a dispatcher between applets and classes
on the server whose methods those applets need to call. (These classes on
the server
should never get downloaded to the client for security reasons.) All of
these classes on the server extend the same class, Solver. Solver has the
method, checkAttempt(). Any subclass of Solver is meant to override this
method.
My servlet uses a URLClassLoader to load the appropriate class based on a
string passed to it from the applet. I am able to get the class loaded, but
when I try to cast it to Solver, so I can call it's checkAttempt() method, I
get a ClassCastException. Here is how I do the class loading:
private Solver getSolver(String solverName) {
Class solverClass;
Solver theSolver = null;
try {
log("about to load class... "+solverName);
solverClass = loader.loadClass(solverName);
log("loaded class: "+solverClass);
log("parent:"+solverClass.getSuperclass());
log("about to instantiate...");
Object theInstance = solverClass.newInstance();
log("instantiated");
if(theInstance instanceof Solver ) {
log("about to cast as Solver...");
theSolver = (Solver)(theInstance);
log("cast solver:"+theSolver);
} else {
log("couldn't cast, not a Solver!");
}
} catch (Exception e) {
log(""+e);
}
return theSolver;
}
"loader" is a URLClassLoader prepared with the appropriate codebase upon
initilization of the servlet. A check of the logs shows that the class I'm
trying to instantiate has Solver as a Superclass, but then "theInstance"
somehow does not end up being an actual instance of Solver, and thus not
castable to Solver. How can this be? What am I doing wrong?
--
John Brecht
Department of Physics and Astronomy
Michigan State University
[EMAIL PROTECTED]
----------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
READ THE FAQ!!!! <http://java.apache.org/faq/>
Archives and Other: <http://java.apache.org/main/mail.html/>
Problems?: [EMAIL PROTECTED]