On Wed, 28 Jun 2000, Dr Wes Munsil wrote:

> Mo DeJong wrote:
> 
> > Objects do not have types, references to objects determine what behavior
> > the object will provide. In Tcl/Java you don't really have a reference
> > but you "reflect" an object as a type. You need to pass in the
> > java.lang.Class object that a given java.lang.Object will be reflected
> > as, and it needs to be the correct class (which is not always the same
> > as the one returned by Object.getClass() ).
> 
> In general, what is "the correct class?"
> 
> In particular, what is the correct class in my example:
> 
>      Object o = v.elementAt (i);
>      ... ReflectObject.newInstance (interp, ???, o) ...

It depends on what you are doing. In regular Java, you need to know
what the type of the objects you put into a vector are because
you need to cast them back up to something when you pull them
out of the vector. TclJava is no different, you just put the
class you would cast to as the argument to newInstance().

> > I thought the docs were clear, but it sounds like they will need some
> > work. Would you like to help? I have tried writing them a couple
> > of times but it seems like the message is still not getting through.
> >
> > ...
> >
> > 2. If you call getClass() it will always return the most derived
> >    type, this is wrong is many many ways. It can lead to problems
> >    with method invocation and it will let you call methods that
> >    would not be accessable in regular Java.
> 
> I would love to help, as soon as I understand what's going on. Could you provide a
> small test case that erroneously uses getClass() and illustrates the problems that
> can arise?

There is one in the documentaiton for ReflectObject.

http://dev.scriptics.com/man/java1.2.6/TclJavaLib/ReflectObject.htm

Note how the example class would be reflected as the wrong type.


Here is another example:


import java.util.Hashtable;
public class Hashtable2 extends Hashtable
{
  public static Hashtable get() {
    return new Hashtable2();
  }
  public void NEVER_CALL() {
    System.out.println("NEVER_CALL");
  }
}


% set h [java::call Hashtable2 get]

( Here is what would happen if you called getClass() )
% java::info class $h
Hashtable2

This means you would be able to invoke "$h NEVER_CALL"
which is not possible from regular Java code.

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/tcljava@scriptics.com

Reply via email to