views:

156

answers:

4

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?

+2  A: 

In Squeak, see the class ObjectTracer. The class comment describes how to use it. You should be able to accomplish what you need with that, or at least using that as a model.

Randal Schwartz
+1  A: 

Hi,

it's kinda funny, we were just discussing that in the Squeak irc channel. Take a peek at ObjectViewer, perhaps. So, I've read your question better and so, I'd propose this.

In your example, you want to intercept the message sends to a SmallInteger. Funnily enough, what I propose 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.

Ok, that was cool, aye?

cheers,

Niko

nes1983
+1  A: 

You can use method wrappers. To see what method wrappers are you can look for a paper called "Wrappers to the rescue". I think there is a package for squeak that already implements method wrappers. In addition, you can see how a test code coverage analysis is made in the last version of Pharo because it uses a kind of method wrapper to see what methods are evaluated during a test run. cheers, Gaboto

+1  A: 

Have a look at the Reflectivity.

Unfortunately some of the paper links are not working, and I don't remember the exact invocation from the top of my head, but it's really easy to instrument code as you want, and even do it at runtime. Look for examples using class Link.

Damien Pollet