Scala reflection

4,395 views
4,039 views

Published on

a short introduction into scala's reflection api

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

No Downloads
Views
Total views
4,395
On SlideShare
0
From Embeds
0
Number of Embeds
1,405
Actions
Shares
0
Downloads
15
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

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!

×