Skip to main content
Engineering LibreTexts

16.10: Chapter Summary

  • Page ID
    43482
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Reflection refers to the ability to query, examine and even modify the metaobjects of the runtime system as ordinary objects.

    • The Inspector uses instVarAt: and related methods to view private instance variables of objects.

    • Send Behavior>>allInstances to query instances of a class.

    • The messages class, isKindOf:, respondsTo: etc. are useful for gathering metrics or building development tools, but they should be avoided in regular applications: they violate the encapsulation of objects and make your code harder to understand and maintain.

    • SystemNavigation is a utility class holding many useful queries for navigation and browsing the class hierarchy. For example, use SystemNavigation default browseMethodsWithSourceString: 'pharo' matchCase:true. to find and browse all methods with a given source string. (Slow, but thorough!)

    • Every Pharo class points to an instance of MethodDictionary which maps selectors to instances of CompiledMethod. A compiled method knows its class, closing the loop.

    • RGMethodDefinition is a lightweight proxy for a compiled method, providing additional convenience methods, and used by many Pharo tools.

    • RBBrowserEnvironment, part of the Refactoring Browser infrastructure, offers a more refined interface than SystemNavigation for querying the system, since the result of a query can be used as a the scope of a new query. Both GUI and programmatic interfaces are available.

    • thisContext is a pseudo-variable that reifies the runtime stack of the virtual machine. It is mainly used by the debugger to dynamically construct an interactive view of the stack. It is also especially useful for dynamically determining the sender of a message.

    • Intelligent breakpoints can be set using haltIf:, taking a method selector as its argument. haltIf: halts only if the named method occurs as a sender in the run-time stack.

    • A common way to intercept messages sent to a given target is to use a minimal object as a proxy for that target. The proxy implements as few methods as possible, and traps all message sends by implementing doesNotUnderstand:. It can then perform some additional action and then forward the message to the original target.

    • Send become: to swap the references of two objects, such as a proxy and its target.

    • Beware, some messages, like class and yourself are never really sent, but are interpreted by the VM. Others, like +, - and ifTrue: may be directly interpreted or inlined by the VM depending on the receiver.

    • Another typical use for overriding doesNotUnderstand: is to lazily load or compile missing methods.

    • doesNotUnderstand: cannot trap self-sends.

    • A more rigorous way to intercept messages is to use an object as a method wrapper. Such an object is installed in a method dictionary in place of a compiled method. It should implement run:with:in: which is sent by the VM when it detects an ordinary object instead of a compiled method in the method dictionary. This technique is used by the SUnit Test Runner to collect coverage data.


    This page titled 16.10: Chapter Summary is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.