From 81535ce5b9acadb79dc12a662519a90e3ad319d8 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 11 Jul 2012 16:18:47 +0200 Subject: 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! --- .../reflection-enclosed-nested-nested-basic.scala | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/files/run/reflection-enclosed-nested-nested-basic.scala (limited to 'test/files/run/reflection-enclosed-nested-nested-basic.scala') diff --git a/test/files/run/reflection-enclosed-nested-nested-basic.scala b/test/files/run/reflection-enclosed-nested-nested-basic.scala new file mode 100644 index 0000000000..8335ea482a --- /dev/null +++ b/test/files/run/reflection-enclosed-nested-nested-basic.scala @@ -0,0 +1,54 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.reflect.{classTag, ClassTag} + +object B { + object BB { + 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.moduleSymbol(classTag[B.BB.type].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 testNestedClass(name: String) = { + val sym = b.typeSignature.declaration(newTypeName(name)).asClass + println(sym) + val ctor = sym.typeSignature.declaration(newTermName("")).asMethod + val ctorMirror = cm.reflectClass(sym).reflectConstructor(ctor) + val instance = ctorMirror() + println(instance) + testMethodInvocation(instance) + } + + testNestedClass("B1") + testNestedClass("B2") + + def testNestedModule(name: String) = { + val sym = b.typeSignature.declaration(newTermName(name)).asModule + println(sym) + val moduleMirror = cm.reflectModule(sym) + val instance = moduleMirror.instance + println(instance) + testMethodInvocation(instance) + } + + testNestedModule("B3") + testNestedModule("B4") + testNestedModule("B5") + testNestedModule("B6") +} -- cgit v1.2.3