[1178] in java-interest

home help back first fref pref prev next nref lref last post

Re: help with class loader

daemon@ATHENA.MIT.EDU (Chuck McManis)
Fri Aug 25 15:01:35 1995

Date: Fri, 25 Aug 1995 09:07:21 -0700
From: cmcmanis@scndprsn.Eng.Sun.COM (Chuck McManis)
To: ekim@nyquist.bellcore.com
Cc: java-interest@java.Eng.Sun.COM, jkanerva@scndprsn.Eng.Sun.COM


Class loader secrets:

	#1) After calling defineClass() you _must_ call resolveClass() before you
	    hand the class back to the person loading it.

	#2) You must use the system versions of class Class and Object or it won't
	    be able to cast your objects into something runnable. Typical class loader
	    flow is:
			loadClass(String name, boolean resolve) {
			    Class c = Class.forName(name);
			    if (c == null) {
				c = (Class)(classHashTable.get(name));
				if (c != null)
				    return c;
			    }
			    ... load class some other way ...
			    c = defineClass(...);
			    if (resolve)
			        resolveClass(c)
			    classHashTable.put(name, c);
			    return c;
			}

	#3) To invoke this new class you need to define an interface loaded on the
	    "system" side of the class loader (for example
		public interface fooable {
			void foo();
		}
	    and then cast your new object to a fooable and invoke foo. like so:
		c = tstLoader.loadClass("my.class", true);
		Object x = c.newInstance();
		((fooable) c).foo();
	    Of course you can do this in only one line:
		(fooable)(c.newInstance()).foo();
	
	#4) Classes loaded through your class loader will be run through the code
	    verifier. (This is a good way to see how good the code verifier is!) 
	    That also means that if you compile them with the -O switch they may not
	    verify (the compiler inlines accesses to private data which the verifier
	    will complain about).

That's about it. Once you've got a working class loader you have to be aware however
that a lot of the system classes key off the fact that there is a class loader on
the stack and assume you are an applet. So if your newly loaded class tries to open
a file for example it will be constrained to the read path and write path that HotJava
uses. This will change somewhat in Beta. 

--Chuck
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post