Help needed for a spying object

Stephen Pair spair at advantive.com
Thu Oct 4 17:14:39 UTC 2001


This is one case where delegation can be really useful.  Rather than
simply forwarding the message to the spied object in does not
understand, you can delegate the message...sends to self in the spied
object will come back through the spy, and any methods in the spied
object that return self will be returning the spy (since self in the
delegated method will be the spy).

If your spy subclasses ProtoObject, and only adds #doesNotUnderstand:
it's protocol will be nearly identical to the spied object (as close as
you can currently get in Squeak without causing problems).

Of course, the current Squeak VM doesn't support delegation, so you'll
need to get the one from: http://spair.swiki.net/delegation

I'd be interested to hear more about your column on reflection...Squeak
doesn't really support reflection (though you can certainly do
everything that reflection allows).  The closest thing it has is the
class, which allows you to observe and manipulate the behavior of
instances, but doesn't allow you to get at the actual slots of an
instance.  To get at the slots of an instance, you can use methods like
#instVarAt: and #instVarAt:put:, but these methods must be installed on
the instance in question...but what if you didn't want your object to
respond to these methods?  Reflection would allow for that.

- Stephen

> -----Original Message-----
> From: squeak-dev-admin at lists.squeakfoundation.org 
> [mailto:squeak-dev-admin at lists.squeakfoundation.org] On 
> Behalf Of ducasse stephane
> Sent: Thursday, October 04, 2001 4:58 AM
> To: squeak-dev at lists.squeakfoundation.org
> Subject: Help needed for a spying object
> 
> 
> Hi
> 
> I'm trying to finish to write a column on using reflection in 
> Squeak. I wanted to show a little spy that puts on the 
> transcript all the messages sent to the spied object.
> 
> So I subclassed from protoObject redefined DNU.
> 
> ProtoObject subclass: #Spy
>     instanceVariableNames: 'spyedObject '
>     classVariableNames: ''
>     poolDictionaries: ''
>     category: 'Scaffolding'
> 
> Spy>> doesNotUnderstand: aMessage
> 
>     Transcript nextPutAll: '*' ; flush.
>     ^ spyedObject perform: aMessage selector withArguments: 
> aMessage arguments
> 
> Spy>>on: anObject
> 
>     spyedObject := anObject
> 
> 
> 
> Then created the class method on: as follow on Spy
> 
> Spy class>>on: anObject
> 
>     | spy |
>     spy := self new.
>     spy become: anObject.
>     anObject on: spy.
>     ^ anObject
> 
> I have now two problems
> 
> First problem:
>     
>     
>     |t |
>     t := OrderedCollection new.
>     Spy on: t. 
>     t add: 3.
>     t add: 5.
>     t 
> 
>     When I execute the following code everythign works
>     Then when I open an inspector on t and send messages there are not
>     captured. I don't understand why. I alreadyplayed a lot 
> with become: and
>     nasty stuff in VW but here I'm stuck.
> 
> 
> Second problem:
> 
>     I wanted to have a spy on a morph. Crazy me....It was 
> nearly working on BoucingAtomsMorph but I tried on something 
> simpler liek
>     RectangleMorph or TextMorph but I got a lot of strange 
> errors or nealry damage my system
> 
> 
> testDamagingThe28System
> 
>     |t |
>     t := TextMorph new.
>     t openInWorld.
>     Spy on: t. 
> 
> 
> testWithEventErrorin28
> 
> |t |
> t := RectangleMorph new.
> t openInWorld.
> Spy on: t. 
> 
> Have you any idea or suggestions?
> 
> Stef
> 
> 
> 
> 
> 
> 
> 
> 





More information about the Squeak-dev mailing list