CLASS
tcl.lang.ReflectObject -- This class is used to reference Java objects.
IMPLEMENTS
tcl.lang.InternalRep
METHODS
static TclObject newInstance(Interp interp, Class class, Object javaObj)
static Object get(Interp interp, TclObject tobj)
static Class getClass(Interp interp, TclObject tobj)
ARGUMENTS
DESCRIPTION
newInstance
get
getClass
SEE ALSO
KEYWORDS

CLASS

tcl.lang.ReflectObject -- This class is used to reference Java objects.

IMPLEMENTS

tcl.lang.InternalRep

METHODS

static TclObject newInstance(Interp interp, Class class, Object javaObj)

static Object get(Interp interp, TclObject tobj)

static Class getClass(Interp interp, TclObject tobj)

ARGUMENTS

Interp interp
If tobj is not a valid TclObject, an error message is left in the interpreter's result object unless interp is null.

Object javaObj
Java Object to wrap into a TclObject.

Class class
Class that the javaObj will be reflected as.

TclObject tobj
TclObject from which to extract the Java Object or Class.

DESCRIPTION

This class is used to create and access arbitrary Java objects using the Java Reflection API. It wraps a Java object (i.e., an instance of a Java class) in a handle and exposes the handle to Tcl scripts. The object is registered inside the interpreter and is given a string name. Tcl scripts can manipulate this object as long as the the reference count of the object is greater than zero.

newInstance
The newInstance method creates an object handle to javaObj. The object handle can later be used to access javaObj via the java::* commands. This method is normally used to pass Java object to Tcl scripts. For example, a command procedure can return an object handle to Tcl scripts by executing the following:
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));

get
The get method returns the Java Object stored inside tobj. The tobj argument must contain a valid Java object handle, otherwise an error is reported.

getClass
The getClass method returns the Java Class of the Java Object stored inside tobj. The tobj argument must contain a valid Java object handle, otherwise an error is reported.

SEE ALSO

InternalRep, java, TclObject, TclList, TclString, TclInteger, TclBoolean, TclDouble

KEYWORDS

reflect object, internal representation, object, object type
Copyright © 1996-1998 Sun Microsystems, Inc.
Copyright © 1995-1997 Roger E. Critchlow Jr.