Reflection: Basic Introspection

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

What Definitions Say: Reflection

Reflection is the ability of a program to manipulate as data something representing the state of the program during its own execution.

What Definitions Say: Reification

Reification is the process to transform implicit to explicit (objects)

Pharo is a Reflective System

A system having itself as application domain and that is causally connected with this domain can be qualified as a reflective system Maes'87

Inspector

How does it access internal object state?

.

State Introspection

Accessing and setting object state

Object >> instVarAt: aNumber
Object >> instVarAt: aNumber put: anObject
Object >> instVarNamed: aString
Object >> instVarNamed: aString put: anObject

Accessing/Setting State Example

pt := 10@3.
pt instVarNamed: 'x'.
> 10 
pt instVarNamed: 'x' put: 33.
pt
> 33@3

Accessing Class

Object >> class
'hello' class
(10@3) class
Smalltalk class
Class class
Class class class
Class class class class

Querying the System

OrderedCollection allSuperclasses size.
OrderedCollection allSelectors size.
OrderedCollection allInstVarNames size.
OrderedCollection selectors size.
OrderedCollection instVarNames size.
OrderedCollection subclasses size.
OrderedCollection allSubclasses size.
OrderedCollection linesOfCode.

Querying the System

SystemNavigation default browseAllImplementorsOf: #,

.

Sending a Message by its Name

Sending a Message by its Name

Object >> perform: aSymbol 
Object >> perform: aSymbol with: arg
5 factorial
5 perform: #factorial

Classes Hold Compiled Methods

.

Executing a Compiled Method

CompiledMethod >> valueWithReceiver:arguments:
(Integer>>#factorial)
  valueWithReceiver: 5 arguments: #()
(SmallInteger>>#factorial)
  valueWithReceiver: 5 arguments: #()

Summary

/