[2153] in java-interest

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

Re: Dynamic Loading of Classes

daemon@ATHENA.MIT.EDU (Chuck McManis)
Tue Sep 26 07:38:27 1995

Date: Mon, 25 Sep 1995 10:26:28 -0700
From: cmcmanis@scndprsn.Eng.Sun.COM (Chuck McManis)
To: gbolcer@liege.ICS.UCI.EDU, mikec@metronet.com
Cc: java-interest@java.Eng.Sun.COM

>In article  Gregory Alan Bolcer <gbolcer@liege.ICS.UCI.EDU> writes:
>>    I've programmed myself into a corner, and I am seeking advice
>>on the following problem.  I have a Java unix process that is
>>maintaining a dynamically changeable object model (categories) 
>>built on top of Java.  The methods to these categories can be 
>>dynamically changed at runtime and are implemented as Java classes
>>within a file directory.  

Mike Responds...

>Sounds cool however, Java is not a dynamic language in the sense that 
>Smalltalk is a dynamic language. This works it self back to the request for 
>perform: capabilities ala Smalltalk. The reason for no perform: (as it was 
>explained to me) is that it opens a big security hole. For example, a Nasty 
>Old Java App (NOJA) could evoke the method "MySystem.sendUglyMailToMyBoss" if 
>a class with such an interface exists. I guess that the NOJA could scan the 
>class which it finds on a given workstation and try things at random causing 
>all sorts of havoc. 

Mike is correct, however if your already running UNIX processes then
you are not an applet and just a Java ap. Consider writing your own
"perform" class, to do some (most?) of it is fairly trivial.

NOTES: THE FOLLOWING CODE IS WRITTEN "on the fly" And probably won't
	even compile, much less work, however it should provide sufficient
	information to do what you want.

Write a class "DynamicMethodInvoker" which is defined something like:
class DynamicMethodInvoker {
    static {System.loadLibrary("dynamix");}

    private native Object executeDynamicMethod(Object o, String meth,
					String sig, Object args[]);

    public Object doDynamicMethod(Object o, String meth, String sig, 
		Object args[]) {
	/* Implement some security policy here. Maybe call SecurityManger
	   canInvoke or something you make up, what ever you do, don't
	   let applets call this. */
	SecurityManager.mySecurityPolicy();
	return (executeDynamicMethod(o,meth, sig, args));
    }
}

And in the C code write:

int parseSignature(char *sig) {
    static char *cursig;
    if (sig != NULL) {
	cursig = sig;	/* set the signature */
	cursig = strchar(sig, '['); /* find the arguments */
	return 0;
    }
    return (getnextparameter(cursig)); /* return next argument type
				          (int, object ref, etc) */
}

int sigComponents(char *sig) {
	/* return number of arguments to the method */
}

ClassDynamicMethodInvoker_executeDynamicMethod(HObject *this,
	HObject *that, HString *meth, HString *sig, HArrayOfObject *args) {
	long res;
	
	switch (sigComponents(sig)) {
	case 0:
		res = java_execute_dynamic_method(getEE(), unhand (that),
			unhand(meth), unhand(sig));
		break;

	case 1:
		parseSignature(sig);
		res = java_execute_dynamic_method(getEE(), unhand (that),
			unhand(meth), unhand(sig),getnextParameter(sig));
		break;
	...
	/* repeat for as many arguments as you would like. */

	}
	type = parsesignatureResult(sig);
	/* this should simply parse the return type. */
	/* if it is a simple type (int, long, etc) then call
	   execute_java_constructor() on the result with the
	   simple type's class
	   (ie execute_java_constructor(..,"Integer", res,...)
	*/
	return res;
}

Note that amateur spelunkers should not try to do this, it will just
confuse you.

--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