• Aucun résultat trouvé

INSPECTING OBJECTS

Dans le document til Smalltalk (Page 164-171)

Inspectors, Notifiers, Debuggers

5.2 INSPECTING OBJECTS

An inspector window allows the internal state of an object to be viewed and modified. For example, inspectors allow a Smalltalk programmer to examine and modify the current values of the instance variables of an object. In addition, the object and its components may be interrogated by sending messages to them or evaluating expressions involving them.

An inspector window can be created on any object simply by sending it the message inspect. For example, suppose we evaluate the following code fragment in a workspace:

I location I

location Eo-(200 @ 300).

locationinspect.

The response to an inspect message is a request to frame a window for the inspector.

An inspector window (see Fig. 5.1) is then generated on the object location. Inspector windows are always labelled with the name of the class of the object being inspected, in this case class Point. Structurally, an inspector window is divided into two panes (see Fig. 5.2):

a variable pane and a value pane. The variable pane contains a menu list of the instance variables of the object. The contents of the value pane are dependent on the selection made from the list of variables in the variable pane. Selecting a variable name from the variable pane causes the current value of that variable to be displayed in the value pane. The value pane is a text pane in which expressions may be entered, edited, and evaluated. The value pane may also be used to modify the values of instance variables of an inspected object.

Inspectors take us inside the object in the sense that any expression we evaluate can reference the object and its instance variables directly; i.e., they allow the programmer to override the

r J j·!l1Iil!

normal requirement that the representation of an object can onlybeaccessed and/or modified using the message protocol supported by the object. Typical yellow button menus associated with the variable and value panes are shown in Fig. 5.2. Note that the inspect entry in the variable pane menu is only available if a variable is selected.

Workspaoe , lIooa tionl

looa tion +- (200@300), looa tion inspeot,

self x

JOO

Figure 5.1 Inspector Window on 'location'.

~1~:~;:~:~:':;::~;;:'I\$.:,:l':~':;i::i~:~,'~'.~:,:",~,:~:::::::,:~;;:,l:::~,l~;l1:,:·:::.J~;X~lt~til41' .

...,

:

---

JOO

self again

x undo

copy

---

cut

~

pastedo It .'

"

linspectl print it .,

s ;

inspeot

aooept :-:

~:. oanoel ~t

'.:-."ili::-... Variable ::

Pane Value Pane

..

....:....!;,

Figure 5,2 Structure of an Inspector Window.

Chapter 5 Debugging with Inspectors, Notifiers, and Debuggers 149

5.2.1 Inspecting the Instance Variables of an Object

Instances of class Point, such as the point bound to location, have two instance variables, x and y, representing the x and y coordinates of the point. The variable pane therefore contains menu entries labelled x and y. In addition, the first entry in the variable pane of any inspector is self, an entry that allows reference to the object being inspected. In general, the variable pane will contain a complete list of the instance variables of the object, including those inherited from superclasses. In Fig. 5.1, the entry y is selected in the variable pane;

the value of y, Le., 300, is displayed in the value pane. Selecting x would display 200.

Selecting self would display 200@300, representing the point (200,300).

The string to be displayed as the value of a variable is computed internally by sending the message printString to the variable. By default, printString simply prints the name of the class to which the object bound to the variable belongs. Many classes provide more specialized printed representations. Integers print as a character string of their digits (e.g., 123), characters print as a dollar sign followed by the character (e.g., $a), strings print as their constituent characters surrounded by single quotes (e.g., 'a string'), and so on. Other classes, such as Point or Dictionary, have even more specialized printed representations (e.g., 200@300, Dictionary ('hi'=>bye' 'white'=>'black')).

5.2.2 Modifying the Values of the Instance Variables of an Object

The value of any instance variable in an inspected object can be modified by selecting the variable to be changed in the variable pane and then typing an expression into the value pane. Selecting accept from the value pane's yellow button menu evaluates the expression and binds the result to the selected instance variable. The new value of the instance variable is displayed in the value pane.

Suppose we wished to change the x coordinate of location to 100. Selecting x in the variable pane displays the current value of the x coordinate (200) in the value pane. Edit this to read 100 and accept the changed value. Selecting self in the value pane will confirm that the new value of location is 100@300.

Expressions in the variable pane may directly reference any instance variable of an inspected object. In addition, they may also use pseudo-variables self and super to refer to the object being inspected. For example, an alternative method of modifying the x-coordinate to 100 would be to accept the expression, self x - 100, or even more simply, x - 100, as the new value for x. Note that the value for self cannot be modified by selecting self in the variable pane and accepting an expression in the variable pane - only instance variables may be modified; self and super are pseudo-variables. Pseudo-variables can never be targets of assignment statements.

5.2.3 Evaluating Expressions within an Inspector

Any expression may be typed into the value pane, selected, and evaluated using do it or print it from the value pane's yellow button menu. Evaluation of the expression is done in the context of the inspected object. The instance variables of the object, together with the pseudo-variables self and super, may be directly referenced within any expression.

I illil I

Another method of modifying the point location from 200@300 to 100@300 would beto evaluate the expression

self x: (x - 1001

in the value pane. The selector x: anArgument defined on points changes the x coordinate of the receiver to anArgument. Select self in the variable pane to confirm that location has been modified correctly.

5.2.4 Inspecting the Instance Variables of an Inspected Object

Sometimes it is necessary to inspect the instance variables of an inspected object. In general, we wish to be able to inspect an object to any level of detail by creating additional inspectors. For example, when inspecting an array object, we might wish to create an inspector on some individual element of the array. An inspector can be created on any selected instance variable within an inspector by selecting inspect from the variable pane's yellow button menu.

For example, suppose we evaluate the following code in a workspace:

I triangle I

triangle~Arraynew:3.

triangle

at: 1 put: 100@140;

at: 2 put: 300@250;

at: 3 put: 3OO@15;

inspect

Aninspector is created on the array of three points named triangle (see the leftmost inspector in Fig. 5.3). The array has three indexed instance variables that are referenced by indices 1,2, and 3 in the variable pane of the inspector. Selecting 3 from the list in the variable pane displays the value stored at position 3 in the array, namely, the point 300@15. If we now select inspect from the variable pane's yellow button menu, an inspector is created on the currently selected instance variable (see the rightmost inspector in Fig. 5.3). We can now examine the instance variables of the third point in the array.

Selecting x from the variable pane displays the x coordinate of this point, 300.

Note that if we modify the values of the instance variables of the point within the point inspector, the change in value will alsobe reflected in the inspector on the array.

Inspect the object created by evaluating each of the following expressions. For each, inspect and modify the instance variables of the object, evaluate expressions involving the inspected object, and open inspectors on the instance variables(if appropriate).

Pen newinspect (7/21inspect Datetoday inspect

RectanglefromUser inspect

(PopUpMenulabels:'doit printit exit' lines: 21inspect

Chapter 5 Debugging with Inspectors, Notifiers, and Debuggers 151

Arra.y

y self

Ii

self 1 2

r~:::::::::::::::::::::::::::::::::::::j

Figure 5.3 Nested Inspector Windows.

What happens

if

you try to activate the yellow button menu of the variable pane when no variable has yet been selected?

5.2.5 Inspecting Dictionaries

It is sometimes convenient to create special inspectors for browsing certain kinds of objects.

Several specialized inspectors are already present in the Smalltalk environment. We will limit discussion todictionary inspectors. Dictionaries in Smalltalk are sets of key-value associations. The user of an inspector on dictionaries should be able to browse through the key-value associations; i.e., select a key and see the corresponding value in the value pane.

Dictionary inspectors provide this capability and, in addition, allow values to be modified and entries (key-value associations) to be addedtoand removed from the dictionary.

Suppose we create a simple telephone directory, add a few entries, and then inspect it.

I telephoneNumbers I

telephoneNumbersf-Dictionarynew.

telephoneNumbers

at:#Johnput:'564-7548';

at:#Daveput:'564-7545';

at:#Wilfput:'564-6301';

inspect.

The resulting dictionary inspector is shown in Fig. 5.4. Dictionary inspectors are structurally identical to normal inspectors. However, rather than a list of instance variables, the variable pane contains the names of the keys for which there are entries in the dictionary.

Selecting a key displays the value associated with that key in the value pane. In Fig. 5.4, key Wilr is selected and his telephone number, 564·6301, is displayed.

The yellow button menu associated with the variable (or key list) pane of a dictionary inspector (see Fig. 5.5) has entries that are different from those provided by a regular inspector. In addition to menu entry inspect, new menu entries references, add field, and

Dictiona.ry

!'III!

remove are also provided. The full menu list appears only if a key is selected from the list of keys in the variable pane.Ifnot, only add field appears in the menu. A selccted key is an implicit argument required by inspect, references, and remove.

Workspace

ItelephoneNumbersl

telephoneNumbers ~ Dictionary new.

telephoneNumbers

at: #John put: '564-7548';

at: #Dave put: '564-7545';

at: #Wilf put: '564-6301';

inspect.

Dave

mn

John

'564-6301 '

A

I

?

>j.

Figure 5.4 A Dictionary Inspector.

inspect references

add field remove

again undo copy cut paste

do It print it inspect accept cancel

.",....~ ...

Figure 5.5 Organization of a Dictionary Inspector Window.

Chapter 5 Debugging with Inspectors, Notifiers, and Debuggers 153

inspect references add field

remove

Opens an inspector on the object (value) associated with the selected key.

Creates a message-set browser on all references to the selected key.

Adds a new entry to a dictionary. A prompter window will appear and request the name of the key tobeadded. An entry will thenbeadded to the dictionary with associated value nil.Ifdesired, another value canbeassociated with the key by entering the value in the value pane and selecting accept.

Removes the key selected in the key list pane. A confirmer will appear to request confirmation that this undoable operation shouldbecarried out.

Why is a specialized inspector for dictionaries desirable? What if we had simply created a normal inspector on the dictionary? To create a regular inspector on the dictionary telephoneNumbers, evaluate the expression super inspect in the variable pane of the inspector (see Fig. 5.6). Using super instead of self will cause the search for the inspect method to start in the superclass of Dictionary - recall that the class for telephone-Numbers is Dictionary. The effect of this will be to invoke the inspect method for normal inspectors defined in class Object rather than the inspect method in class Dictionary.

Dictionary Dave John

(W!If.:::::::::::::::::}

self tally 1 2 3

46 7 8

John- )'564-7548'

~.'

~;.

<:~

L

":.:.. ~.,;:,,:: ...,,J

Figure 5.6 A normal Inspector opened on a dictionary.

r ! 1'!111!1· I

The inspector on telephoneNumbers is shown in Fig. 5.6. Notice that the variable pane now contains the indices of the indexed instance variables rather than the keys. This view of the dictionary emphasizes the fact that a dictionary is an array of associations.

Clearly, this is a physical view of the dictionary, appropriate for an implementor, but not the logical view required by the user. To a user, integer indices are meaningless in general.

For example, the association with key John (see Fig. 5.6) is referenced through index 5!

The instance variable, tally, keeps track of the number of entries in the array that are in use;

i.e., those entries consisting of key-value associations (as opposed to nil).

Evaluate the expression: Dictionary new inspect. Within the resulting inspec-tor. gain experience adding. setting the value of. and removing entries.

What happens

if

you try to add a new field with a duplicate key?

What happens

if

you select accept when no key is selected?

Inspect Smalltalk - a system dictionary.

Dans le document til Smalltalk (Page 164-171)