7

我在scala中玩反射。我选择了

class CloneSubject(val a :Int, b :String, private var c :Boolean)

作为主题并迭代typeOf[CloneSubject].members查找字段(sym.isTerm && (sym.asTerm.isVar || sym.asTerm.isVal))但是当我尝试创建 FieldMirror 时,(cm mirrorField sym.asTerm)我得到这个异常:

Exception in thread "main" scala.ScalaReflectionException: Scala field b isn't represented as a Java field, 
neither it has a Java accessor method
note that private parameters of class constructors don't get mapped onto fields and/or accessors,
unless they are used outside of their declaring constructors.` 

现在,我完全理解为什么会发生这种情况,但是我找不到一种方法来检查这个而不抛出异常。当然,使用 Java 反射工作得很好,但那是对的。有任何想法吗?

4

2 回答 2

1

为避免错误,请b预先过滤掉:

scala> val m = reflect.runtime.universe.runtimeMirror(getClass.getClassLoader)
m: reflect.runtime.universe.Mirror = JavaMirror with ...

scala> val im = m.reflect(new CloneSubject(5, "hello", true))
im: reflect.runtime.universe.InstanceMirror = instance mirror for CloneSubject@2a95e173

scala> val fields = typeOf[CloneSubject].members collect { case m: MethodSymbol if m.isGetter => m.accessed }
fields: Iterable[reflect.runtime.universe.Symbol] = List(variable c, value a)

scala> fields map (s => im.reflectField(s.asTerm).get)
res45: Iterable[Any] = List(true, 5)

您知道每个可以访问的字段都有一个 Getter,因此唯一要做的就是获取所有现有 Getter 所代表的字段。

使用它来检查 val/var 是否有 Getter 的另一个选项:

scala> typeOf[CloneSubject].members.filter(s => s.isTerm && (s.asTerm.isVar || s.asTerm.isVal) && s.asTerm.getter != NoSymbol)
res50: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(variable c, value a)
于 2013-06-22T00:57:28.077 回答
1

要使用隐形墨水书写,您可以使用柠檬汁:

scala> typeOf[CloneSubject].members.collect { case s if s.name.decoded.endsWith(" ") => s }
res9: Iterable[reflect.runtime.universe.Symbol] = List(variable c, value a)

scala> cm reflect new CloneSubject(8, "foo", true)
res11: reflect.runtime.universe.InstanceMirror = instance mirror for CloneSubject@cdeca29

scala> res9 map (res11 reflectField _.asTerm) map (_.get)
res12: Iterable[Any] = List(true, 8)

神秘空间是:

scala> import reflect.runtime.universe._
import reflect.runtime.universe._

scala> nme.
CONSTRUCTOR           EMPTY                 EMPTY_PACKAGE_NAME    ERROR                 
LOCAL_SUFFIX_STRING   NameType              PACKAGE               ROOTPKG               
WILDCARD              asInstanceOf          isInstanceOf          toString              

scala> nme.LOCAL_SUFFIX_STRING
res0: String = " "
于 2013-06-22T06:58:32.493 回答