[Newbies] How to introspect method instance variables

Yoshiki Ohshima yoshiki at vpri.org
Tue Aug 25 03:39:24 UTC 2009


At Mon, 24 Aug 2009 21:16:16 -0500,
rdmerrio wrote:
> 
> I have defined a method, i.e.,
> 
> someMethod
>     anInstVariable := anotherInstVariable1 + anotherInstVariable2.
> 
> I would like to intercept the acceptance of this method by the browser 
> and programatically determine what instance variables this method is 
> using so that I can grab these names for other processing tasks.
> 
> Additionally, I would really like to be able to determine what instance 
> variables are being assigned to, for instance, anInstVariable in this 
> case and which ones are the "independent" instance variables, 
> anotherInstVariable1 and anotherInstVariable2 in this case.
> 
> How can I do this?

  The "why" question Michael asked is an interesting one, but I can
see many potential use of it.  (I've been writing a few automatic inst
var access rewriting compilers and friends with similar techniques.)

  During the compilation, the Compiler uses Encoder to support the
compilation and Encoder keeps track of the accesses of variables.

Encoder>>init:context:notifying:

populates "scopeTable" inst var with VariableNodes.  When an
assignment to the variable is encountered, #nowHasDef message is sent
to the node.  When a reference is encountered, #nowHasRef message is
sent to the node.  The default behavior is do nothing, but you can
define methods like (in the latest image, the actual class name varies
based on the Squeak version):

InstanceVariableNode
nowHasRef
	Transcript show: 'ref: '; show: self printString; cr.

InstanceVariableNode
nowHasDef
	Transcript show: 'def: '; show: self printString; cr.

and you get the list of these accesses.

To be less intrusive, you can store these info into somewhere and
analyze it.

-- Yoshiki



More information about the Beginners mailing list