我正在尝试使用 Scala 反射(Scala 2.11)将 Scala Map[String, Any] 转换为案例类,如下所示 -
val m = Map("name" -> "ABC", "age" -> 7, "gender" -> "male")
case class someCC(name: String, age: Int, gender: String)
import scala.reflect.ClassTag
def createCaseClass[T](someMap : Map[String, Any])(implicit someClassTag : ClassTag[T]) = {
val ctor = someClassTag.runtimeClass.getConstructors.head
val args = someClassTag.runtimeClass.getDeclaredFields.map(x => someMap(x.getName))
ctor.newInstance(args: _*).asInstanceOf[T]
}
不幸的是,这会导致编译错误 -
Name: Unknown Error
Message: <console>:106: error: type mismatch;
found : Array[Any]
required: Array[_ <: Object]
Note: Any >: Object, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: Object`. (SLS 3.2.10)
ctor.newInstance(args: _*).asInstanceOf[T]
^
我对使用 ClassTags 相当陌生,我知道这个错误主要是因为 java.lang.Object 是 Any 的子集,并且 Any 可能包含非 java 对象。
当我尝试用 AnyRef(对应于 JRE 中的 java.lang.Object)替换 Any 时,函数调用会导致类型不匹配错误。
import scala.reflect.ClassTag
def createCaseClass[T](someMap : Map[String, AnyRef])(implicit someClassTag : ClassTag[T]) = {
val ctor = someClassTag.runtimeClass.getConstructors.head
val args = someClassTag.runtimeClass.getDeclaredFields.map(x => someMap(x.getName))
ctor.newInstance(args: _*).asInstanceOf[T]
}
val someCC = createCaseClass[someCC](m)
Name: Unknown Error
Message: <console>:106: error: type mismatch;
found : scala.collection.immutable.Map[String,Any]
required: Map[String,AnyRef]
val someCC = createCaseClass[someCC](m)
解决此错误的最佳方法是什么?建议表示赞赏。谢谢!
更新 1 - 更新 this 以将 Any 隐式转换为 AnyRef 会导致函数调用出现错误“java.util.NoSuchElementException”。
import scala.reflect.ClassTag
import scala.reflect.runtime.universe._
def createMyClass[T](someMap : Map[String, Any])(implicit someClassTag : ClassTag[T]) = {
val ctor = someClassTag.runtimeClass.getConstructors.head
val args = someClassTag.runtimeClass.getDeclaredFields.map(x => someMap(x.getName))
ctor.newInstance(args.asInstanceOf[Seq[AnyRef]]: _*).asInstanceOf[T]
}
val m = Map("name" -> "ABC", "age" -> 7, "gender" -> "male")
case class someCC(name: String, age: Int, gender: String)
createMyClass[someCC](m)
Name: java.util.NoSuchElementException
Message: key not found: $outer
StackTrace: at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:59)
at $$$e75186ae1b35495ffea8e318378149a$$$$anonfun$1.apply(<console>:135)
at $$$e75186ae1b35495ffea8e318378149a$$$$anonfun$1.apply(<console>:135)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
at scala.collection.mutable.ArrayOps$ofRef.map(ArrayOps.scala:186)
at createMyClass(<console>:135)
我在这里做错了什么?