Method Spies


 | tools+ | restore | home |

What are Method Spies?

Inserting a Spy

Editing a Spy

Spy Examples


  What are Method Spies?

Method Spies are additional sections of code inserted invisibly into existing methods. The code of the spy has full access to all variables of the method in which it is inserted – self, parameters, temporaries, instance variables etc – and hence can ‘spy’ on any aspect of it. In addition, spies can execute any valid code, from a simple self halt to sophisticated diagnostics.

In addition , the code of a method spy can refer to the spy itself through the pseudo-variable thisSpy. Via thisSpy, the spy can maintain and interrogate information about itself and its activations. To facilitate this, the spy provides two variables – context and activationContext. context is a Dictionary which is global to all activations of the spy; activationContext is also a Dictionary, but a separate activationContext exists for each activation of the spy. All activationContexts are stored within the spy, and can be separately inspected.


Advantages of method spies
  • Transparent insertion/removal – Inserting a method spy does not involve modifying the original source code of a method. Thus a Spy can be inserted into a method without marking that method’s owning Package as having changed, and once removed leaves the method exactly as it was prior to use of the spy.
  • Quick – self halt can be inserted into a method with a single key press
  • Controllable – spies can be enabled/disabled individually or en masse
  • Easy to track – by browsing the ‘method spies’ method category, one can easily identify all spies within a class, hierarchy of classes or the whole image
  • Flexible – using thisSpy, context and activationContext a method spy has sophisticated control over its own execution. For example, it is possible to have the spy code execute once only, or to capture arguments or other data from its host method for later examination.


© 2002 Solutions Software Ltd.