summaryrefslogtreecommitdiff
path: root/test/files/run/reflection-sanitychecks.scala
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-08-06 19:37:07 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-08-06 23:09:33 +0200
commit7bcb9da47362ba862a695f7c82c0095a8205e3e2 (patch)
treef3434a982cb3104a1abc7f1fd3f4751f5e896da7 /test/files/run/reflection-sanitychecks.scala
parent3cbe07f3e3ddb7201d1d174399d14d4a69df52fd (diff)
downloadscala-7bcb9da47362ba862a695f7c82c0095a8205e3e2.tar.gz
scala-7bcb9da47362ba862a695f7c82c0095a8205e3e2.tar.bz2
scala-7bcb9da47362ba862a695f7c82c0095a8205e3e2.zip
mirrors now support overriden fields and methods
Previously `checkMemberOf` was blocking base fields and methods that are overriden in receiver.getClass. Now this is fixed. The fix also uncovered an issue with field mirrors. Currently their `get` and `set` methods don't respect overriding and always return field values from a base class. After discussing this on a reflection meeting, we decided that this behavior is desirable and that for overriding people should use reflectMethod and then apply on getters/setters. See the discussion at: https://github.com/scala/scala/pull/1054.
Diffstat (limited to 'test/files/run/reflection-sanitychecks.scala')
-rw-r--r--test/files/run/reflection-sanitychecks.scala35
1 files changed, 25 insertions, 10 deletions
diff --git a/test/files/run/reflection-sanitychecks.scala b/test/files/run/reflection-sanitychecks.scala
index b0982fc2fc..f817f23731 100644
--- a/test/files/run/reflection-sanitychecks.scala
+++ b/test/files/run/reflection-sanitychecks.scala
@@ -1,34 +1,49 @@
class C {
- val foo = 1
- def bar = 2
+ val foo = 11
+ def bar = 12
+ val quux = 13
+ def baz = 14
class C { override def toString = "CC" }
object O { override def toString = "CO" }
override def toString = "an instance of class C"
}
-class D {
- val foo = 3
- def bar = 4
- class C { override def toString = "DC" }
- object O { override def toString = "DO" }
+class D extends C {
+ override val foo = 21
+ override def bar = 22
override def toString = "an instance of class D"
}
+class E {
+ val foo = 31
+ def bar = 32
+ val quux = 33
+ def baz = 34
+ class C { override def toString = "EC" }
+ object O { override def toString = "EO" }
+ override def toString = "an instance of class E"
+}
+
object Test extends App {
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}
- val im = cm.reflect(new C)
+ val im = cm.reflect(new D)
def test(tpe: Type): Unit = {
def failsafe(action: => Any): Any = try action catch { case ex: Throwable => ex.toString }
- println("field: " + failsafe(im.reflectField(tpe.member(newTermName("foo")).asTerm).get))
- println("method: " + failsafe(im.reflectMethod(tpe.member(newTermName("bar")).asMethod)()))
+ println(s"=========members of ${tpe.typeSymbol.name} in a mirror of D=========")
+ println("field #1: " + failsafe(im.reflectField(tpe.member(newTermName("foo")).asTerm).get))
+ println("method #1: " + failsafe(im.reflectMethod(tpe.member(newTermName("bar")).asMethod)()))
+ println("field #2: " + failsafe(im.reflectField(tpe.member(newTermName("quux")).asTerm).get))
+ println("method #2: " + failsafe(im.reflectMethod(tpe.member(newTermName("baz")).asMethod)()))
println("constructor #1: " + failsafe(cm.reflectClass(im.symbol).reflectConstructor(tpe.member(newTermName("bar")).asMethod)()))
println("constructor #2: " + failsafe(cm.reflectClass(im.symbol).reflectConstructor(tpe.member(newTermName("<init>")).asMethod)()))
println("class: " + failsafe(im.reflectClass(tpe.member(newTypeName("C")).asClass).reflectConstructor(typeOf[C].member(newTypeName("C")).asClass.typeSignature.member(newTermName("<init>")).asMethod)()))
println("object: " + failsafe(im.reflectModule(tpe.member(newTermName("O")).asModule).instance))
+ println()
}
test(typeOf[C])
test(typeOf[D])
+ test(typeOf[E])
} \ No newline at end of file