访问Companion对象中的专用字段

我有一个类陪同对象的TestClass 。 如何在scala中使用运行时反射来访问私有字段,例如在类中设置私有字段时,在配对对象中声明xyz ,如下所示。

class TestClass {  TestClass.xyz = 100 }
object TestClass { private var xyz: Int = _ }

我尝试了以下

import scala.reflect.runtime.{currentMirror, universe => ru}
val testModuleSymbol = ru.typeOf[TestClass.type].termSymbol.asModule
val moduleMirror = currentMirror.reflectModule(testModuleSymbol)
val instanceMirror = currentMirror.reflect(moduleMirror.instance)
val xyzTerm = ru.typeOf[TestClass.type].decl(ru.TermName("xyz")).asTerm.accessed.asTerm
val fieldMirror = instanceMirror.reflectField(xyzTerm)
val context = fieldMirror.get.asInstanceOf[Int]

但我得到了下面的错误。

scala> val fieldMirror = instanceMirror.reflectField(xyzTerm)
scala.ScalaReflectionException: Scala field xyz  of object TestClass isn't represented as a Java field, nor does it have a
Java accessor method. One common reason for this is that it may be a private class parameter
not used outside the primary constructor.
  at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$abort(JavaMirrors.scala:115)
  at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$ErrorNonExistentField(JavaMirrors.scala:127)
  at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectField(JavaMirrors.scala:242)
  at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectField(JavaMirrors.scala:233)
  ... 29 elided

只有当我引用TestClass中的变量xyz (即TestClass.xyz = 100 )时才引发此异常。 如果这个引用从类中删除,那么我的示例代码就可以正常工作。


得到这个工作:

import scala.reflect.runtime.universe._
import scala.reflect.runtime.{universe => ru}

val runMirror = ru.runtimeMirror(getClass.getClassLoader)
val objectDef = Class.forName("org.myorg.TestClass")
val objectTypeModule = runMirror.moduleSymbol(objectDef).asModule
val objectType = objectTypeModule.typeSignature

val methodMap = objectType.members
  .filter(_.isMethod)
  .map(d => {
    d.name.toString -> d.asMethod
  })
  .toMap

// get the scala Object
val instance = runMirror.reflectModule(objectTypeModule).instance
val instanceMirror = runMirror.reflect(instance)
// get the private value
val result = instanceMirror.reflectMethod(methodMap("xyz")).apply()
assert(result == 100)
链接地址: http://www.djcxy.com/p/76533.html

上一篇: Access private field in Companion object

下一篇: scala reflection on anonymous object