如何通过反射获取构造函数参数类型Scala中2.10.1(How do I obtain ctor

2019-08-18 12:10发布

我试图做的应该是非常简单的。 我认为。 我想使用反射来确定的参数名称和类型的案件类的构造函数。 下面是一个REPL互动。 可以跳到哪里“paramss”被调用结束。 我想检查或对内部列表的符号模式匹配,以确定“名”确实是一个字符串,“年龄”是一个int。

我怎么做?

scala> case class Person(name: String, age: Int)
defined class Person

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

scala> typeOf[Person]
res0: reflect.runtime.universe.Type = Person

scala> typeTag[Person]
res1: reflect.runtime.universe.TypeTag[Person] = TypeTag[Person]

scala> res1.mirror
res2: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@2daf73a4 of type class scala.tools.nsc.interpreter.IMain$TranslatingClassLoader with classpath [(memory)] and parent being scala.tools.nsc.util.ScalaClassLoader$URLClassLoader@59d12b6a of type class scala.tools.nsc.util.ScalaClassLoader$URLClassLoader with classpath [file:/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre/lib/resources.jar,file:/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre/lib/rt.jar,file:/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre/lib/jsse.jar,file:/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/jre/lib/jce.jar,file:/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/j...
scala> res1.tpe
res3: reflect.runtime.universe.Type = Person

scala> res3.typeSymbol.asClass
res4: reflect.runtime.universe.ClassSymbol = class Person

scala> res2.reflectClass(res4)
res5: reflect.runtime.universe.ClassMirror = class mirror for Person (bound to null)

scala> res3.declaration(reflect.runtime.universe.nme.CONSTRUCTOR)
res6: reflect.runtime.universe.Symbol = constructor Person

scala> res6.asMethod
res7: reflect.runtime.universe.MethodSymbol = constructor Person

scala> res7.paramss
res8: List[List[reflect.runtime.universe.Symbol]] = List(List(value name, value age))

Answer 1:

这就是你得到一个Type代表你的构造函数参数类型的对象:

val ctorParamss = typeOf[Person].member(nme.CONSTRUCTOR).asMethod.paramss
val typeOfNameParam: Type = paramss(0)(0).typeSignature

现在,你有对象,你可以对其执行各种测试:

typeOfNameParam =:= typeOf[Int]      // is this type an Int ?
typeOfNameParam <:< typeOf[Number]   // is this type java.lang.Number or its subtype ?


Answer 2:

使用SMirror :

scala> import net.fwbrasil.smirror._
import net.fwbrasil.smirror._

scala> implicit val mirror = scala.reflect.runtime.currentMirror
mirror: reflect.runtime.universe.Mirror = JavaMirror with scala.t...

scala> case class Person(name: String, age: Int)
defined class Person

scala> val clazz = sClassOf[Person]
clazz: net.fwbrasil.smirror.SClass[Person] = Person

scala> val ctor = clazz.constructors.head
ctor: net.fwbrasil.smirror.SConstructor[Person] = <init>(name: java.lang.String, age: scala.Int): Person

scala> ctor.parameters
res0: List[ctor.SParameterType] = List(name: java.lang.String, age: scala.Int)

scala> ctor.parameters.head.sClass == sClassOf[String]
res2: Boolean = true


Answer 3:

滑稽你应该问! 这正是我在做什么现在。

因此,首先,你必须paramss。 所以,你/你是否支持多个参数列表来怎么处理。 下面是一些坚持一个精确的参数列表上,并捕获该代码:

val ctorParams  : List[Symbol] = ctorParamss match {
  case Nil => {
println("Warning: Trying to populate a no-arg constructor from a Map. No data will be used.");
Nil;
  }
  case head :: Nil => head;
  case _ => throw new UnsupportedOperationException("Cannot populate a constructor with multiple param lists from a Map.");
}

现在,我们需要的名称和类型名称:

val ctorParamNames = ctorParams.map( _.name.toString.trim );
val ctorParamTypeNames = ctorParams.map( _.typeSignature.baseClasses(0).fullName.toString.trim );

我仍然在做这个,在这个非常时刻,但有一点是为我工作至今。 祝好运!



文章来源: How do I obtain ctor argument types via reflection in Scala 2.10.1