本文介绍了如何通过 Scala 2.10.1 中的反射获取 ctor 参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想要做的应该非常简单.我认为.我想使用反射来确定案例类构造函数的参数的名称和类型.下面是一个 REPL 交互.您可以跳到调用 'paramss' 的末尾.我想对内部列表中的符号进行检查或模式匹配,以确定名称"确实是一个字符串,而年龄"是一个 Int.

What I'm trying to do should be really simple. I think. I want to use reflection to determine the names and types of the arguments to a case class constructor. Below is a REPL interaction. You can skip to the end where 'paramss' gets called. I want to examine or pattern match against the Symbols in the inner list to determine that 'name' is indeed a String and 'age' is an 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))

推荐答案

使用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

这篇关于如何通过 Scala 2.10.1 中的反射获取 ctor 参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:09