From 202eb73b6cd6ebb3e20ff9f0a198c4ea83319851 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 28 Jan 2014 12:23:47 +0300 Subject: adds showDeclaration(sym: Symbol): String MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per Paul’s request, this commit exposes Symbol.defString, although in a different way to ensure consistency with our other prettyprinting facilities provided in the reflection API. --- test/files/run/showdecl/Macros_1.scala | 30 ++++++++++++++++++++++++++++++ test/files/run/showdecl/Test_2.scala | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/files/run/showdecl/Macros_1.scala create mode 100644 test/files/run/showdecl/Test_2.scala (limited to 'test/files/run/showdecl') diff --git a/test/files/run/showdecl/Macros_1.scala b/test/files/run/showdecl/Macros_1.scala new file mode 100644 index 0000000000..d0493fb97f --- /dev/null +++ b/test/files/run/showdecl/Macros_1.scala @@ -0,0 +1,30 @@ +import scala.reflect.macros.whitebox._ +import scala.language.experimental.macros + +object Macros { + def impl(c: Context) = { + var messages = List[String]() + def println(msg: String) = messages :+= msg + + import c.universe._ + def test(sym: Symbol): Unit = { + println(s"uninitialized ${sym.name}: ${showDeclaration(sym)}") + sym.typeSignature + println(s"initialized ${sym.name}: ${showDeclaration(sym)}") + } + + println("compile-time") + test(c.mirror.staticClass("D")) + test(c.mirror.staticClass("D").typeSignature.member(TermName("x"))) + test(c.mirror.staticClass("D").typeSignature.member(TermName("y"))) + test(c.mirror.staticClass("D").typeSignature.member(TermName("z"))) + test(c.mirror.staticClass("D").typeSignature.member(TermName("t"))) + test(c.mirror.staticClass("D").typeSignature.member(TypeName("W"))) + test(c.mirror.staticClass("D").typeSignature.member(TypeName("C"))) + test(c.mirror.staticClass("D").typeSignature.member(TermName("O"))) + + q"..${messages.map(msg => q"println($msg)")}" + } + + def foo: Any = macro impl +} \ No newline at end of file diff --git a/test/files/run/showdecl/Test_2.scala b/test/files/run/showdecl/Test_2.scala new file mode 100644 index 0000000000..65ab2f147c --- /dev/null +++ b/test/files/run/showdecl/Test_2.scala @@ -0,0 +1,32 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} + +object Test extends App { + def test(sym: Symbol): Unit = { + println(s"autoinitialized ${sym.name}: ${showDeclaration(sym)}") + sym.typeSignature + println(s"autoinitialized ${sym.name}: ${showDeclaration(sym)}") + } + + Macros.foo + println("runtime") + test(symbolOf[D]) + test(typeOf[D].member(TermName("x"))) + test(typeOf[D].member(TermName("y"))) + test(typeOf[D].member(TermName("z"))) + test(typeOf[D].member(TermName("t"))) + test(typeOf[D].member(TypeName("W"))) + test(typeOf[D].member(TypeName("C"))) + test(typeOf[D].member(TermName("O"))) +} + +class C +class D extends C { + val x = 2 + lazy val y = 3 + var z = 4 + def t[T <: Int](x: D)(y: x.W) = 5 + type W = String + class C extends D + object O extends C +} -- cgit v1.2.3