summaryrefslogtreecommitdiff
path: root/test/files/run/reflection-sanitychecks.scala
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-07-17 17:54:41 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-07-19 21:58:57 +0200
commit911bbc4fdd889ea8a880b4ae47a490f64c54a2a9 (patch)
treef0b937a8f0eb5ee6da027ba100e4ec207a85009a /test/files/run/reflection-sanitychecks.scala
parentbab827a5426aeb654006573712eb7aabc047db36 (diff)
downloadscala-911bbc4fdd889ea8a880b4ae47a490f64c54a2a9.tar.gz
scala-911bbc4fdd889ea8a880b4ae47a490f64c54a2a9.tar.bz2
scala-911bbc4fdd889ea8a880b4ae47a490f64c54a2a9.zip
SI-5984 improves error reporting in JavaMirrors
Factors out error raising code and introduces a special exception class for Scala reflection errors. Also adds membership sanity checks to reflectXXX. Previously reflectField, reflectMethod, reflectClass and reflectModule in InstanceMirror didn't check that the symbols being passed to them actually correspond to some member of the related class.
Diffstat (limited to 'test/files/run/reflection-sanitychecks.scala')
-rw-r--r--test/files/run/reflection-sanitychecks.scala30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/files/run/reflection-sanitychecks.scala b/test/files/run/reflection-sanitychecks.scala
new file mode 100644
index 0000000000..a6a24088a4
--- /dev/null
+++ b/test/files/run/reflection-sanitychecks.scala
@@ -0,0 +1,30 @@
+class C {
+ val foo = 1
+ def bar = 2
+ class C { override def toString = "CC" }
+ object O { override def toString = "CO" }
+}
+
+class D {
+ val foo = 3
+ def bar = 4
+ class C { override def toString = "DC" }
+ object O { override def toString = "DO" }
+}
+
+object Test extends App {
+ import scala.reflect.runtime.universe._
+ import scala.reflect.runtime.{currentMirror => cm}
+ val im = cm.reflect(new C)
+
+ 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")).asTermSymbol).get))
+ println("method: " + failsafe(im.reflectMethod(tpe.member(newTermName("bar")).asMethodSymbol)()))
+ println("class: " + failsafe(im.reflectClass(tpe.member(newTypeName("C")).asClassSymbol).reflectConstructor(typeOf[C].member(newTypeName("C")).asClassSymbol.typeSignature.member(newTermName("<init>")).asMethodSymbol)()))
+ println("object: " + failsafe(im.reflectModule(tpe.member(newTermName("O")).asModuleSymbol).instance))
+ }
+
+ test(typeOf[C])
+ test(typeOf[D])
+} \ No newline at end of file