diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-20 00:32:39 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-20 00:32:39 -0700 |
commit | a8bd1e25b9fe32a8eff2f51ee3bfa527440beb84 (patch) | |
tree | e5ce970714407e6269b4d5a1255bc8947c99a4a4 /test | |
parent | cf2a36315f380391a4bdf9f3f608715b392ccca6 (diff) | |
parent | baf3d1a2516e69660cd9a3d6ea3120327885fe93 (diff) | |
download | scala-a8bd1e25b9fe32a8eff2f51ee3bfa527440beb84.tar.gz scala-a8bd1e25b9fe32a8eff2f51ee3bfa527440beb84.tar.bz2 scala-a8bd1e25b9fe32a8eff2f51ee3bfa527440beb84.zip |
Merge pull request #929 from scalamacros/ticket/5895
fixes field mirrors and also improves docs and exceptions for all mirrors
Diffstat (limited to 'test')
14 files changed, 86 insertions, 39 deletions
diff --git a/test/files/run/reflection-constructormirror-inner-badpath.check b/test/files/run/reflection-constructormirror-inner-badpath.check index 28b936eca1..2fb0610ad6 100644 --- a/test/files/run/reflection-constructormirror-inner-badpath.check +++ b/test/files/run/reflection-constructormirror-inner-badpath.check @@ -1,2 +1,2 @@ -this is an inner class, use reflectClass on an InstanceMirror to obtain its ClassMirror
+class R is an inner class, use reflectClass on an InstanceMirror to obtain its ClassMirror
()
diff --git a/test/files/run/reflection-constructormirror-nested-badpath.check b/test/files/run/reflection-constructormirror-nested-badpath.check index 9ceb603dc2..acd21df9c0 100644 --- a/test/files/run/reflection-constructormirror-nested-badpath.check +++ b/test/files/run/reflection-constructormirror-nested-badpath.check @@ -1,2 +1,2 @@ -this is a static class, use reflectClass on a RuntimeMirror to obtain its ClassMirror
+class R is a static class, use reflectClass on a RuntimeMirror to obtain its ClassMirror
()
diff --git a/test/files/run/reflection-constructormirror-toplevel-badpath.check b/test/files/run/reflection-constructormirror-toplevel-badpath.check index 9ceb603dc2..acd21df9c0 100644 --- a/test/files/run/reflection-constructormirror-toplevel-badpath.check +++ b/test/files/run/reflection-constructormirror-toplevel-badpath.check @@ -1,2 +1,2 @@ -this is a static class, use reflectClass on a RuntimeMirror to obtain its ClassMirror
+class R is a static class, use reflectClass on a RuntimeMirror to obtain its ClassMirror
()
diff --git a/test/files/run/reflection-fieldmirror-accessorsareokay.check b/test/files/run/reflection-fieldmirror-accessorsareokay.check new file mode 100644 index 0000000000..635dcd04ce --- /dev/null +++ b/test/files/run/reflection-fieldmirror-accessorsareokay.check @@ -0,0 +1,6 @@ +true
+42
+2
+true
+2
+2
diff --git a/test/files/run/reflection-fieldmirror-accessorsareokay.scala b/test/files/run/reflection-fieldmirror-accessorsareokay.scala new file mode 100644 index 0000000000..c586ce9bdd --- /dev/null +++ b/test/files/run/reflection-fieldmirror-accessorsareokay.scala @@ -0,0 +1,29 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} + +object Test extends App { + class A { + var x: Int = 42 + } + + val a = new A + + val im: InstanceMirror = cm.reflect(a) + val cs = im.symbol + + def test(f: Symbol) = { + try { + val fm: FieldMirror = im.reflectField(f.asTermSymbol) + println(fm.symbol.isVariable) + println(fm.get) + fm.set(2) + println(fm.get) + } catch { + case ex: Throwable => + println(ex.getMessage) + } + } + + test(cs.typeSignature.declaration(newTermName("x")).asTermSymbol) + test(cs.typeSignature.declaration(newTermName("x_$eq")).asTermSymbol) +} diff --git a/test/files/run/reflection-fieldmirror-ctorparam.check b/test/files/run/reflection-fieldmirror-ctorparam.check index 8b99a6f772..31f6491b14 100644 --- a/test/files/run/reflection-fieldmirror-ctorparam.check +++ b/test/files/run/reflection-fieldmirror-ctorparam.check @@ -1 +1,3 @@ -class java.lang.NoSuchFieldException: Test$A$$x
+class scala.ScalaReflectionException: Scala field x isn't represented as a Java field, neither it has a Java accessor method +note that private parameters of class constructors don't get mapped onto fields and/or accessors, +unless they are used outside of their declaring constructors.
diff --git a/test/files/run/reflection-fieldmirror-getsetval.check b/test/files/run/reflection-fieldmirror-getsetval.check index 707bbcccce..e1927f68d0 100644 --- a/test/files/run/reflection-fieldmirror-getsetval.check +++ b/test/files/run/reflection-fieldmirror-getsetval.check @@ -1,2 +1,2 @@ 42
-cannot set an immutable field
+cannot set an immutable field x
diff --git a/test/files/run/reflection-fieldmirror-sanitycheck.check b/test/files/run/reflection-fieldmirror-sanitycheck.check deleted file mode 100644 index e5134de4e3..0000000000 --- a/test/files/run/reflection-fieldmirror-sanitycheck.check +++ /dev/null @@ -1,6 +0,0 @@ -expected a field symbol, you provided a method symbol -A typical cause of this problem is using a field accessor symbol instead of a field symbol. -To obtain a field symbol append nme.LOCAL_SUFFIX_STRING to the name of the field, -when searching for a member with Type.members or Type.declarations. -This is a temporary inconvenience that will be resolved before 2.10.0-final. -More information can be found here: https://issues.scala-lang.org/browse/SI-5895.
diff --git a/test/files/run/reflection-fieldmirror-sanitycheck.scala b/test/files/run/reflection-fieldmirror-sanitycheck.scala deleted file mode 100644 index 6a992dd282..0000000000 --- a/test/files/run/reflection-fieldmirror-sanitycheck.scala +++ /dev/null @@ -1,22 +0,0 @@ -import scala.reflect.runtime.universe._ -import scala.reflect.runtime.{currentMirror => cm} - -object Test extends App { - class A { - var x: Int = 42 - } - - val a = new A - - val im: InstanceMirror = cm.reflect(a) - val cs = im.symbol - //val f = cs.typeSignature.declaration(newTermName("x" + nme.LOCAL_SUFFIX_STRING)).asTermSymbol - val f = cs.typeSignature.declaration(newTermName("x")).asTermSymbol - try { - val fm: FieldMirror = im.reflectField(f) - println("this indicates a failure") - } catch { - case ex: Throwable => - println(ex.getMessage) - } -} diff --git a/test/files/run/reflection-modulemirror-inner-badpath.check b/test/files/run/reflection-modulemirror-inner-badpath.check index d3fe43336e..1e990ec900 100644 --- a/test/files/run/reflection-modulemirror-inner-badpath.check +++ b/test/files/run/reflection-modulemirror-inner-badpath.check @@ -1,2 +1,2 @@ -this is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror -() +object R is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror
+()
diff --git a/test/files/run/reflection-modulemirror-nested-badpath.check b/test/files/run/reflection-modulemirror-nested-badpath.check index 16a5b10390..f7980b9986 100644 --- a/test/files/run/reflection-modulemirror-nested-badpath.check +++ b/test/files/run/reflection-modulemirror-nested-badpath.check @@ -1,2 +1,2 @@ -this is a static module, use reflectModule on a RuntimeMirror to obtain its ModuleMirror -() +object R is a static module, use reflectModule on a RuntimeMirror to obtain its ModuleMirror
+()
diff --git a/test/files/run/reflection-modulemirror-toplevel-badpath.check b/test/files/run/reflection-modulemirror-toplevel-badpath.check index 16a5b10390..f7980b9986 100644 --- a/test/files/run/reflection-modulemirror-toplevel-badpath.check +++ b/test/files/run/reflection-modulemirror-toplevel-badpath.check @@ -1,2 +1,2 @@ -this is a static module, use reflectModule on a RuntimeMirror to obtain its ModuleMirror -() +object R is a static module, use reflectModule on a RuntimeMirror to obtain its ModuleMirror
+()
diff --git a/test/files/run/reflection-sanitychecks.check b/test/files/run/reflection-sanitychecks.check new file mode 100644 index 0000000000..d977e0ed66 --- /dev/null +++ b/test/files/run/reflection-sanitychecks.check @@ -0,0 +1,8 @@ +field: 1
+method: 2
+class: CC
+object: java.lang.Error: inner and nested modules are not supported yet
+field: scala.ScalaReflectionException: expected a member of class C, you provided value D.foo
+method: scala.ScalaReflectionException: expected a member of class C, you provided method D.bar
+class: scala.ScalaReflectionException: expected a member of class C, you provided class D.C
+object: scala.ScalaReflectionException: expected a member of class C, you provided object D.O
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 |