Reflection




  Marcus Denker
  denker@acm.org




© Marcus Denker
Roadmap




> Introduction: Reflection
> I. Sub-Method Structural Reflection
> II. Partial Behavioral Reflection




© Marcus...
Roadmap




> Introduction: Reflection
> I. Sub-Method Structural Reflection
> II. Partial Behavioral Reflection




© Marcus...
Reflection




© Marcus Denker
Reflection




              Program   Description




© Marcus Denker
Reflection




                  Program                  Description




                            Query and Change


© ...
Why?

© Marcus Denker
The Systems of the future...




>    ... are getting larger and more complex

>    ... are getting more and more dependen...
Examples of New Demands



>    Dynamic Analysis
     — Fine-grained selection
     — Install / retract at runtime
     — ...
System




Definition:

A computational system is a computer-based system
whose purpose is to answer questions and/or suppo...
Causally Connected




Definition:

A system is said to be causally connected to its domain if
the internal structures and ...
Reflective System




Definition:

A reflective system is a system which incorporates causally
connected structures represent...
Introspection



>    Introspection
     — Self-representation can be queried

>    Intercession
     — Self-representatio...
Structure and Behavior




>    Structural Reflection
     — Concerned with static structure
     — For example: packages, ...
Tower of Interpreters
 Tower of Interpreters



> > First studied forfor
      First studied
     procedural languages
   ...
Reflection and OOP


>    A good match: self-representation build of objects
     — Better then interpreter data-structures...
Example: Java


>    Structural introspection
     — java.lang.reflect
     — Query a model of the program (classes, protoc...
Example: Squeak



>    Squeak has support for reflection

>    Structural reflection
     — Classes / methods are objects
 ...
Can we do better?


>    Structural Reflection stops at method level
     — Bytecode in the CompiledMethod: Numbers
     — ...
Roadmap




> Introduction: Reflection
> I. Sub-Method Structural Reflection
> II. Partial Behavioral Reflection




© Marcus...
Structural Reflection



>    Structure modeled as objects

     — e.g. Classes, methods
     — Causally connected

>    Us...
Methods and Reflection



>    Method are Objects
     — e.g in Smalltalk


>    No high-level model for sub-method element...
Sub-Method Reflection




>    Many tools work on sub method level
     — Profiler, Refactoring Tool, Debugger, Type Checker...
Existing Method Representations




>    Existing representations for Methods

     — Text

     — Bytecode

     — AST


...
Requirements


>    Causal Connection

>    Abstraction Level

>    Extensibility

>    Persistency

>    Size and Perform...
Text




>    Low level abstraction
     — String of characters


>    Not causally connected
     — Need to call compiler...
Bytecode



>    Low level abstraction
     — Array of Integers


>    Missing extensibility
     — e.g. for tools

>    M...
Abstract Syntax Tree



>    Not causally connected
     — Need to call compiler


>    Not extensible
     — Fixed set of...
Solution: Reflective Methods


> Annotated, persistent AST
> Bytecode generated on demand and cached


    :ReflectiveMethod...
Persephone



>    Implementation of Reflective Methods for Squeak

>    Smalltalk compiler generates Reflective Methods
   ...
Requirements revisited


>    Abstraction Level OK

>    Causal Connection OK

>    Extensibility OK

>    Persistency OK
...
Annotations


>    Source visible annotations
     — extended Smalltalk syntax

                  (9 raisedTo: 10000) <:ev...
Example: Pluggable Type-System


>    Example for textual annotations


      bitFromBoolean: aBoolean <:type: Boolean :>
...
Memory


                    number of classes   memory


Squeak 3.9          2040                15.7 MB

Persephone     ...
Roadmap




> Introduction: Reflection in Squeak
> I. Sub-Method Structural Reflection
> II. Partial Behavioral Reflection


...
Behavioral Reflection


>    Reflect on the execution
     — method execution
     — message sending, variable access

>    ...
MetaclassTalk


>    Extends the Smalltalk metaclass model
     — Similar to CLOS MOP


>    Metaclass defines
     — messa...
Reflex: Partial Behavioral Reflection



> Hooksets: collection of operation occurrences
> Links
     — Bind hooksets to met...
Example: Profiler



>    Operation:
     — Method execution (around)


>    Hookset:
     — All execution operations in a ...
Reflex for Squeak




>    Partial Behavioral Reflection pioneered in Java
     — Code transformation at load time
     — No...
Problems


>    Annotation performance
     — Decompile bytecode


>    Execution performance
     — Preambles for stack m...
Links as Annotations


>    Links can be annotations on the AST

            Method
                                      ...
Properties



>    Very fast annotations
     — No decompile!

>    On-the-fly code generation
     — Only code executed ge...
Demo




>    Show Bounce Demo




© Marcus Denker
Reflectivity




>    Prototype implementation in Squeak

     — Sub-Method Structure
     — Partial Behavioral Reflection

...
Questions

                  ?
© Marcus Denker
License

>    http://creativecommons.org/licenses/by-sa/2.5/



                                           Attribution-Sha...
Upcoming SlideShare
Loading in …5
×

Lecture: Reflection

3,030 views
2,869 views

Published on

Lecture: "Sub-Method Structural and Behavioral Reflection", DCC University of Chile, 22.09.2009

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,030
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Lecture: Reflection

  1. 1. Reflection Marcus Denker denker@acm.org © Marcus Denker
  2. 2. Roadmap > Introduction: Reflection > I. Sub-Method Structural Reflection > II. Partial Behavioral Reflection © Marcus Denker
  3. 3. Roadmap > Introduction: Reflection > I. Sub-Method Structural Reflection > II. Partial Behavioral Reflection © Marcus Denker
  4. 4. Reflection © Marcus Denker
  5. 5. Reflection Program Description © Marcus Denker
  6. 6. Reflection Program Description Query and Change © Marcus Denker
  7. 7. Why? © Marcus Denker
  8. 8. The Systems of the future... > ... are getting larger and more complex > ... are getting more and more dependent on each other > The demands are changing © Marcus Denker
  9. 9. Examples of New Demands > Dynamic Analysis — Fine-grained selection — Install / retract at runtime — Complete system > Development Environment — Complete representation of the system — Extensible © Marcus Denker
  10. 10. 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) © Marcus Denker
  11. 11. 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) © Marcus Denker
  12. 12. Reflective System Definition: A reflective system is a system which incorporates causally connected structures representing (aspects of) itself. (Patty Maes, OOPSLA 87) © Marcus Denker
  13. 13. Introspection > Introspection — Self-representation can be queried > Intercession — Self-representation can be changed Reflection = Introspection + Intercession © Marcus Denker
  14. 14. 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 © Marcus Denker
  15. 15. Tower of Interpreters Tower of Interpreters > > First studied forfor First studied procedural languages procedural languages > > David A. Smith: 3Lisp David A. Smith: 3Lisp Interpreter at level 3 Interpreter at level 2 > > Tower-of-Interpreters Tower-of-Interpreters Interpreter at level 1 > > Theoretical. Slow! Theoretical. Slow! User Program running a level 0 © Marcus Denker © Marcus Denker
  16. 16. 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 © Marcus Denker
  17. 17. 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 © Marcus Denker
  18. 18. 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 © Marcus Denker
  19. 19. 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! © Marcus Denker
  20. 20. Roadmap > Introduction: Reflection > I. Sub-Method Structural Reflection > II. Partial Behavioral Reflection © Marcus Denker
  21. 21. Structural Reflection > Structure modeled as objects — e.g. Classes, methods — Causally connected > Uses: — Development environments — Language extensions and experiments © Marcus Denker
  22. 22. 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 © Marcus Denker
  23. 23. 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 © Marcus Denker
  24. 24. Existing Method Representations > Existing representations for Methods — Text — Bytecode — AST © Marcus Denker
  25. 25. Requirements > Causal Connection > Abstraction Level > Extensibility > Persistency > Size and Performance © Marcus Denker
  26. 26. Text > Low level abstraction — String of characters > Not causally connected — Need to call compiler © Marcus Denker
  27. 27. 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 © Marcus Denker
  28. 28. 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 © Marcus Denker
  29. 29. Solution: Reflective Methods > Annotated, persistent AST > Bytecode generated on demand and cached :ReflectiveMethod :CompiledMethod annotation compiledMethod #(12 13 45 38 98 128 annotation reflectiveMethod 84 72 42 77 22 28 59 32 7 49 51 87 64) Tools VM © Marcus Denker
  30. 30. 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 © Marcus Denker
  31. 31. Requirements revisited > Abstraction Level OK > Causal Connection OK > Extensibility OK > Persistency OK > Size and Performance OK © Marcus Denker
  32. 32. Annotations > Source visible annotations — extended Smalltalk syntax (9 raisedTo: 10000) <:evaluateAtCompiletime:> > Source invisible annotations — Reflective API — Can reference any object > Every node can be annotated > Semantics: Compiler Plugins © Marcus Denker
  33. 33. 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 © Marcus Denker
  34. 34. Memory number of classes memory Squeak 3.9 2040 15.7 MB Persephone 2224 20 MB no reflective methods Persephone 2224 123 MB reflective methods © Marcus Denker
  35. 35. Roadmap > Introduction: Reflection in Squeak > I. Sub-Method Structural Reflection > II. Partial Behavioral Reflection © Marcus Denker
  36. 36. 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! © Marcus Denker
  37. 37. 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 © Marcus Denker
  38. 38. Reflex: Partial Behavioral Reflection > Hooksets: collection of operation occurrences > Links — Bind hooksets to meta-objects — Define protocol between base and meta metaobject > Goals — Highly selective reification links activation condition — Flexible meta-level engineering – Protocol specification hookset – Cross-cutting hooksets Tanter, OOPSLA03 © Marcus Denker
  39. 39. Example: Profiler > Operation: — Method execution (around) > Hookset: — All execution operations in a package metaobject > Meta-object: — A profiling tool links activation condition hookset © Marcus Denker
  40. 40. 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 © Marcus Denker
  41. 41. Problems > Annotation performance — Decompile bytecode > Execution performance — Preambles for stack manipulation > Low-level representation — ifTrue:ifFalse: — Blocks — Global variables © Marcus Denker
  42. 42. Links as Annotations > Links can be annotations on the AST Method Meta Link © Marcus Denker
  43. 43. 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 © Marcus Denker
  44. 44. Demo > Show Bounce Demo © Marcus Denker
  45. 45. Reflectivity > Prototype implementation in Squeak — Sub-Method Structure — Partial Behavioral Reflection > Download: http:/scg.unibe.ch/Research/Reflectivity © Marcus Denker
  46. 46. Questions ? © Marcus Denker
  47. 47. License > http://creativecommons.org/licenses/by-sa/2.5/ Attribution-ShareAlike 2.5 You 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. © Marcus Denker

×