swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Are Messages First Class Entities In Java?


From: Christoph Schlumpf
Subject: Re: Are Messages First Class Entities In Java?
Date: Tue, 22 Oct 1996 09:58:15 +0200

Dear Manor,

To dynamically invoke methods on an arbitrary Object you have to use the
Reflection API of Java. The first release of this API will be ready to
download at javasoft this december. You can already get the Specification
of this API from:

http://www.javasoft.com/products/JDK/1.1/designspecs/reflection/index.html.

With Reflections you can construct method pointers this way:

Method aMessage = myAgentClass.getMethod("doSomething", null);

You can build a container for messages called for example Event. This container
has two fields:

Object theObject;
Method theMethod;

This container has a constructor:

void Event(Object anObject, Method aMethod){
        theObject= anObject;
        theMessage=aMessage;}

so you can build an event Object:

Method aMessage = myAgentClass.getMethod("doSomething", null);
Event anEvent = new Event(myAgentObject, aMessage);

In the Event Class you can put a method to invoke the message:
public Object execute(Object[] args) {
        Object result = theMethod.invoke(theObject, args);
        return result;}

Thats it. now you can invoke the Event whenever you want:

Event.execute(null);

or you can build a new Event Object for the same Message:

Event aSecondEvent= new Event(aSecondAgentObject; aMessage)

I must admit that this is only conceputal code. Each method in Java has to
throw the appropriate Exceptions. Remember that Java is much more Typesave
than other languages. There are Exceptions thrown whenever you want to
construct a method object for a class which doesn't have this method or
when you want to invoke a method on an object which doesn't belong to the
right class.

The Reflection API has (as far as I konw) some advantages over the action
messages used in swarm/ObjC. You can invoke methods with an arbitrary
number of arguments and any return type (int long Object Class Array ...).

This are only ideas out of my first look at the Reflection API. Perhaps
there are pitfalls and problems which I haven't seen. I hope nevertheless
that that I could give you some ideas. My conclusion is that Messages in
Java are first class Entities (they are members of the class Method and are
independent of an Object at hand).

I'm very interrested in your further evaluation work on java.

Yours,
Christoph


>------------------------------------------------------------------------
>
>This message is meant primarily for those who asked to have Swarm ported
>to Java... I have a question for you, which I'm not clear about.
>
>------------------------------------------------------------------------
>
>I was going through the Java language definition and I couldn't find a
>way to separate a message from the target object to which it should be
>sent, in other words, I wasn't able to find a way to do the equivalent
>of:
>
>  Objective-C
>  -----------
>
>  // Note: This sort of thing is crucial for a general-purpose
>  // simulation framework (i.e. a simulation environment that
>  // doesn't need recompilation whenever a user adds new classes
>  // of simulated objects)...
>
>  -createEventFor: anObject Do: (SEL) aSelector {
>
>    return [anObject perform: aSelector] ;
>
>  }
>
>
>  Java
>  ----
>
>  createEventFor(Object anObject, XXX theMethod){
>    anObject.theMethod() ;   // how...?
>  }
>
>  Note: It is illegal to declare anObject of class Object, so we already
>        have to be more specific, but in any case, I couldn't find what
>        I should write instead of XXX... Unless I'm missing something
>        obvious, Java is much more strongly typed than I thought, and
>        consequently there is no way for us to implement a general-purpose
>        schedule class.
>
>Have I missed something obvious???
>
>Regards,
>
>Manor.

at work:_______________________________________________________________
Christoph Schlumpf                   e-mail:          address@hidden
EAWAG, Switzerland                   phone:              +41-1-823-5535
Environmental Physics                fax:                +41-1-823-5210
Ueberlandstr. 133                    by foot:                  room D16
CH-8600 Duebendorf                   www: http://www.eawag.ch/~schlumpf
                              ,,,
                             (o o)
private:-----------------oOO--(_)--OOo---------------------------------
Christoph Schlumpf                   phone:              +41-1-948-0829
Seestr. 21
CH-8617 Moenchaltorf




reply via email to

[Prev in Thread] Current Thread [Next in Thread]