我在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 反射工作得很好,但那是对的。有任何想法吗?