我试图得到classOf[the-abstract-class-Option],但我总是得到classOf[the-Option-*object*]. 我怎样才能得到抽象类的类呢?
两者Option.getClass都给classOf[Option[_]]我class scala.Option$。
编辑:我不必问这个;突然之间,classOf[Option[_]]工作正常,奇怪。/编辑
背景:
我试图通过反射调用一个带有Option[String]参数的方法。
它的签名看起来像这样:...(..., anySectionId: Option[String], ...)...
在调用该方法之前,我通过getDeclaredMethod. 但要做到这一点,我需要一个参数类型列表,我通过调用_.getClass每个要提供给方法的参数来构造它。但是_.getClass返回classOf[None]orclassOf[Some]用于 Option 实例,这会getDeclaredMethod导致失败,因为 (?) 签名基于 Option 而不是 Some/None。
这是代码:
val clazz: Class[_] = Play.current.classloader.loadClass(className)
val paramTypes = arguments.map(_ match {
case None => Option.getClass // gives me the object, not the abstract class
case _: Some[_] => classOf[Option[_]] // this also gives me the object :-(
case x => x.getClass // results in None or Some for Option instances
})
val m: jl.reflect.Method = clazz.getDeclaredMethod("apply", paramTypes: _*)
并且上面的最后一行对于带有任何Option参数的方法都失败了(否则一切正常)。