[squeak-dev] Interception messages in Squeak

Yann-Gaël Guéhéneuc yann-gael.gueheneuc at polymtl.ca
Mon Apr 15 04:17:40 UTC 2013


	Dear all,

I am trying to understand better 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 assumed that I 
could override the method `ProtoObject>>withArgs:executeMethod` but 
Stéphane (Ducasse) explained me that for performance reason, this 
method is not used (this is my own summary of his answer). Which 
method should I override / how could intercept sent messages?

Here is the code of 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 this entire piece of code yields:

     This is foo: 42

When I would like to have:

     Caught: This is foo: 42

	I was told about wrapper objects and MethodWrapper (see 
http://stackoverflow.com/questions/15975340/interception-messages-in-squeak) 
but in my quest to understand reflection, I would like to know if 
there would be something more "reflective", typically overriding a 
message :-)

	Thank you very much in advance!
	Yann

-- 
Yann-Gaël Guéhéneuc
Ph.D. et ing. / Ph.D. and eng.
Professeur titulaire / Full professor
Chaire de recherche du Canada sur les Patrons (de) logiciels /
   Canada Research Chair on Software Patterns and Patterns of Software
DGIGL, École Polytechnique        1-514-340-4711 #7116 (Tél. / Phone)
C.P. 6079, succ. Centre-Ville     1-514-340-5139       (Téléc. / Fax)
Montréal, QC, H3C 3A7, Canada     www.ptidej.net


More information about the Squeak-dev mailing list