如何在Scala中的运行时获取通用类型 [英] How to obtain generic type at runtime in Scala

查看:71
本文介绍了如何在Scala中的运行时获取通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上一堂课

package myapp.model

case class Person(
 name: String,
 age: Option[Int]
)

我想实现以下功能:

def getFieldClass(className: String, fieldName:String): java.lang.Class[_] = {
    // case normal field return its class
    // case Option field return generic type of Option
}

以便进行以下输入:

  • className ="myapp.model.Person"
  • fieldName ="age"

该函数将返回类对象:scala.Int

the function will return class object: scala.Int

使用Java Reflection API的解决方案不能很好地工作,它为Option [Int]返回java.lang.Object:

Solution with Java Reflection API doesn't work well, it returns java.lang.Object for Option[Int]:

def getFieldClass(className: String, fieldName:String): java.lang.Class[_] = {
    val cls = java.lang.Class.forName(className)
    val pt = cls.getDeclaredField(fieldName).getGenericType.asInstanceOf[java.lang.reflect.ParameterizedType]
    val tpe = pt.getActualTypeArguments()(0);
    java.lang.Class.forName(tpe.getTypeName)
}

我正在编写反序列化功能的一部分,但没有对象来检查其类型,我只有一个类名.

I'm writing part of deserializing feature and I don't have the object to check it's type, I have only a class name.

推荐答案

您可以使用Scala的反射库来完成此操作.

You can accomplish this with Scala's reflection library.

虽然不是特别漂亮:

import scala.reflect.runtime.{ universe => u }
import scala.reflect.runtime.universe._

object ReflectionHelper {

  val classLoader = Thread.currentThread().getContextClassLoader

  val mirror = u.runtimeMirror(classLoader)

  def getFieldType(className: String, fieldName: String): Option[Type] = {

    val classSymbol = mirror.staticClass(className)

    for {
      fieldSymbol <- classSymbol.selfType.members.collectFirst({
        case s: Symbol if s.isPublic && s.name.decodedName.toString() == fieldName => s
      })
    } yield {

      fieldSymbol.info.resultType
    }
  }

  def maybeUnwrapFieldType[A](fieldType: Type)(implicit tag: TypeTag[A]): Option[Type] = {
    if (fieldType.typeConstructor == tag.tpe.typeConstructor) {
      fieldType.typeArgs.headOption
    } else {
      Option(fieldType)
    }
  }

  def getFieldClass(className: String, fieldName: String): java.lang.Class[_] = {

    // case normal field return its class
    // case Option field return generic type of Option

    val result = for {
      fieldType <- getFieldType(className, fieldName)
      unwrappedFieldType <- maybeUnwrapFieldType[Option[_]](fieldType)
    } yield {
      mirror.runtimeClass(unwrappedFieldType)
    }

    // Consider changing return type to: Option[Class[_]]
    result.getOrElse(null)
  }
}

然后:

ReflectionHelper.getFieldClass("myapp.model.Person", "age")  // int
ReflectionHelper.getFieldClass("myapp.model.Person", "name") // class java.lang.String

如果字段值没有意义,我建议将getFieldClass的返回类型更改为可选!

I would recommend changing the return type of getFieldClass to be optional in case the field value doesn't make sense!

这篇关于如何在Scala中的运行时获取通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆