Re: alternative to my ClassLoader hack
Mark Space wrote:
I have my version working now. The secret is to poke the object made
with the new classloader reflectively. I guess the exception I get
above is related to the fact that MyClassLoader is loaded by two
different classloaders, and therefore one class type isn't the same as
the other. Weird, but true.
Yeah, you had:
// in MyClassLoader
Class<Launcher> main = (Class<Launcher>) cl.loadClass( "fubar.Launcher" );
Launcher mcl = main.newInstance();
....but the Launcher class being referred to statically in this code was
loaded by the system classloader (we'll call it Launcher1), whereas the
one referenced by 'main' has come from 'cl' (we'll call it Launcher2),
so a Class<Launcher1> can't newInstance a Launcher2.
This version does pass the first MyClassLoader.class to the second,
and then compares the two class types and determines they are not
equal, as expected. I think this program and Stevens programs are
equivalent, showing that both ideas are valid.
(This one used anymore?:)
class Launcher
{
public void launch()
{
System.out.println( "Classloader: " + getClass().
getClassLoader() );
}
}
class FubarLoader extends URLClassLoader
{
public FubarLoader( URL[] urls )
{
super( urls );
}
@Override
public Class<?> loadClass( String className )
throws ClassNotFoundException
{
System.out.println( "finding " + className );
if( className.startsWith( "fubar" ) ) {
Class<?> c = null;
try {
c = findClass( className );
}
catch( ClassNotFoundException ex ) {
}
if( c != null ) {
System.out.println( "findClass got it" );
return c;
}
}
System.out.println( "trying super class..." );
return super.loadClass( className );
}
}
Ah, this inverts the normal order of super.loadClass being called before
this.findClass, hence you can load an already-loaded class with a
different classloader.
Cheers!
--
ss at comp dot lancs dot ac dot uk