From b578059b43a4caaadee2cb20f74cd9a7c876c8ef Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 31 Jul 2012 10:50:54 +0200 Subject: SI-5888 Mirrors now have sane toStrings Adds informative toString for InstanceMirror, FieldMirror, MethodMirror (for methods and constructors), ClassMirror and ModuleMirror. Universe mirrors (e.g. JavaMirrors or compiler mirrors) already have good toString methods that show their affiliation and/or classpaths. --- .../files/run/reflection-allmirrors-tostring.check | 14 +++++++ .../files/run/reflection-allmirrors-tostring.scala | 43 ++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 test/files/run/reflection-allmirrors-tostring.check create mode 100644 test/files/run/reflection-allmirrors-tostring.scala (limited to 'test/files/run') diff --git a/test/files/run/reflection-allmirrors-tostring.check b/test/files/run/reflection-allmirrors-tostring.check new file mode 100644 index 0000000000..b5fe6c33bb --- /dev/null +++ b/test/files/run/reflection-allmirrors-tostring.check @@ -0,0 +1,14 @@ +class mirror for C (bound to null) +module mirror for M (bound to null) +instance mirror for an instance of C +field mirror for C.f1 (bound to an instance of C) +field mirror for C.f2 (bound to an instance of C) +method mirror for C.m1: Int (bound to an instance of C) +method mirror for C.m2(): Int (bound to an instance of C) +method mirror for C.m3[T >: String <: Int]: T (bound to an instance of C) +method mirror for C.m4[A, B <: A[Int]](x: A[B])(implicit y: Int): Nothing (bound to an instance of C) +method mirror for C.m5(x: => Int, y: Int*): String (bound to an instance of C) +class mirror for C.C (bound to an instance of C) +module mirror for C.M (bound to an instance of C) +constructor mirror for C.(): C (bound to null) +constructor mirror for C.C.(): C.this.C (bound to an instance of C) diff --git a/test/files/run/reflection-allmirrors-tostring.scala b/test/files/run/reflection-allmirrors-tostring.scala new file mode 100644 index 0000000000..73afff291c --- /dev/null +++ b/test/files/run/reflection-allmirrors-tostring.scala @@ -0,0 +1,43 @@ +import scala.reflect.runtime.universe._ + +class C { + val f1 = 2 + var f2 = 3 + + def m1 = 4 + def m2() = 5 + def m3[T >: String <: Int]: T = ??? + def m4[A[_], B <: A[Int]](x: A[B])(implicit y: Int) = ??? + def m5(x: => Int, y: Int*): String = ??? + + class C + object M + + override def toString = "an instance of C" +} +object M + +object Test extends App { + val cm = scala.reflect.runtime.currentMirror +// println(cm) + + println(cm.reflectClass(cm.staticClass("C"))) + println(cm.reflectModule(cm.staticModule("M"))) + println(cm.reflect(new C)) + + val im = cm.reflect(new C) + println(im.reflectField(typeOf[C].member(newTermName("f1")).asTerm)) + println(im.reflectField(typeOf[C].member(newTermName("f2")).asTerm)) + println(im.reflectMethod(typeOf[C].member(newTermName("m1")).asMethod)) + println(im.reflectMethod(typeOf[C].member(newTermName("m2")).asMethod)) + println(im.reflectMethod(typeOf[C].member(newTermName("m3")).asMethod)) + println(im.reflectMethod(typeOf[C].member(newTermName("m4")).asMethod)) + println(im.reflectMethod(typeOf[C].member(newTermName("m5")).asMethod)) + println(im.reflectClass(typeOf[C].member(newTypeName("C")).asClass)) + println(im.reflectModule(typeOf[C].member(newTermName("M")).asModule)) + + val c = cm.staticClass("C") + val cc = typeOf[C].member(newTypeName("C")).asClass + println(cm.reflectClass(c).reflectConstructor(c.typeSignature.member(nme.CONSTRUCTOR).asMethod)) + println(im.reflectClass(cc).reflectConstructor(cc.typeSignature.member(nme.CONSTRUCTOR).asMethod)) +} \ No newline at end of file -- cgit v1.2.3