You can test the "internal type" you get as a result of the eval().
TclObject tobj;
// Do a thread safe Tcl eval and set tobj to the interp result.
InternalRep rep = tobj.getInternalRep();
if (rep instanceof TclString) {
String s = rep.toString();
} else if (rep instanceof TclInteger) {
int i = TclInteger.get(interp, rep);
} else if (rep instanceof TclDouble) {
double d = TclDouble.get(interp, rep);
} else if (rep instanceof ReflectObject) {
Object o = ReflectObject.get(interp, rep);
Class cl = ReflectObject.getClass(interp, rep);
}
I hope that clears things up.
Mo Dejong
Red Hat Inc.
On Wed, 19 Apr 2000, Paul Eng wrote:
> Mo,
>
> Thanks for the reply.
>
> Suppose I want to use Jacl to process users' scripts so that I have no idea
> what the return type is going to be. I understand how I could check to see
> if the return object is a ReflectObject and how I could unwrap it to get the
> Java object if it was. But, let's say a proc simply has "return 1". This
> comes back from interp.getResult() as a TclObject, but what I really want is
> a java.lang.Integer. Can I somehow convert it or is my only choice to get
> it as a java.lang.String?
>
> I'm new to Tcl/Jacl, so forgive me if this doesn't make sense.
>
> Thanks,
> Paul Eng
>
>
> -----Original Message-----
> From: Mo DeJong [mailto:[EMAIL PROTECTED]]
> Sent: Friday, April 14, 2000 3:29 PM
> To: Paul Eng
> Cc: [EMAIL PROTECTED]
> Subject: Re: [Tcl Java] Converting Interp.getResult TclObject to Java
>
>
> On Fri, 14 Apr 2000, Paul Eng wrote:
>
> > I am looking into using Jacl to call Tcl scripts. I can call
> interp.eval()
> > and then interp.getResult() to get the return value, but the return value
> is
> > a TclObject. What I would like to do is convert this TclObject result
> into
> > it's appropriate java object, so that if it's a reflected object, I would
> > get the java object, if it's a TclString, I would get java.lang.String,
> etc.
> >
> > I found the convertTclObject() method in JavaInvoke which looks like what
> I
> > want, but the method is not public. Is there a way I can do this or am I
> > missing something?
> >
> > Thanks,
> > Paul Eng
>
> The "trick" here is that Tcl objects have "internal representations",
> that could be a float, a Java object, or whatever. Because a Tcl
> object is a "wrapper" around an InternalRep, you need to "unwrap"
> the TclObject in you Java code if you want to do something with it.
> Lets say you invoke a Tcl proc that does some Java related stuff,
> it sets the interp result to a ReflectObject, a Tcl "wrapper" for
> a Java object.
>
> interp.eval("some_java_func");
>
> Now you can "unwrap" the Java object like so.
>
> TclObject result = interp.getResult();
>
> Object jobj = ReflectObject.get(interp, result);
>
> If you want to know what the Java Class of the reflected
> Java object is, you can find that out like this.
>
> Class jcl = ReflectObject.getClass(interp, result);
>
> (WARNING: YOU MUST GET THE REFLECTED CLASS TYPE LIKE THIS,
> DO NOT CALL THE jobj.getClass() METHOD TO GET THE
> REFLECTED CLASS TYPE, THEY ARE NOT THE SAME THINGS).
>
> To get the "string rep" of a TclObject just call the
> toString() method on the TclObject.
>
> I hope that helps.
> Mo Dejong
> Red Hat Inc.
>
> ----------------------------------------------------------------
> The TclJava mailing list is sponsored by Scriptics Corporation.
> To subscribe: send mail to [EMAIL PROTECTED]
> with the word SUBSCRIBE as the subject.
> To unsubscribe: send mail to [EMAIL PROTECTED]
> with the word UNSUBSCRIBE as the subject.
> To send to the list, send email to '[EMAIL PROTECTED]'.
> An archive is available at http://www.mail-archive.com/[email protected]
>
>
----------------------------------------------------------------
The TclJava mailing list is sponsored by Scriptics Corporation.
To subscribe: send mail to [EMAIL PROTECTED]
with the word SUBSCRIBE as the subject.
To unsubscribe: send mail to [EMAIL PROTECTED]
with the word UNSUBSCRIBE as the subject.
To send to the list, send email to '[EMAIL PROTECTED]'.
An archive is available at http://www.mail-archive.com/[email protected]