Linguistic Reflection Via Mirrors
Gilad Bracha and David Ungar Sun Microsystems
Copyright Gilad Bracha 2002-2005 Overview
• This work is about how to structure the API for reflection in an object oriented language • Most of this is old work: Self, then Strongtalk, then JDI - but • never properly discussed in the literature; it needs to be
Copyright Gilad Bracha 2002-2005 A Simple Example
There was an old woman who lived in a shoe
Copyright Gilad Bracha 2002-2005 What kind of shoe?
There was an old woman who lived in a shoe
Copyright Gilad Bracha 2002-2005 What kind of shoe?
There was an old woman who lived in a shoe
Sneaker
getClass()
Copyright Gilad Bracha 2002-2005 There was an old woman who lived in a shoe
OldWomanProxy ShoeProxy
Copyright Gilad Bracha 2002-2005 There was an old woman who lived in a shoe
getClass()
OldWomanProxy Proxy ShoeProxy
Copyright Gilad Bracha 2002-2005 There was an old woman who lived in a shoe
getClass() Sneaker OldWomanProxy ShoeProxy Proxy
Sneaker
getClass()
Copyright Gilad Bracha 2002-2005 Encapsulation
• Who your class is is fixed and exposed to your clients via getClass() • We can’t replace the implementation of getClass()
Copyright Gilad Bracha 2002-2005 Stratification
• Confusion between base level and meta level • Ask a base level question: What kind of shoe? • Get a meta level answer: Proxy • Hard to tell what answer is required; depends who’s asking
Copyright Gilad Bracha 2002-2005 Traditional Reflection
Base level Meta level
Sneaker Shoe getSuperclass() getClass()
Copyright Gilad Bracha 2002-2005 Mirrors
Base level Meta level getSuperclass() Sneaker Class Mirror on Sneaker Class Mirror reflect on Shoe getClass() getClassMirror()
an ObjectMirror reflect
Copyright Gilad Bracha 2002-2005 There was an old woman who lived in a shoe
getClass() Sneaker OldWomanProxy ShoeProxy Proxy
Sneaker
getClass()
Copyright Gilad Bracha 2002-2005 Mirrors
Base level Meta level getSuperclass() Class Mirror on Sneaker Class Mirror on Shoe
getClassMirror()
an ObjectMirror reflect
Copyright Gilad Bracha 2002-2005 Mirrors & Encapsulation
• Mirrors provide a functional decomposition • Different implementations possible, e.g., local, remote, source based • All can conform to common interface • Subsume core reflection, JDI, javadoc, APT
Copyright Gilad Bracha 2002-2005 Deployment
Base level Meta level getSuperclass() Class Mirror on Sneaker Class Mirror on Shoe
getClassMirror()
an ObjectMirror reflect
Copyright Gilad Bracha 2002-2005 Deployment
Base level Meta level getSuperclass() Class Mirror on Sneaker Class Mirror on Shoe
getClassMirror()
an ObjectMirror reflect
Copyright Gilad Bracha 2002-2005 Deployment Base level
• Easy to deploy without reflection • Useful when security or footprint are an issue • Unlike static solutions, you can ...
Copyright Gilad Bracha 2002-2005 Deployment Base level
• add or remove it dynamically
Copyright Gilad Bracha 2002-2005 Deployment
Base level Meta level getSuperclass() Class Mirror on Sneaker Class Mirror on Shoe
getClassMirror()
an ObjectMirror reflect
Copyright Gilad Bracha 2002-2005 Correspondence
Object ObjectMirror Class ClassMirror Method MethodMirror Stackframe StackframeMirror local variable ?
comment ? program counter ?
Copyright Gilad Bracha 2002-2005 Correspondence
Object ObjectMirror Class ClassMirror Method MethodMirror Stackframe StackframeMirror local variable ?
comment ? program counter ?
Copyright Gilad Bracha 2002-2005 Reflecting Method Bodies
• Traditional reflective APIs do not reflect the internals of a method • Debuggers need this; APIs often provide raw byte code (VML) which clients attempt to decompile • Issues: ability to reconstruct source
Copyright Gilad Bracha 2002-2005 Correspondence
Object ObjectMirror Class ClassMirror Method MethodMirror Stackframe StackframeMirror local variable VariableMirror
comment ? program counter ?
Copyright Gilad Bracha 2002-2005 Code vs. Computation
• The word program is ambiguous • Must distinguish between • Code: A description written in some programming language • Computation: An executing computational process • Code is intentional, declarative, static • Computation is extensional, stateful, dynamic
Copyright Gilad Bracha 2002-2005 Code vs. Computation (2) • An API like JDI is not well suited to writing a class browser that examines source files • Notions like active threads, call stacks, object instantiation assume a computation • Conversely, source code contains things like comments that might not be available at run time • Metaprogramming APIs should model the commonalities and distinctions between code and computation
Copyright Gilad Bracha 2002-2005 Correspondence
Object ObjectMirror Class ClassMirror Method MethodMirror Stackframe StackframeMirror local variable VariableMirror
comment CommentMirror program counter ?
Copyright Gilad Bracha 2002-2005 Which Language to Reflect? • Reflect at source level or VM level? • Implementation requires reflective access to VM • VM may support high-level language(s) with different semantics • Each HLL must have its own reflective API, or semantic discrepancies will arise • Example: Strongtalk separates mirrors into low and high levels
Copyright Gilad Bracha 2002-2005 Example: Nested Classes in Java
• VM does not directly support nested classes • Compiler uses synthetic members as part of translation from HLL (Java) to VML (Java byte code) • Core reflection exposes all members, synthetic or not, exposing translation strategy
Copyright Gilad Bracha 2002-2005 Unifying Reflection and Metaprogramming • If one models the ontology of the programming language, both structurally and temporally, one may be able to unify reflection and metaprogramming • Use consistent framework for compilers, assemblers, debuggers, browsers and for reflection • Example applications: javac, javap/ jdis/jasm/jcod, verifier, JIT.
Copyright Gilad Bracha 2002-2005 The Goal?
• The ideal reflective API: • Corresponds to the language, structurally and temporally • Respects encapsulation and stratification • Supports introspection, self- modification and intercession • Are these goals mutually consistent?
Copyright Gilad Bracha 2002-2005 Related Work
• Self • Strongtalk • JDI • APT • Wirfs-Brock/Smalltalk Firewall • MOP • Class loaders • Templ/Oberon • MetaBeta, Yggdrasil • Lorenz & Vlissides
Copyright Gilad Bracha 2002-2005