Search code examples
smalltalksqueakobject-model

In Squeak, how to wrap every method send?


I created a class, and in that class I have a method 'sendMessage: to: withArgs:' which recieves an object, a message and an array of arguments. The method is used to send messages to object and perform some algorithm. To use this method I have to create an instance x of the class I created and do something like x sendMessage: '+' to: '7' withArgs: '#(5)'. The result of this sending the message '+' to the object 7 with the parameter 5, plus some stuff that my algorithm does. But what I want is that the algorithm will be used in every method call, meaning 7+5 will call my 'sendMessage: to: withArgs:'. How can I do that? Or at least, Is there something called in each method sent to every object?


Solution

  • It's kinda funny, we were just discussing that in the Squeak irc channel. Take a peek at ObjectViewer, perhaps.

    In your example, you want to intercept the message sends to a SmallInteger. Funnily enough, ObjectViewer works with very much every class BUT SmallInteger.

    So, to intercept message sends to myObject, do this.

    Create class Intercepter, let it inherit from ObjectTracer, perhaps. Change doesNotUnderstand to something that serves you:

    doesNotUnderstand: aMessage
       "do Mojo to aMessage as you describe it"
    

    Then, to get your stuff going, create your Intercepter:

    myIntercepter := Intercepter on: myObject.
    

    And then

    myObject become: myInterceptor.