[squeak-dev] Interception messages in Squeak
Chris Muller
asqueaker at gmail.com
Mon Apr 15 20:18:22 UTC 2013
I take it you're looking for something lower-level than overriding
doesNotUnderstand:.
I don't know if this meets your qualifications, but one thing Magma
does for its write-barrier is dynamically compile an anonymous
subclass of the classes wanting to be overridden. Then use
#primitiveChangeClassTo: to change the class of existing instances to
the anonymous class.
On Sun, Apr 14, 2013 at 11:17 PM, Yann-Gaël Guéhéneuc
<yann-gael.gueheneuc at polymtl.ca> wrote:
>
> 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
|