diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2012-07-11 14:28:46 +0200 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2012-08-07 17:58:10 +0200 |
commit | e8f1a423d1d02c488ccd8e9940c55c2d9859cf42 (patch) | |
tree | 1060c77894ff691b70761fabc013b41704203b50 | |
parent | d8b35a11d67c2a597f1d93456b341a650b542520 (diff) | |
download | scala-e8f1a423d1d02c488ccd8e9940c55c2d9859cf42.tar.gz scala-e8f1a423d1d02c488ccd8e9940c55c2d9859cf42.tar.bz2 scala-e8f1a423d1d02c488ccd8e9940c55c2d9859cf42.zip |
SI-5498 completes ModuleMirror.instance
As per referenced issue, this patch implements `instance`
for ModuleMirrors corresponding to nested and inner modules.
4 files changed, 16 insertions, 22 deletions
diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala index 2bf57d61cc..c6e7a5d7b7 100644 --- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala +++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala @@ -18,7 +18,7 @@ import collection.mutable.{ HashMap, ListBuffer } import internal.Flags._ //import scala.tools.nsc.util.ScalaClassLoader //import scala.tools.nsc.util.ScalaClassLoader._ -import ReflectionUtils.{singletonInstance} +import ReflectionUtils.{staticSingletonInstance, innerSingletonInstance} import language.existentials import scala.runtime.{ScalaRunTime, BoxesRunTime} import scala.reflect.internal.util.Collections._ @@ -403,8 +403,11 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym def erasure = symbol.moduleClass.asClass def isStatic = true def instance = { - if (!symbol.owner.isPackageClass) throw new Error("inner and nested modules are not supported yet") - singletonInstance(classLoader, symbol.fullName) + if (symbol.owner.isPackageClass) + staticSingletonInstance(classLoader, symbol.fullName) + else + if (outer == null) staticSingletonInstance(classToJava(symbol.moduleClass.asClass)) + else innerSingletonInstance(outer, symbol.name) } def companion: Option[ClassMirror] = symbol.companionClass match { case cls: ClassSymbol => Some(new JavaClassMirror(outer, cls)) diff --git a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala index 2c7be6e317..3c22d478ce 100644 --- a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala +++ b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala @@ -63,25 +63,18 @@ object ReflectionUtils { } } - def singletonInstance(cl: ClassLoader, className: String): AnyRef = { + def staticSingletonInstance(cl: ClassLoader, className: String): AnyRef = { val name = if (className endsWith "$") className else className + "$" val clazz = java.lang.Class.forName(name, true, cl) - val singleton = clazz getField "MODULE$" get null - singleton + staticSingletonInstance(clazz) } - // Retrieves the MODULE$ field for the given class name. - def singletonInstanceOpt(cl: ClassLoader, className: String): Option[AnyRef] = - try Some(singletonInstance(cl, className)) - catch { case _: ClassNotFoundException => None } + def staticSingletonInstance(clazz: Class[_]): AnyRef = clazz getField "MODULE$" get null - def invokeFactory(cl: ClassLoader, className: String, methodName: String, args: AnyRef*): AnyRef = { - val singleton = singletonInstance(cl, className) - val method = singleton.getClass.getMethod(methodName, classOf[ClassLoader]) - method.invoke(singleton, args: _*) + 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 + accessor setAccessible true + accessor invoke outer } - - def invokeFactoryOpt(cl: ClassLoader, className: String, methodName: String, args: AnyRef*): Option[AnyRef] = - try Some(invokeFactory(cl, className, methodName, args: _*)) - catch { case _: ClassNotFoundException => None } } 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
|