static TclObject newInstance(Interp interp, Class class, Object javaObj)
static Object get(Interp interp, TclObject tobj)
static Class getClass(Interp interp, TclObject tobj)
Object obj_to_return = new Object();
interp.setResult(
ReflectObject.newInstance(interp, Object.class, obj_to_return));
The object handle returned by this method is only valid in the given
interpreter, interp.
It is CRITICAL that the class argument to newInstance
be of the correct type. For example, an instance of a String object
can be referenced as a String or as an Object but it can not
be referenced as say an Integer. It is also CRITICAL that
an object not be referenced as it's most derived type when it should really
be referenced as a parent type. Here is an example of a common mistake:
// assume that getHashtable() is defined like this:
// public Hashtable getHashtable() { return new DerivedFromHashtable(); }
Hashtable h = getHashtable();
interp.setResult(
ReflectObject.newInstance(interp, h.getClass(), h));
This object should be referenced as a Hashtable not a
DerivedFromHashtable. If you incorrectly reference this
object as it's most derived type the reflection system will
not work as expected and you could introduce serious security
bugs into your program. The correct way to implement the
previous example is like this:
// assume that getHashtable() is defined like this:
// public Hashtable getHashtable() { return new DerivedFromHashtable(); }
Hashtable h = getHashtable();
interp.setResult(
ReflectObject.newInstance(interp, Hashtable.class, h));
Copyright © 1996-1998 Sun Microsystems, Inc. Copyright © 1995-1997 Roger E. Critchlow Jr.