0

我正在尝试使用 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)

我在这里做错了什么?

4

1 回答 1

2

使用演员表:

ctor.newInstance(args.asInstanceOf[Seq[AnyRef]]: _*).asInstanceOf[T]

请注意,您将案例类的每个字段都映射到构造函数参数。这是不正确的,因为案例类可以具有不在构造函数中的字段,并且您的代码将中断。

一个更好的主意是使用 Scala 反射:

import reflect.runtime.universe._

def mkClassInstance[T: TypeTag](args: Map[String, Any]): T = {
  val rMirror = runtimeMirror(getClass.getClassLoader)
  val cMirror = rMirror.reflectClass(typeOf[T].typeSymbol.asClass)
  // The primary constructor is the first one
  val ctor = typeOf[T].decl(termNames.CONSTRUCTOR).asTerm.alternatives.head.asMethod
  val argList = ctor.paramLists.flatten.map(param => args(param.name.toString))
  cMirror.reflectConstructor(ctor)(argList: _*).asInstanceOf[T]
}
def mkInnerClassInstance[T: TypeTag](outer: Any)(args: Map[String, Any]): T = {
  val rMirror = runtimeMirror(getClass.getClassLoader)
  val cMirror = rMirror.reflect(outer).reflectClass(typeOf[T].typeSymbol.asClass)
  // The primary constructor is the first one
  val ctor = typeOf[T].decl(termNames.CONSTRUCTOR).asTerm.alternatives.head.asMethod
  val argList = ctor.paramLists.flatten.map(param => args(param.name.toString))
  cMirror.reflectConstructor(ctor)(argList: _*).asInstanceOf[T]
}
于 2017-08-14T21:35:23.890 回答