summaryrefslogtreecommitdiff
path: root/src/reflect
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-06-30 03:37:17 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-07-02 11:48:09 +0200
commit3237b1cd6fc2b2029080fe9d4b2b76d43ab8011c (patch)
tree7583751eced61efe1e02686e2f068409ce07ed38 /src/reflect
parentcba0cbb892e991a0946270d1a7da63993f248225 (diff)
downloadscala-3237b1cd6fc2b2029080fe9d4b2b76d43ab8011c.tar.gz
scala-3237b1cd6fc2b2029080fe9d4b2b76d43ab8011c.tar.bz2
scala-3237b1cd6fc2b2029080fe9d4b2b76d43ab8011c.zip
further improves reflection printers
Diffstat (limited to 'src/reflect')
-rw-r--r--src/reflect/scala/reflect/api/Printers.scala39
-rw-r--r--src/reflect/scala/reflect/internal/Printers.scala75
-rw-r--r--src/reflect/scala/reflect/runtime/ReflectionUtils.scala15
3 files changed, 79 insertions, 50 deletions
diff --git a/src/reflect/scala/reflect/api/Printers.scala b/src/reflect/scala/reflect/api/Printers.scala
index 7f4ff8a7fb..27d3b8ba7d 100644
--- a/src/reflect/scala/reflect/api/Printers.scala
+++ b/src/reflect/scala/reflect/api/Printers.scala
@@ -10,12 +10,15 @@ trait Printers { self: Universe =>
protected var printTypes = false
protected var printIds = false
protected var printKinds = false
+ protected var printMirrors = false
def withTypes: this.type = { printTypes = true; this }
def withoutTypes: this.type = { printTypes = false; this }
def withIds: this.type = { printIds = true; this }
def withoutIds: this.type = { printIds = false; this }
def withKinds: this.type = { printKinds = true; this }
def withoutKinds: this.type = { printKinds = false; this }
+ def withMirrors: this.type = { printMirrors = true; this }
+ def withoutMirrors: this.type = { printMirrors = false; this }
}
case class BooleanFlag(val value: Option[Boolean])
@@ -25,13 +28,14 @@ trait Printers { self: Universe =>
implicit def optionToBooleanFlag(value: Option[Boolean]): BooleanFlag = BooleanFlag(value)
}
- protected def render(what: Any, mkPrinter: PrintWriter => TreePrinter, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None): String = {
+ protected def render(what: Any, mkPrinter: PrintWriter => TreePrinter, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None, printMirrors: BooleanFlag = None): String = {
val buffer = new StringWriter()
val writer = new PrintWriter(buffer)
var printer = mkPrinter(writer)
printTypes.value.map(printTypes => if (printTypes) printer.withTypes else printer.withoutTypes)
printIds.value.map(printIds => if (printIds) printer.withIds else printer.withoutIds)
printKinds.value.map(printKinds => if (printKinds) printer.withKinds else printer.withoutKinds)
+ printMirrors.value.map(printMirrors => if (printMirrors) printer.withMirrors else printer.withoutMirrors)
printer.print(what)
writer.flush()
buffer.toString
@@ -40,42 +44,25 @@ trait Printers { self: Universe =>
/** By default trees are printed with `show` */
override protected def treeToString(tree: Tree) = show(tree)
- /** Renders a prettified representation of a tree.
+ /** Renders a prettified representation of a reflection artifact.
* Typically it looks very close to the Scala code it represents.
- * This function is used in Tree.toString.
*/
- def show(tree: Tree, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None): String =
- render(tree, newTreePrinter(_), printTypes, printIds, printKinds)
+ def show(any: Any, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None, printMirrors: BooleanFlag = None): String =
+ render(any, newTreePrinter(_), printTypes, printIds, printKinds, printMirrors)
- /** Hook to define what `show(tree)` means.
+ /** Hook to define what `show(...)` means.
*/
def newTreePrinter(out: PrintWriter): TreePrinter
- /** Renders internal structure of a tree.
+ /** Renders internal structure of a reflection artifact.
*/
- def showRaw(tree: Tree, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None): String =
- render(tree, newRawTreePrinter(_), printTypes, printIds, printKinds)
+ def showRaw(any: Any, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None, printMirrors: BooleanFlag = None): String =
+ render(any, newRawTreePrinter(_), printTypes, printIds, printKinds, printMirrors)
- /** Hook to define what `showRaw(tree)` means.
+ /** Hook to define what `showRaw(...)` means.
*/
def newRawTreePrinter(out: PrintWriter): TreePrinter
- /** Renders a prettified representation of a symbol.
- */
- def show(sym: Symbol): String = sym.toString
-
- /** Renders internal structure of a symbol.
- */
- def showRaw(sym: Symbol): String = render(sym, newRawTreePrinter(_))
-
- /** Renders a prettified representation of a type.
- */
- def show(tpe: Type): String = tpe.toString
-
- /** Renders internal structure of a type.
- */
- def showRaw(tpe: Type): String = render(tpe, newRawTreePrinter(_))
-
/** Renders a prettified representation of a name.
*/
def show(name: Name): String
diff --git a/src/reflect/scala/reflect/internal/Printers.scala b/src/reflect/scala/reflect/internal/Printers.scala
index 82a8c42f7c..c018ddc88e 100644
--- a/src/reflect/scala/reflect/internal/Printers.scala
+++ b/src/reflect/scala/reflect/internal/Printers.scala
@@ -10,6 +10,7 @@ package internal
import java.io.{ OutputStream, PrintWriter, StringWriter, Writer }
import Flags._
+import compat.Platform.EOL
trait Printers extends api.Printers { self: SymbolTable =>
@@ -65,6 +66,7 @@ trait Printers extends api.Printers { self: SymbolTable =>
printTypes = settings.printtypes.value
printIds = settings.uniqid.value
printKinds = settings.Yshowsymkinds.value
+ printMirrors = false // typically there's no point to print mirrors inside the compiler, as there is only one mirror there
protected def doPrintPositions = settings.Xprintpos.value
def indent() = indentMargin += indentStep
@@ -477,22 +479,61 @@ trait Printers extends api.Printers { self: SymbolTable =>
def flush = { /* do nothing */ }
}
- // provides footnotes for types
- private var typeCounter = 0
- private val typeMap = collection.mutable.WeakHashMap[Type, Int]()
-
def newRawTreePrinter(writer: PrintWriter): RawTreePrinter = new RawTreePrinter(writer)
def newRawTreePrinter(stream: OutputStream): RawTreePrinter = newRawTreePrinter(new PrintWriter(stream))
def newRawTreePrinter(): RawTreePrinter = newRawTreePrinter(new PrintWriter(ConsoleWriter))
+ // provides footnotes for types and mirrors
+ import scala.collection.mutable.{Map, WeakHashMap, SortedSet}
+ private val footnoteIndex = new FootnoteIndex
+ private class FootnoteIndex {
+ private val index = Map[Class[_], WeakHashMap[Any, Int]]()
+ private def classIndex[T: ClassTag] = index.getOrElseUpdate(classTag[T].runtimeClass, WeakHashMap[Any, Int]())
+ private val counters = Map[Class[_], Int]()
+ private def nextCounter[T: ClassTag] = {
+ val clazz = classTag[T].runtimeClass
+ counters.getOrElseUpdate(clazz, 0)
+ counters(clazz) = counters(clazz) + 1
+ counters(clazz)
+ }
+
+ def mkFootnotes() = new Footnotes
+ class Footnotes {
+ private val footnotes = Map[Class[_], SortedSet[Int]]()
+ private def classFootnotes[T: ClassTag] = footnotes.getOrElseUpdate(classTag[T].runtimeClass, SortedSet[Int]())
+
+ def put[T: ClassTag](any: T): Int = {
+ val index = classIndex[T].getOrElseUpdate(any, nextCounter[T])
+ classFootnotes[T] += index
+ index
+ }
+
+ def get[T: ClassTag]: List[(Int, Any)] =
+ classFootnotes[T].toList map (fi => (fi, classIndex[T].find{ case (any, ii) => ii == fi }.get._1))
+
+ def print[T: ClassTag](printer: Printers.super.TreePrinter): Unit = {
+ val footnotes = get[T]
+ if (footnotes.nonEmpty) {
+ printer.print(EOL)
+ footnotes.zipWithIndex foreach {
+ case ((fi, any), ii) =>
+ printer.print("[", fi, "] ", any)
+ if (ii < footnotes.length - 1) printer.print(EOL)
+ }
+ }
+ }
+ }
+ }
+
// emits more or less verbatim representation of the provided tree
class RawTreePrinter(out: PrintWriter) extends super.TreePrinter {
private var depth = 0
- private var footnotes = collection.mutable.Map[Int, Type]()
- private var printingFootnotes = false
private var printTypesInFootnotes = true
+ private var printingFootnotes = false
+ private var footnotes = footnoteIndex.mkFootnotes()
def print(args: Any*): Unit = {
+ // don't print type footnotes if the argument is a mere type
if (depth == 0 && args.length == 1 && args(0) != null && args(0).isInstanceOf[Type])
printTypesInFootnotes = false
@@ -544,14 +585,15 @@ trait Printers extends api.Printers { self: SymbolTable =>
else print(sym.name)
if (printIds) print("#", sym.id)
if (printKinds) print("#", sym.abbreviatedKindString)
+ if (printMirrors) print("%M", footnotes.put[MirrorOf[_]](mirrorThatLoaded(sym)))
case NoType =>
print("NoType")
case NoPrefix =>
print("NoPrefix")
- case tpe: Type if printTypesInFootnotes && !printingFootnotes =>
- val index = typeMap.getOrElseUpdate(tpe, { typeCounter += 1; typeCounter })
- footnotes(index) = tpe
- print("[", index, "]")
+ case tpe: Type =>
+ val defer = printTypesInFootnotes && !printingFootnotes
+ if (defer) print("[", footnotes.put(tpe), "]")
+ else printProduct(tpe.asInstanceOf[Product])
case mods: Modifiers =>
print("Modifiers(")
if (mods.flags != NoFlags || mods.privateWithin != tpnme.EMPTY || mods.annotations.nonEmpty) print(show(mods.flags))
@@ -569,16 +611,11 @@ trait Printers extends api.Printers { self: SymbolTable =>
out.print(arg)
}
depth -= 1
- if (depth == 0 && footnotes.nonEmpty && !printingFootnotes) {
+ if (depth == 0 && !printingFootnotes) {
printingFootnotes = true
- out.println()
- val typeIndices = footnotes.keys.toList.sorted
- typeIndices.zipWithIndex foreach {
- case (typeIndex, i) =>
- print("[" + typeIndex + "] ")
- print(footnotes(typeIndex))
- if (i < typeIndices.length - 1) out.println()
- }
+ footnotes.print[Type](this)
+ footnotes.print[MirrorOf[_]](this)
+ printingFootnotes = false
}
}
diff --git a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala
index 4e82fe8ad2..7839850529 100644
--- a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala
+++ b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala
@@ -37,12 +37,17 @@ object ReflectionUtils {
systemProperties find (_._1 endsWith ".boot.class.path") map (_._2) getOrElse ""
)
- def show(cl: ClassLoader) = {
+ def show(cl: ClassLoader): String = {
+ def isAbstractFileClassLoader(clazz: Class[_]): Boolean = {
+ if (clazz == null) return false
+ if (clazz.getName == "scala.tools.nsc.interpreter.AbstractFileClassLoader") return true
+ return isAbstractFileClassLoader(clazz.getSuperclass)
+ }
def inferClasspath(cl: ClassLoader): String = cl match {
case cl: java.net.URLClassLoader =>
- "[" + (cl.getURLs mkString ",") + "]"
- case cl if cl != null && cl.getClass.getName == "scala.tools.nsc.interpreter.AbstractFileClassLoader" =>
- "[" + cl.asInstanceOf[{val root: scala.reflect.internal.AbstractFileApi}].root + "] and " + inferClasspath(cl.getParent)
+ (cl.getURLs mkString ",")
+ case cl if cl != null && isAbstractFileClassLoader(cl.getClass) =>
+ cl.asInstanceOf[{val root: scala.reflect.internal.AbstractFileApi}].root.canonicalPath
case null =>
inferBootClasspath
case _ =>
@@ -50,7 +55,7 @@ object ReflectionUtils {
}
cl match {
case cl if cl != null =>
- "%s of type %s with classpath %s".format(cl, cl.getClass, inferClasspath(cl))
+ "%s of type %s with classpath [%s] and parent being %s".format(cl, cl.getClass, inferClasspath(cl), show(cl.getParent))
case null =>
"primordial classloader with boot classpath [%s]".format(inferClasspath(cl))
}