From e8f1a423d1d02c488ccd8e9940c55c2d9859cf42 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Wed, 11 Jul 2012 14:28:46 +0200 Subject: SI-5498 completes ModuleMirror.instance As per referenced issue, this patch implements `instance` for ModuleMirrors corresponding to nested and inner modules. --- test/files/run/reflection-modulemirror-inner-good.check | 3 +-- test/files/run/reflection-modulemirror-nested-good.check | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'test/files') diff --git a/test/files/run/reflection-modulemirror-inner-good.check b/test/files/run/reflection-modulemirror-inner-good.check index 0bf38a73d1..fe658e7087 100644 --- a/test/files/run/reflection-modulemirror-inner-good.check +++ b/test/files/run/reflection-modulemirror-inner-good.check @@ -1,2 +1 @@ -inner and nested modules are not supported yet -() +R diff --git a/test/files/run/reflection-modulemirror-nested-good.check b/test/files/run/reflection-modulemirror-nested-good.check index 0bf38a73d1..fe658e7087 100644 --- a/test/files/run/reflection-modulemirror-nested-good.check +++ b/test/files/run/reflection-modulemirror-nested-good.check @@ -1,2 +1 @@ -inner and nested modules are not supported yet -() +R -- cgit v1.2.3 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! --- .../scala/reflect/runtime/JavaMirrors.scala | 25 +++++++--- .../scala/reflect/runtime/ReflectionUtils.scala | 13 +++-- test/files/run/reflection-enclosed-basic.check | 18 +++++++ test/files/run/reflection-enclosed-basic.scala | 46 +++++++++++++++++ .../run/reflection-enclosed-inner-basic.check | 20 ++++++++ .../run/reflection-enclosed-inner-basic.scala | 52 +++++++++++++++++++ .../reflection-enclosed-inner-inner-basic.check | 20 ++++++++ .../reflection-enclosed-inner-inner-basic.scala | 58 ++++++++++++++++++++++ .../reflection-enclosed-inner-nested-basic.check | 20 ++++++++ .../reflection-enclosed-inner-nested-basic.scala | 55 ++++++++++++++++++++ .../run/reflection-enclosed-nested-basic.check | 20 ++++++++ .../run/reflection-enclosed-nested-basic.scala | 52 +++++++++++++++++++ .../reflection-enclosed-nested-inner-basic.check | 20 ++++++++ .../reflection-enclosed-nested-inner-basic.scala | 54 ++++++++++++++++++++ .../reflection-enclosed-nested-nested-basic.check | 20 ++++++++ .../reflection-enclosed-nested-nested-basic.scala | 54 ++++++++++++++++++++ test/files/run/reflection-repl-classes.check | 35 +++++++++++++ test/files/run/reflection-repl-classes.scala | 22 ++++++++ test/files/run/reflection-repl-elementary.check | 9 ++++ test/files/run/reflection-repl-elementary.scala | 7 +++ test/files/run/reflection-repl.check | 9 ---- test/files/run/reflection-repl.scala | 7 --- test/files/run/reflection-sanitychecks.check | 4 +- 23 files changed, 613 insertions(+), 27 deletions(-) create mode 100644 test/files/run/reflection-enclosed-basic.check create mode 100644 test/files/run/reflection-enclosed-basic.scala create mode 100644 test/files/run/reflection-enclosed-inner-basic.check create mode 100644 test/files/run/reflection-enclosed-inner-basic.scala create mode 100644 test/files/run/reflection-enclosed-inner-inner-basic.check create mode 100644 test/files/run/reflection-enclosed-inner-inner-basic.scala create mode 100644 test/files/run/reflection-enclosed-inner-nested-basic.check create mode 100644 test/files/run/reflection-enclosed-inner-nested-basic.scala create mode 100644 test/files/run/reflection-enclosed-nested-basic.check create mode 100644 test/files/run/reflection-enclosed-nested-basic.scala create mode 100644 test/files/run/reflection-enclosed-nested-inner-basic.check create mode 100644 test/files/run/reflection-enclosed-nested-inner-basic.scala create mode 100644 test/files/run/reflection-enclosed-nested-nested-basic.check create mode 100644 test/files/run/reflection-enclosed-nested-nested-basic.scala create mode 100644 test/files/run/reflection-repl-classes.check create mode 100644 test/files/run/reflection-repl-classes.scala create mode 100644 test/files/run/reflection-repl-elementary.check create mode 100644 test/files/run/reflection-repl-elementary.scala delete mode 100644 test/files/run/reflection-repl.check delete mode 100644 test/files/run/reflection-repl.scala (limited to 'test/files') diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala index c6e7a5d7b7..4ce2cda04a 100644 --- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala +++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala @@ -1092,12 +1092,25 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym noClass else if (clazz.owner.isPackageClass) javaClass(clazz.javaClassName) - else if (clazz.owner.isClass) - classToJava(clazz.owner.asClass) - .getDeclaredClasses - .find(_.getSimpleName == clazz.name.toString) - .getOrElse(noClass) - else + else if (clazz.owner.isClass) { + val childOfClass = !clazz.owner.isModuleClass + val childOfTopLevel = clazz.owner.owner.isPackageClass + val childOfTopLevelObject = clazz.owner.isModuleClass && childOfTopLevel + + // suggested in https://issues.scala-lang.org/browse/SI-4023?focusedCommentId=54759#comment-54759 + var ownerClazz = classToJava(clazz.owner.asClass) + if (childOfTopLevelObject) ownerClazz = Class.forName(ownerClazz.getName stripSuffix "$", true, ownerClazz.getClassLoader) + val ownerChildren = ownerClazz.getDeclaredClasses + + var fullNameOfJavaClass = ownerClazz.getName + if (childOfClass || childOfTopLevel) fullNameOfJavaClass += "$" + fullNameOfJavaClass += clazz.name + if (clazz.isModuleClass) fullNameOfJavaClass += "$" + + // println(s"ownerChildren = ${ownerChildren.toList}") + // println(s"fullNameOfJavaClass = $fullNameOfJavaClass") + ownerChildren.find(_.getName == fullNameOfJavaClass).getOrElse(noClass) + } else noClass } diff --git a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala index 3c22d478ce..e87c6b339b 100644 --- a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala +++ b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala @@ -6,7 +6,7 @@ package scala.reflect.runtime import java.lang.{Class => jClass} -import java.lang.reflect.{ InvocationTargetException, UndeclaredThrowableException } +import java.lang.reflect.{ Method, InvocationTargetException, UndeclaredThrowableException } /** A few java-reflection oriented utility functions useful during reflection bootstrapping. */ @@ -72,8 +72,15 @@ object ReflectionUtils { def staticSingletonInstance(clazz: Class[_]): AnyRef = clazz getField "MODULE$" get null def innerSingletonInstance(outer: AnyRef, className: String): AnyRef = { - val name = if (className endsWith "$") className.substring(0, className.length - 1) else className - val accessor = outer.getClass getDeclaredMethod name + val accessorName = if (className endsWith "$") className.substring(0, className.length - 1) else className + def singletonAccessor(clazz: Class[_]): Option[Method] = + if (clazz == null) None + else { + val declaredAccessor = clazz.getDeclaredMethods.filter(_.getName == accessorName).headOption + declaredAccessor orElse singletonAccessor(clazz.getSuperclass) + } + + val accessor = singletonAccessor(outer.getClass) getOrElse { throw new NoSuchMethodException(s"${outer.getClass.getName}.$accessorName") } accessor setAccessible true accessor invoke outer } diff --git a/test/files/run/reflection-enclosed-basic.check b/test/files/run/reflection-enclosed-basic.check new file mode 100644 index 0000000000..41f6a72f1c --- /dev/null +++ b/test/files/run/reflection-enclosed-basic.check @@ -0,0 +1,18 @@ +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 diff --git a/test/files/run/reflection-enclosed-basic.scala b/test/files/run/reflection-enclosed-basic.scala new file mode 100644 index 0000000000..1dcb6c2a27 --- /dev/null +++ b/test/files/run/reflection-enclosed-basic.scala @@ -0,0 +1,46 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.reflect.{classTag, ClassTag} + +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 { + 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 = cm.staticClass(name) + 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 = cm.staticModule(name) + println(sym) + val moduleMirror = cm.reflectModule(sym) + val instance = moduleMirror.instance + println(instance) + testMethodInvocation(instance) + } + + testNestedModule("B3") + testNestedModule("B4") + testNestedModule("B5") + testNestedModule("B6") +} diff --git a/test/files/run/reflection-enclosed-inner-basic.check b/test/files/run/reflection-enclosed-inner-basic.check new file mode 100644 index 0000000000..984fb1ff12 --- /dev/null +++ b/test/files/run/reflection-enclosed-inner-basic.check @@ -0,0 +1,20 @@ +class B +List(constructor B, class B1, class B2, object B3, object B4, object B5, object B6) +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 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("")).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") +} diff --git a/test/files/run/reflection-enclosed-inner-inner-basic.check b/test/files/run/reflection-enclosed-inner-inner-basic.check new file mode 100644 index 0000000000..8987f31b18 --- /dev/null +++ b/test/files/run/reflection-enclosed-inner-inner-basic.check @@ -0,0 +1,20 @@ +class BB +List(constructor BB, class B1, class B2, object B3, object B4, object B5, object B6) +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 diff --git a/test/files/run/reflection-enclosed-inner-inner-basic.scala b/test/files/run/reflection-enclosed-inner-inner-basic.scala new file mode 100644 index 0000000000..1b9e19d37d --- /dev/null +++ b/test/files/run/reflection-enclosed-inner-inner-basic.scala @@ -0,0 +1,58 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.reflect.{classTag, ClassTag} + +class B { + class 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.classSymbol(classTag[B#BB].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("")).asMethod + val outer1 = new B + val outer2 = new outer1.BB + val ctorMirror = cm.reflect(outer2).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 outer1 = new B + val outer2 = new outer1.BB + val moduleMirror = cm.reflect(outer2).reflectModule(sym) + val instance = moduleMirror.instance + println(instance) + testMethodInvocation(instance) + } + + testInnerModule("B3") + testInnerModule("B4") + testInnerModule("B5") + testInnerModule("B6") +} diff --git a/test/files/run/reflection-enclosed-inner-nested-basic.check b/test/files/run/reflection-enclosed-inner-nested-basic.check new file mode 100644 index 0000000000..0f5176a6e7 --- /dev/null +++ b/test/files/run/reflection-enclosed-inner-nested-basic.check @@ -0,0 +1,20 @@ +object BB +List(constructor BB, class B1, class B2, object B3, object B4, object B5, object B6) +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 diff --git a/test/files/run/reflection-enclosed-inner-nested-basic.scala b/test/files/run/reflection-enclosed-inner-nested-basic.scala new file mode 100644 index 0000000000..2800ee2548 --- /dev/null +++ b/test/files/run/reflection-enclosed-inner-nested-basic.scala @@ -0,0 +1,55 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.reflect.{classTag, ClassTag} + +class 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 outer1 = new B() + val b = cm.moduleSymbol(classTag[outer1.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.reflect(outer1.BB).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.reflect(outer1.BB).reflectModule(sym) + val instance = moduleMirror.instance + println(instance) + testMethodInvocation(instance) + } + + testNestedModule("B3") + testNestedModule("B4") + testNestedModule("B5") + testNestedModule("B6") +} diff --git a/test/files/run/reflection-enclosed-nested-basic.check b/test/files/run/reflection-enclosed-nested-basic.check new file mode 100644 index 0000000000..b0e61149f8 --- /dev/null +++ b/test/files/run/reflection-enclosed-nested-basic.check @@ -0,0 +1,20 @@ +object B +List(constructor B, class B1, class B2, object B3, object B4, object B5, object B6) +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 diff --git a/test/files/run/reflection-enclosed-nested-basic.scala b/test/files/run/reflection-enclosed-nested-basic.scala new file mode 100644 index 0000000000..8b740c2da2 --- /dev/null +++ b/test/files/run/reflection-enclosed-nested-basic.scala @@ -0,0 +1,52 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.reflect.{classTag, ClassTag} + +object 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.moduleSymbol(classTag[B.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") +} diff --git a/test/files/run/reflection-enclosed-nested-inner-basic.check b/test/files/run/reflection-enclosed-nested-inner-basic.check new file mode 100644 index 0000000000..add7a81c0a --- /dev/null +++ b/test/files/run/reflection-enclosed-nested-inner-basic.check @@ -0,0 +1,20 @@ +class BB +List(constructor BB, class B1, class B2, object B3, object B4, object B5, object B6) +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 diff --git a/test/files/run/reflection-enclosed-nested-inner-basic.scala b/test/files/run/reflection-enclosed-nested-inner-basic.scala new file mode 100644 index 0000000000..7466733d37 --- /dev/null +++ b/test/files/run/reflection-enclosed-nested-inner-basic.scala @@ -0,0 +1,54 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.reflect.{classTag, ClassTag} + +object B { + class 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.classSymbol(classTag[B.BB].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("")).asMethod + val ctorMirror = cm.reflect(new B.BB).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.BB).reflectModule(sym) + val instance = moduleMirror.instance + println(instance) + testMethodInvocation(instance) + } + + testInnerModule("B3") + testInnerModule("B4") + testInnerModule("B5") + testInnerModule("B6") +} diff --git a/test/files/run/reflection-enclosed-nested-nested-basic.check b/test/files/run/reflection-enclosed-nested-nested-basic.check new file mode 100644 index 0000000000..0f5176a6e7 --- /dev/null +++ b/test/files/run/reflection-enclosed-nested-nested-basic.check @@ -0,0 +1,20 @@ +object BB +List(constructor BB, class B1, class B2, object B3, object B4, object B5, object B6) +class B1 +B1 +1 +class B2 +B2 +2 +object B3 +B3 +3 +object B4 +B4 +4 +object B5 +B5 +5 +object B6 +B6 +6 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") +} diff --git a/test/files/run/reflection-repl-classes.check b/test/files/run/reflection-repl-classes.check new file mode 100644 index 0000000000..1c7f86c90c --- /dev/null +++ b/test/files/run/reflection-repl-classes.check @@ -0,0 +1,35 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> + +scala> class A +defined class A + +scala> + +scala> class B { + def foo(x: A) = 1 +} +defined class B + +scala> + +scala> object defs { + val cm = reflect.runtime.currentMirror + val u = cm.universe + val im = cm.reflect(new B) + val method = im.symbol.typeSignature.member(u.newTermName("foo")).asMethod + val mm = im.reflectMethod(method) +} +defined module defs + +scala> import defs._ +import defs._ + +scala> + +scala> mm(new A) +res0: Any = 1 + +scala> diff --git a/test/files/run/reflection-repl-classes.scala b/test/files/run/reflection-repl-classes.scala new file mode 100644 index 0000000000..80e332cde3 --- /dev/null +++ b/test/files/run/reflection-repl-classes.scala @@ -0,0 +1,22 @@ +import scala.tools.partest.ReplTest + +object Test extends ReplTest { + def code = """ + |class A + | + |class B { + | def foo(x: A) = 1 + |} + | + |object defs { + | val cm = reflect.runtime.currentMirror + | val u = cm.universe + | val im = cm.reflect(new B) + | val method = im.symbol.typeSignature.member(u.newTermName("foo")).asMethod + | val mm = im.reflectMethod(method) + |} + |import defs._ + | + |mm(new A) + |""".stripMargin +} diff --git a/test/files/run/reflection-repl-elementary.check b/test/files/run/reflection-repl-elementary.check new file mode 100644 index 0000000000..341dd10ab0 --- /dev/null +++ b/test/files/run/reflection-repl-elementary.check @@ -0,0 +1,9 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> + +scala> scala.reflect.runtime.universe.typeOf[List[Nothing]] +res0: reflect.runtime.universe.Type = scala.List[Nothing] + +scala> diff --git a/test/files/run/reflection-repl-elementary.scala b/test/files/run/reflection-repl-elementary.scala new file mode 100644 index 0000000000..72b65a1a70 --- /dev/null +++ b/test/files/run/reflection-repl-elementary.scala @@ -0,0 +1,7 @@ +import scala.tools.partest.ReplTest + +object Test extends ReplTest { + def code = """ + |scala.reflect.runtime.universe.typeOf[List[Nothing]] + |""".stripMargin +} diff --git a/test/files/run/reflection-repl.check b/test/files/run/reflection-repl.check deleted file mode 100644 index 341dd10ab0..0000000000 --- a/test/files/run/reflection-repl.check +++ /dev/null @@ -1,9 +0,0 @@ -Type in expressions to have them evaluated. -Type :help for more information. - -scala> - -scala> scala.reflect.runtime.universe.typeOf[List[Nothing]] -res0: reflect.runtime.universe.Type = scala.List[Nothing] - -scala> diff --git a/test/files/run/reflection-repl.scala b/test/files/run/reflection-repl.scala deleted file mode 100644 index 72b65a1a70..0000000000 --- a/test/files/run/reflection-repl.scala +++ /dev/null @@ -1,7 +0,0 @@ -import scala.tools.partest.ReplTest - -object Test extends ReplTest { - def code = """ - |scala.reflect.runtime.universe.typeOf[List[Nothing]] - |""".stripMargin -} diff --git a/test/files/run/reflection-sanitychecks.check b/test/files/run/reflection-sanitychecks.check index a1df486b51..821457a999 100644 --- a/test/files/run/reflection-sanitychecks.check +++ b/test/files/run/reflection-sanitychecks.check @@ -6,7 +6,7 @@ method #2: 14 constructor #1: scala.ScalaReflectionException: expected a constructor of class D, you provided method bar constructor #2: scala.ScalaReflectionException: expected a constructor of class D, you provided constructor C class: CC -object: java.lang.Error: inner and nested modules are not supported yet +object: CO =========members of D in a mirror of D========= field #1: 21 @@ -16,7 +16,7 @@ method #2: 14 constructor #1: scala.ScalaReflectionException: expected a constructor of class D, you provided method bar constructor #2: an instance of class D class: CC -object: java.lang.Error: inner and nested modules are not supported yet +object: CO =========members of E in a mirror of D========= field #1: scala.ScalaReflectionException: expected a member of class D, you provided value E.foo -- cgit v1.2.3