lecture: reflection
DESCRIPTION
Lecture: Reflection, University of Berne, 05.03.2008TRANSCRIPT
![Page 2: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/2.jpg)
© Marcus Denker
Roadmap
> Introduction: Reflection> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection
![Page 3: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/3.jpg)
© Marcus Denker
Roadmap
> Introduction: Reflection> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection
![Page 4: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/4.jpg)
© Marcus Denker
System
Definition:
A computational system is a computer-based system whose purpose is to answer questions and/or support actions about some domain.
(P. Maes, “Concepts and Experiments in Computational Reflection," Proceedings of OOPLA 87)
![Page 5: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/5.jpg)
© Marcus Denker
Causally Connected
Definition:
A system is said to be causally connected to its domain if the internal structures and the domain they represent are linked in such a way that if one of them changes, this leads to a corresponding effect of the other.
(Patty Maes, OOPSLA 87)
![Page 6: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/6.jpg)
© Marcus Denker
Reflective System
Definition:
A reflective system is a system which incorporates causally connected structures representing (aspects of) itself.
(Patty Maes, OOPSLA 87)
![Page 7: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/7.jpg)
© Marcus Denker
Introspection
> Introspection— Self-representation can be queried
> Intercession— Self-representation can be changed
Reflection = Introspection + Intercession
![Page 8: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/8.jpg)
© Marcus Denker
Structure and Behavior
> Structural Reflection — Concerned with static structure— For example: packages, data-types, procedures
> Behavioral Reflection— Concerned with execution— For example: procedure execution, assignment, variable read
![Page 9: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/9.jpg)
© Marcus Denker
Tower of Interpreters
> First studied for procedural languages
> David A. Smith: 3Lisp
> Tower-of-Interpreters
> Theoretical. Slow!User Program running a level 0
Interpreter at level 1
Interpreter at level 2
Interpreter at level 3
![Page 10: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/10.jpg)
© Marcus Denker
Reflection and OOP
> A good match: self-representation build of objects— Better then interpreter data-structures
> Language-based reflection— Language entities represented as objects— Meta-objects describe behavior of base level objects
> Structure: classes/methods are objects> Behavior: meta-objects define behavior
— Example: meta-class defines method lookup
![Page 11: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/11.jpg)
© Marcus Denker
Example: Java
> Structural introspection— java.lang.reflect— Query a model of the program (classes, protocols)
> Limited intercession— No change of classes
> Limited behavioral reflection— Wrappers on objects— No way to intercept method calls, variable access
![Page 12: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/12.jpg)
© Marcus Denker
Example: Squeak
> Squeak has support for reflection
> Structural reflection— Classes / methods are objects— Can be changed at runtime
> Behavioral reflection— Current execution reified (thisContext)— #doesNotUnderstand / MethodWrappers
![Page 13: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/13.jpg)
© Marcus Denker
Can we do better?
> Structural Reflection stops at method level— Bytecode in the CompiledMethod: Numbers— Text: Just a String, needs to be compiled
> Behavior hard coded in the Virtual Machine— Message Sending— Variable Access
> Both structural and behavioral reflection is limited— We should do better!
![Page 14: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/14.jpg)
© Marcus Denker
Roadmap
> Introduction: Reflection> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection
![Page 15: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/15.jpg)
© Marcus Denker
Structural Reflection
> Structure modeled as objects
— e.g. Classes, methods— Causally connected
> Uses:— Development environments— Language extensions and experiments
![Page 16: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/16.jpg)
© Marcus Denker
Methods and Reflection
> Method are Objects— e.g in Smalltalk
> No high-level model for sub-method elements— Message sends— Assignments— Variable access
> Structural reflection stops at the granularity of methods
![Page 17: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/17.jpg)
© Marcus Denker
Sub-Method Reflection
> Many tools work on sub method level— Profiler, Refactoring Tool, Debugger, Type Checker
> Communication between tools needed— Example: Code coverage
> All tools use different representations— Tools are harder to build— Communication not possible
![Page 18: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/18.jpg)
© Marcus Denker
Existing Method Representations
> Existing representations for Methods
— Text
— Bytecode
— AST
![Page 19: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/19.jpg)
© Marcus Denker
Requirements
> Causal Connection
> Abstraction Level
> Extensibility
> Persistency
> Size and Performance
![Page 20: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/20.jpg)
© Marcus Denker
Text
> Low level abstraction— String of characters
> Not causally connected— Need to call compiler
![Page 21: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/21.jpg)
© Marcus Denker
Bytecode
> Low level abstraction— Array of Integers
> Missing extensibility— e.g. for tools
> Mix of base- and meta-level code— Problems with synthesized code when changing code— Examples: AOP point-cut residues, reflection hooks
![Page 22: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/22.jpg)
© Marcus Denker
Abstract Syntax Tree
> Not causally connected— Need to call compiler
> Not extensible— Fixed set of codes, no way to store meta data
> Not persistent— Generated by compiler from text, never stored
![Page 23: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/23.jpg)
© Marcus Denker
Solution: Reflective Methods
> Annotated, persistent AST> Bytecode generated on demand and cached
:ReflectiveMethod
annotation
#(12 13 45 38 98 128 84 72 42 77 22 28 59
32 7 49 51 87 64)
:CompiledMethod
compiledMethod
reflectiveMethodannotation
Tools VM
![Page 24: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/24.jpg)
© Marcus Denker
Persephone
> Implementation of Reflective Methods for Squeak
> Smalltalk compiler generates Reflective Methods— Translated to bytecode on demand
> Open Compiler: Plugins— Called before code generation— Transform a copy of the AST
![Page 25: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/25.jpg)
© Marcus Denker
Requirements revisited
> Abstraction Level OK
> Causal Connection OK
> Extensibility OK
> Persistency OK
> Size and Performance OK
![Page 26: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/26.jpg)
© Marcus Denker
Annotations
> Source visible annotations— extended Smalltalk syntax
> Source invisible annotations— Reflective API— Can reference any object
> Every node can be annotated> Semantics: Compiler Plugins
(9 raisedTo: 10000) <:evaluateAtCompiletime:>
![Page 27: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/27.jpg)
© Marcus Denker
Example: Pluggable Type-System
> Example for textual annotations
bitFromBoolean: aBoolean <:type: Boolean :> ^ (aBoolean ifTrue: [1] ifFalse: [0]) <:type: Integer :>
> Optional, pluggable type-system> Types stored as annotations in the Reflective Methods
![Page 28: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/28.jpg)
© Marcus Denker
Memory
number of classes memory
Squeak 3.9
Persephoneno reflective methods
Persephonereflective methods
2040 15.7 MB
2224 20 MB
2224 123 MB
![Page 29: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/29.jpg)
© Marcus Denker
Roadmap
> Introduction: Reflection in Squeak> I. Sub-Method Structural Reflection> II. Partial Behavioral Reflection
![Page 30: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/30.jpg)
© Marcus Denker
Behavioral Reflection
> Reflect on the execution— method execution— message sending, variable access
> In Smalltalk— No model of execution below method body— message sending / variable access hard coded by VM— #doesNotUnderstand / MethodWrappers
> Reflective capabilities of Smalltalk should be improved!
![Page 31: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/31.jpg)
© Marcus Denker
MetaclassTalk
> Extends the Smalltalk metaclass model— Similar to CLOS MOP
> Metaclass defines— message lookup— access to instance variables
> Problems:— Reflection only controllable at class boundaries— No fine-grained selection (e.g. single operations)— Protocol between base and meta level is fixed
![Page 32: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/32.jpg)
© Marcus Denker
Reflex: Partial Behavioral Reflection
> Hooksets: collection of operation occurrences> Links
— Bind hooksets to meta-objects— Define protocol between base and meta
> Goals— Highly selective reification— Flexible meta-level engineering
– Protocol specification– Cross-cutting hooksets
activation
condition
hookset
metaobject
links
Tanter, OOPSLA03
![Page 33: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/33.jpg)
© Marcus Denker
Example: Profiler
> Operation: — Method execution (around)
> Hookset: — All execution operations in a package
> Meta-object: — A profiling tool activation
condition
hookset
metaobject
links
![Page 34: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/34.jpg)
© Marcus Denker
Reflex for Squeak
> Partial Behavioral Reflection pioneered in Java— Code transformation at load time— Not unanticipated (itʼs Java...)
> Geppetto: Partial Behavioral Reflection for Smalltalk— For Squeak 3.9 with Bytecode transformation
![Page 35: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/35.jpg)
© Marcus Denker
Problems
> Annotation performance— Decompile bytecode
> Execution performance— Preambles for stack manipulation
> Low-level representation— ifTrue:ifFalse:— Blocks— Global variables
![Page 36: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/36.jpg)
© Marcus Denker
Method
Links as Annotations
Meta
Link
> Links can be annotations on the AST
![Page 37: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/37.jpg)
© Marcus Denker
Properties
> Very fast annotations— No decompile!
> On-the-fly code generation— Only code executed gets generated
> Generated code is fast— Better then working on bytecode level
![Page 38: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/38.jpg)
© Marcus Denker
Demo
> Show Bounce Demo
![Page 39: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/39.jpg)
© Marcus Denker
Reflectivity
> Prototype implementation in Squeak
— Sub-Method Structure— Partial Behavioral Reflection
> Download:
http:/scg.unibe.ch/Research/Reflectivity
![Page 40: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/40.jpg)
© Marcus Denker
Whatʼs next...
> Optimize Size of AST Representation— Simpler AST— AST Compression
> Beyond Text— Store only AST (no text)— Build text from annotated AST
![Page 41: Lecture: Reflection](https://reader034.vdocument.in/reader034/viewer/2022052618/554f9820b4c9057b298b45b4/html5/thumbnails/41.jpg)
© Marcus Denker
License
> http://creativecommons.org/licenses/by-sa/2.5/
Attribution-ShareAlike 2.5You are free:• to copy, distribute, display, and perform the work• to make derivative works• to make commercial use of the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.
• For any reuse or distribution, you must make clear to others the license terms of this work.• Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.