[106] in java-interest
Re: Java
daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Sat May 20 18:31:08 1995
Date: Sat, 20 May 1995 15:23:29 -0700
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: java-interest@java.Eng.Sun.COM
Hi David,
Thanks for your mail.
> Michael Gersten wrote:
>
> > > #2. Can a arbitrary message be sent to an arbitrary
> > > object? I think not.
> >
> > No. But you could write a class with some native methods
> > to do it for you. It would be a huge security hole...
>
> If the call fails when the message is not found, and if argument
> types are checked dynamically, why would it be a security hole?
> Something like this is necessary to cleanly support CORBA and other
> object RPC/IPC systems, interfaces to dynamic languages, etc.
You are right that the security hole can be plugged. But there
is some cost associated. Calling a dynamic method will be very
slow because you have to dynamically select the appropriate method
(deal with method overloading), check the accesibility of the
method, convert all the arguments to the appropriate types and
call the method.
You can already support CORBA systems. Most IDL based systems
simply generate the stub code for the required interfaces at
compile time. This deals with all the type checking issues.
I believe that there is already an IDL backend for Java that
lets it talk to Spring and DOE.
We used to have an extention that would allow arbitrary methods
to be called but we found that it was used rarely because most
cases could be solved better using interfaces. We therefore
gave it up to make the system simpler.
> > > The reason for this is that you may have abstract
> > > controller classes that work with other objects. A simple
> > > example is a list of objects, which might be sent a
> > > method to send to all objects contained within that list.
> >
> > This can be done using interfaces. Make sure that all the objects
> > on the list implement the Fooable interface so that you can call
> > foo() on them.
>
> I think Michael was talking about doing this for arbitrary methods
> (as in the Smalltalk collection classes, for example). It's very
> useful to be able to broadcast a message to all members of a data
> structure without knowing statically what the message selector is.
>
> If you don't have something like this, then some of the things you
> can use it for can also be done using parameterized types. Are there
> any plans to add parameterized types to Java?
I would like to have parameterized types, it would make some of
the code a lot more efficient. We've thought about this but
we haven't really found a simple implementation. I find that
interfaces eliminate most of the need for parameterized types
though.
> > > #3. Am I correct in thinking that, like C++, the compiler
> > > knows either
> > > A) The address of the destination method, or
> > > B) The address of a pointer to the destination method
> > > in every case, at compile time?
> >
> > No. Everything is bound lazily at runtime.
>
> Yes, but there are various ways of doing runtime binding.
> In C++, each class has a vtable (array of pointers to methods),
> and the offset within that table for each call is known statically.
> The code for a dynamic call is something like:
> *((*(receiver->vtable)) [offset]) ();
Unfortunately, if the offset changes you have to recompile your
program. That makes C++ a very bad choice for writing long lived
libraries. Changes the API of objects often means that you have
to recompile all the code that uses the API. This is avoided in
Java by determining the offset at link time.
> In, say, Objective-C, only the selector is known statically, and
> something like a hashtable lookup is used to get the method
> implementation. The selector is global, and the same selector can
> be used between programs that were compiled separately.
> Java uses vtables, right?
Right. That is very similar to how Java does it.
> Also, how are interfaces implemented? Using method lookup, as in
> Cecil, or by creating stubs for each combination of interface and
> class, as in GNU g++?
Interface method calls are looked dynamically. The result of the
lookup is cached so that the lookup can be avoided in subsequent calls.
> > > Q2: Since C programs can do this, and since they might
> > > make a non-existant call, don't you need a
> > > NoSuchMethod exception as well? (or did I miss that
> > > while reading the manual)
> >
> > C programs are "unsafe". If the method does not exist you
> > will get an IncompatibleClassChangeException.
>
> What if the method does exist, but the arguments are of
> incompatible types (or you have the wrong number of arguments)?
>
> Also, what happens if that method name is overloaded? Which method
> gets called?
The C program must specify an exact method signature. If the
method signature doesn't match exactly you will get an exception
and the method won't be called.
Have fun,
Arthur van Hoff (avh@eng.sun.com)
http://java.sun.com/people/avh/
Sun Microsystems Inc, M/S UPAL02-301,
100 Hamilton Avenue, Palo Alto CA 94301, USA
Tel: +1 415 473 7242, Fax: +1 415 473 7104
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com