scala.reflect.api

Importers

trait Importers extends AnyRef

EXPERIMENTAL

This trait provides support for importers, a facility to migrate reflection artifacts between universes. Note: this trait should typically be used only rarely.

Reflection artifacts, such as Symbols and Types, are contained in Universes. Typically all processing happens within a single Universe (e.g. a compile-time macro Universe or a runtime reflection Universe), but sometimes there is a need to migrate artifacts from one Universe to another. For example, runtime compilation works by importing runtime reflection trees into a runtime compiler universe, compiling the importees and exporting the result back.

Reflection artifacts are firmly grounded in their Universes, which is reflected by the fact that types of artifacts from different universes are not compatible. By using Importers, however, they be imported from one universe into another. For example, to import foo.bar.Baz from the source Universe to the target Universe, an importer will first check whether the entire owner chain exists in the target Universe. If it does, then nothing else will be done. Otherwise, the importer will recreate the entire owner chain and will import the corresponding type signatures into the target Universe.

Since importers match Symbol tables of the source and the target Universes using plain string names, it is programmer's responsibility to make sure that imports don't distort semantics, e.g., that foo.bar.Baz in the source Universe means the same that foo.bar.Baz does in the target Universe.

Example

Here's how one might implement a macro that performs compile-time evaluation of its argument by using a runtime compiler to compile and evaluate a tree that belongs to a compile-time compiler:

def staticEval[T](x: T) = macro staticEval[T]

def staticEval[T](c: scala.reflect.macros.Context)(x: c.Expr[T]) = {
  // creates a runtime reflection universe to host runtime compilation
  import scala.reflect.runtime.{universe => ru}
  val mirror = ru.runtimeMirror(c.libraryClassLoader)
  import scala.tools.reflect.ToolBox
  val toolBox = mirror.mkToolBox()

  // runtime reflection universe and compile-time macro universe are different
  // therefore an importer is needed to bridge them
  // currently mkImporter requires a cast to correctly assign the path-dependent types
  val importer0 = ru.mkImporter(c.universe)
  val importer = importer0.asInstanceOf[ru.Importer { val from: c.universe.type }]

  // the created importer is used to turn a compiler tree into a runtime compiler tree
  // both compilers use the same classpath, so semantics remains intact
  val imported = importer.importTree(tree)

  // after the tree is imported, it can be evaluated as usual
  val tree = toolBox.resetAllAttrs(imported.duplicate)
  val valueOfX = toolBox.eval(imported).asInstanceOf[T]
  ...
}
Self Type
Universe
Source
Importers.scala
Linear Supertypes
AnyRef, Any
Known Subclasses
Type Hierarchy Learn more about scaladoc diagrams
Ordering
  1. Grouped
  2. Alphabetic
  3. By inheritance
Inherited
  1. Importers
  2. AnyRef
  3. Any
Implicitly
  1. by StringAdd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. trait Importer extends AnyRef

    The API of importers.

Abstract Value Members

  1. abstract def mkImporter(from0: Universe): Universe.Importer { val from: from0.type }

    Creates an importer that moves reflection artifacts between universes.

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. def +(other: String): String

    Implicit information
    This member is added by an implicit conversion from Importers to StringAdd[Importers] performed by method StringAdd in scala.Predef.
    Definition Classes
    StringAdd
  5. def ->[B](y: B): (Importers, B)

    Implicit information
    This member is added by an implicit conversion from Importers to ArrowAssoc[Importers] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  6. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  7. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  8. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  9. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  10. def ensuring(cond: (Importers) ⇒ Boolean, msg: ⇒ Any): Importers

    Implicit information
    This member is added by an implicit conversion from Importers to Ensuring[Importers] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  11. def ensuring(cond: (Importers) ⇒ Boolean): Importers

    Implicit information
    This member is added by an implicit conversion from Importers to Ensuring[Importers] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: Boolean, msg: ⇒ Any): Importers

    Implicit information
    This member is added by an implicit conversion from Importers to Ensuring[Importers] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def ensuring(cond: Boolean): Importers

    Implicit information
    This member is added by an implicit conversion from Importers to Ensuring[Importers] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  15. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  16. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  17. def formatted(fmtstr: String): String

    Returns string formatted according to given format string.

    Returns string formatted according to given format string. Format strings are as for String.format (@see java.lang.String.format).

    Implicit information
    This member is added by an implicit conversion from Importers to StringFormat[Importers] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  18. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  19. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  20. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  21. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  22. final def notify(): Unit

    Definition Classes
    AnyRef
  23. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  24. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  25. def toString(): String

    Definition Classes
    AnyRef → Any
  26. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  27. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  28. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  29. def [B](y: B): (Importers, B)

    Implicit information
    This member is added by an implicit conversion from Importers to ArrowAssoc[Importers] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion StringAdd from Importers to StringAdd[Importers]

Inherited by implicit conversion StringFormat from Importers to StringFormat[Importers]

Inherited by implicit conversion Ensuring from Importers to Ensuring[Importers]

Inherited by implicit conversion ArrowAssoc from Importers to ArrowAssoc[Importers]

Importers

Ungrouped