• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Scala reflection

Scala reflection



a short introduction into scala's reflection api

a short introduction into scala's reflection api



Total Views
Views on SlideShare
Embed Views



2 Embeds 296

http://www.pichsenmeister.com 237
http://localhost 59



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Scala reflection Scala reflection Presentation Transcript

    • (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 reflection 2. Compile-time reflection 3. Reification introduced in Scala 2.10 [1]http://docs.scala-lang.org/overviews/reflection/overview.html
    • (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)
    • (the) Universe imports
    • Mirrors information provided by reflection accessible through mirrors different flavors of mirrors Classloader Mirrors Invoker Mirrors obtaining special Invoker Mirrors from Classloader Mirror
    • Types of Mirrors & their usecases ReflectiveMirror loading symbols by name entry point to invoker mirrors InstanceMirror creating Invoker Mirrors MethodMirrors invoking instance methods
    • Types of Mirrors & their usecases ClassMirror creating invoker mirrors for constructors ModuleMirror getting singleton instances of objects FieldMirror getting/settings instance fields
    • 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
    • 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)
    • Examples invoking a constructor
    • Examples invoking a class instance invoking an object instance
    • Examples reflecting a method alternative:
    • THANKS!