summaryrefslogtreecommitdiff
path: root/test/files/run/reflection-enclosed-inner-basic.scala
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-07-11 16:18:47 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-08-08 09:40:36 +0200
commit81535ce5b9acadb79dc12a662519a90e3ad319d8 (patch)
tree2c37f4cd5de8408b75b102596788f2e9107668e8 /test/files/run/reflection-enclosed-inner-basic.scala
parente8f1a423d1d02c488ccd8e9940c55c2d9859cf42 (diff)
downloadscala-81535ce5b9acadb79dc12a662519a90e3ad319d8.tar.gz
scala-81535ce5b9acadb79dc12a662519a90e3ad319d8.tar.bz2
scala-81535ce5b9acadb79dc12a662519a90e3ad319d8.zip
SI-5947 works around getDeclaredClasses
Our name mangling scheme w.r.t stuff nested into objects conflicts with JVM's ideas of beauty, which messes up getDeclaredClasses. Scala reflection needs getDeclaredClasses to convert between Scala and Java, so the situation looked grim. Greg suggested a workaround described in: https://issues.scala-lang.org/browse/SI-4023?focusedCommentId=54759#comment-54759. Luckily the workaround worked!
Diffstat (limited to 'test/files/run/reflection-enclosed-inner-basic.scala')
-rw-r--r--test/files/run/reflection-enclosed-inner-basic.scala52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/files/run/reflection-enclosed-inner-basic.scala b/test/files/run/reflection-enclosed-inner-basic.scala
new file mode 100644
index 0000000000..2b2c701993
--- /dev/null
+++ b/test/files/run/reflection-enclosed-inner-basic.scala
@@ -0,0 +1,52 @@
+import scala.reflect.runtime.universe._
+import scala.reflect.runtime.{currentMirror => cm}
+import scala.reflect.{classTag, ClassTag}
+
+class B {
+ class B1 { override def toString = "B1"; def foo = 1 }
+ private class B2 { override def toString = "B2"; def foo = 2 }
+ object B3 { override def toString = "B3"; def foo = 3 }
+ private object B4 { override def toString = "B4"; def foo = 4 }
+ object B5 extends B1 { override def toString = "B5"; override def foo = 5 }
+ private object B6 extends B2 { override def toString = "B6"; override def foo = 6 }
+}
+
+object Test extends App {
+ val b = cm.classSymbol(classTag[B].runtimeClass)
+ println(b)
+ println(b.typeSignature.declarations.toList)
+
+ def testMethodInvocation(instance: Any) = {
+ val instanceMirror = cm.reflect(instance)
+ val method = instanceMirror.symbol.typeSignature.declaration(newTermName("foo")).asMethod
+ val methodMirror = instanceMirror.reflectMethod(method)
+ println(methodMirror())
+ }
+
+ def testInnerClass(name: String) = {
+ val sym = b.typeSignature.declaration(newTypeName(name)).asClass
+ println(sym)
+ val ctor = sym.typeSignature.declaration(newTermName("<init>")).asMethod
+ val ctorMirror = cm.reflect(new B).reflectClass(sym).reflectConstructor(ctor)
+ val instance = ctorMirror()
+ println(instance)
+ testMethodInvocation(instance)
+ }
+
+ testInnerClass("B1")
+ testInnerClass("B2")
+
+ def testInnerModule(name: String) = {
+ val sym = b.typeSignature.declaration(newTermName(name)).asModule
+ println(sym)
+ val moduleMirror = cm.reflect(new B).reflectModule(sym)
+ val instance = moduleMirror.instance
+ println(instance)
+ testMethodInvocation(instance)
+ }
+
+ testInnerModule("B3")
+ testInnerModule("B4")
+ testInnerModule("B5")
+ testInnerModule("B6")
+}