(very)

short introduction into
scala reflection
by david pichsenmeister
Agenda
● What is Reflection?
● Scala reflection Universe
● Mirrors (information providers)
● Symbols
● Some Examples
What is reflection?
Reflection is the ability to inspect, and possibly
even modify itself at runtime. [1]
1. Runtime refle...
(the) Universe
entry point to Scala’s reflection
provides an interface to main types:
Types
Symbols (definitions)
Trees (a...
(the) Universe
imports
Mirrors
information provided by reflection accessible
through mirrors
different flavors of mirrors
Classloader Mirrors
Inv...
Types of Mirrors & their usecases
ReflectiveMirror
loading symbols by name
entry point to invoker mirrors
InstanceMirror
c...
Types of Mirrors & their usecases
ClassMirror
creating invoker mirrors for constructors
ModuleMirror
getting singleton ins...
Symbols
bindings between a name and the entity it
refers to
TypeSymbol
type, class, trait declarations
e.g. ClassSymbol
Te...
Examples
val mirror: RuntimeMirror = universe.runtimeMirror(getClass.getClassLoader)
> res0: reflect.runtime.universe.Mirr...
Examples
invoking a constructor
Examples
invoking a class instance

invoking an object instance
Examples
reflecting a method

alternative:
THANKS!
Upcoming SlideShare
Loading in...5
×

Scala reflection

3,458

Published on

a short introduction into scala's reflection api

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

No Downloads
Views
Total Views
3,458
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
9
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Scala reflection"

  1. 1. (very) short introduction into scala reflection by david pichsenmeister
  2. 2. Agenda ● What is Reflection? ● Scala reflection Universe ● Mirrors (information providers) ● Symbols ● Some Examples
  3. 3. What is reflection? Reflection is the ability to inspect, and possibly even modify itself at runtime. [1] 1. Runtime reflection 2. Compile-time reflection 3. Reification introduced in Scala 2.10 [1]http://docs.scala-lang.org/overviews/reflection/overview.html
  4. 4. (the) Universe entry point to Scala’s reflection provides an interface to main types: Types Symbols (definitions) Trees (abstract syntax trees) Names (terms and type names) Annotations Constants Positions (source position of tree nodes) FlagSet (sets of flags that apply to symbol and definition trees)
  5. 5. (the) Universe imports
  6. 6. Mirrors information provided by reflection accessible through mirrors different flavors of mirrors Classloader Mirrors Invoker Mirrors obtaining special Invoker Mirrors from Classloader Mirror
  7. 7. Types of Mirrors & their usecases ReflectiveMirror loading symbols by name entry point to invoker mirrors InstanceMirror creating Invoker Mirrors MethodMirrors invoking instance methods
  8. 8. Types of Mirrors & their usecases ClassMirror creating invoker mirrors for constructors ModuleMirror getting singleton instances of objects FieldMirror getting/settings instance fields
  9. 9. Symbols bindings between a name and the entity it refers to TypeSymbol type, class, trait declarations e.g. ClassSymbol TermSymbol val, var, def, object declarations e.g. MethodSymbol, ModuleSymbol
  10. 10. Examples val mirror: RuntimeMirror = universe.runtimeMirror(getClass.getClassLoader) > res0: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.n… class Reflect(x: Int) val clz: ClassSymbol = typeOf[Reflect].typeSymbol.asClass > res1: reflect.runtime.universe.ClassSymbol = class Reflect def getTypeTag[T: universe.TypeTag](obj: T) = universe.typeTag[T] val r = new Reflect(4) val clz: ClassSymbol = getTypeTag( r).tpe.typeSymbol.asClass > res2: reflect.runtime.universe.ClassSymbol = class Reflect val cm: ClassMirror = mirror.reflectClass( clz) > res3: reflect.runtime.universe.ClassMirror = class mirror for Reflect (bound to null) object C{ def x = 2} val mod: ModuleSymbol = typeOf[C.type].termSymbol.asModule > res4: reflect.runtime.universe.ModuleSymbol = object C val mm: ModuleMirror = mirror.reflectModule( mod) > res5: reflect.runtime.universe.ModuleMirror = module mirror for C (bound to null)
  11. 11. Examples invoking a constructor
  12. 12. Examples invoking a class instance invoking an object instance
  13. 13. Examples reflecting a method alternative:
  14. 14. THANKS!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×