I am trying to understand the best reflection in Smalltalk. I am using the latest version of Squeak (v4.3). I want to intercept every message sent to instances of one of my classes. I suggested that I could override the method ProtoObject>>withArgs:executeMethod, but Stéphane Ducasse explained to me that for performance reasons this method is not used (this is my own summary of its answer). What method should I override / how to intercept sent messages?
Here is the code for my attempt:
Object subclass: #C
instanceVariableNames: 'i'
classVariableNames: ''
poolDictionaries: ''
category: 'CSE3009'.
C class compile: 'newWithi: anInt
^(self new) i: anInt ; yourself.'.
C compile: 'withArgs: someArgs executeMethod: aMethod
Transcript show: ''Caught: ''.
^ super withArgs: someArgs executeMethod aMethod.'.
C compile: 'foo: aText
Transcript show: aText.
Transcript show: i.
Transcript cr.'.
C compile: 'i: anInt
i := anInt.'.
o := C newWithi: 42.
o foo: 'This is foo: '.
Executing all of this code snippet yields:
This is foo: 42
When I would like to have:
Caught: This is foo: 42
source
share