我正在尝试将对象状态(使用特定字段)镜像到地图
class ObjectState[T] {
// object reference
var objRef : T = _
// map that mirrors current object state
var stateMap : Map[String, Any] = _
def registerFields(fields: List[String], objectInstance: T): Unit = {
// just register
objRef = objectInstance
stateMap = fields.map(field => field -> null)(collection.breakOut)
Mirror.inMirrorObjectState(this)
}
object Mirror {
// basically populate object state
def inMirrorObjectState[T](state: ObjectState[T]): Unit = {
val objectInstance = state.objRef
stateMap.keySet foreach { key =>
println(s"Attempt to find ${key}")
println(s"Fields: ${objectInstance.getClass.getFields.size}") // zero field size
val field = objectInstance.getClass.getField(key) // exception
stateMap += (key -> field.get(objectInstance))
}
}
}
}
我的对象实例看起来像这样
val obj = new BasicObject
obj.fieldA = "field"
obj.fieldB = 10
// 1. register object in OSM and get id - might be useful
val id = OSMPlatform.registerObject(obj, List("fieldA", "fieldB"))
班级BasicObject
看起来像这样
class BasicObject {
var fieldA : String = _
var fieldB : Int = _
override def toString: String = {
"Basic object: $fieldA : $fieldB"
}
}
然后我尝试创建我的ObjectState
类
def registerObject(objectInstance: AnyRef, fields: List[String]): String = {
val id = UUID.randomUUID().toString
val objState = new ObjectState[AnyRef]
objState.registerFields(fields, objectInstance)
OSMContext.registerObject(id, objState)
id
}
但后来我得到了异常
Exception in thread "main" java.lang.NoSuchFieldException: fieldA
在那一行
val field = objectInstance.getClass.getField(key)
似乎无法找到该字段。这是为什么?归因于T
类型吗?
答案 0 :(得分:2)
根据我的运行情况,你应该使用:getDeclaredField
并使用setAccessible(true)
来摆脱IllegalAccessException。
class ObjectState[T] {
// object reference
var objRef : T = _
// map that mirrors current object state
var stateMap : Map[String, Any] = _
def registerFields(fields: List[String], objectInstance: T): Unit = {
// just register
objRef = objectInstance
stateMap = fields.map(field => field -> null)(collection.breakOut)
Mirror.inMirrorObjectState(this)
}
object Mirror {
// basically populate object state
def inMirrorObjectState[T](state: ObjectState[T]): Unit = {
val objectInstance = state.objRef
stateMap.keySet foreach { key =>
println(s"Attempt to find ${key}")
// zero field size
val value = objectInstance.getClass.getDeclaredField(key) // exception
value.setAccessible(true) // remote the IllegalAccessException
stateMap += (key -> value.get(objectInstance))
}
}
}
}
class Name () {
var name: String = _
}
val obj = new Name();
obj.name = "H"
val objState = new ObjectState[AnyRef];
objState.registerFields(List("name"), obj);
objState.stateMap
希望有所帮助,但我认为对我来说使用setAccessible api就像是一个黑客。
似乎这更好,删除了setAccessible并使用getDeclaredMethod(名称的getter)然后调用它。
scala> class ObjectState[T] {
| // object reference
| var objRef : T = _
| // map that mirrors current object state
| var stateMap : Map[String, Any] = _
|
| def registerFields(fields: List[String], objectInstance: T): Unit = {
| // just register
| objRef = objectInstance
| stateMap = fields.map(field => field -> null)(collection.breakOut)
| Mirror.inMirrorObjectState(this)
| }
|
| object Mirror {
| // basically populate object state
| def inMirrorObjectState[T](state: ObjectState[T]): Unit = {
| val objectInstance = state.objRef
| stateMap.keySet foreach { key =>
| println(s"Attempt to find ${key}")
| // zero field size
| val value = objectInstance.getClass.getDeclaredMethod(key) // exception
| stateMap += (key -> value.invoke(objectInstance))
| }
| }
| }
| }
defined class ObjectState
scala>
scala> class Name () {
| var name: String = _
| }
defined class Name
scala>
scala> val obj = new Name();
obj: Name = Name@fb21b91
scala> obj.name = "H"
obj.name: String = H
scala>
scala> val objState = new ObjectState[AnyRef];
objState: ObjectState[AnyRef] = ObjectState@688bf369
scala> objState.registerFields(List("name"), obj);
Attempt to find name
scala> objState.stateMap
res1: Map[String,Any] = Map(name -> H)