This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Question about custom classloaders in GCJ
- From: Sal <gcj at svf dot dreamhost dot com>
- To: java at gcc dot gnu dot org
- Date: Wed, 09 Jun 2004 23:15:39 -0400
- Subject: Question about custom classloaders in GCJ
I am trying to figure out a way to solve an issue I'm having with custom
classloaders under GCJ. Its not really a GCJ 'bug', just that I'm
trying to do some things that I don't think custom classloaders were
intended for...
Let me try to explain the problem. I would like to make an
application-wide hook to the classloading system... and under most
conditions send the normally requested class to the caller, but under
some conditions send a different class with different functionality but
identical call signature (class naming, inheritance, method naming,
etc). The reason I am doing this is to 'inject' RMI-type objects via
'new' depending on the IP address of the machine making the request.
I was able to trap all classloading by instantiating the application's
'main' object through a custom classloader:
//ccl is a custom classloader object
Class clas = ccl.loadClass( mainProgramClass);
Class mainArgs[] = { (new String[0]).getClass() };
Method mainMtd = clas.getMethod( "main", mainArgs);
mainMtd .invoke( null, arguments);
Then in the custom classloader load all .class files manually with:
byte classdata[] = getClassBytes( classFile);
clas = defineClass( name, classdata, 0, classdata.length );
resolveClass( clas );
return clas;
This works fine under Sun's VM. If it is a normal case the normal class
file gets sent, if not... a RMI enabled .class is loaded from another
location and sent.
Now I am trying to do the equivalent in GCJ, however work with native
objects. I don't mind if the RMI objects are loaded from class files,
but how can I delegate the 'normal' cases back to the underlying system,
so in the normal case the natively compiled objects are used?
The problem is if I do this:
clas = Class.forName(name);
To delegate loading to a parent classloader, all future objects
allocated within that object will then use that classloader and not my
custom one. Is there a way to 'fool' the system into thinking that my
classloader loaded the class? And have it work with native objects?
If this isn't currently possible with GCJ I would understand... however
if there are any pointers on how I could implement it (via hacking GCJ
source directly) I would be interested in giving that a try.
Thanks in advance,
- Sal