简体   繁体   English

使用Scala在通用类型上调用方法并反射包

[英]Invoke a method on a generic type with scala and reflect package

My question is based on a search that I have made on the following pages (but I am still to new to scala to succeed in what I want to do): 我的问题是基于我在以下页面上进行的搜索(但是我仍然对scala陌生,无法成功完成自己想做的事情):

reflection overview 反射概述

The purpose of my code is to invoke a method from a generic type and not an instance of a known type. 我的代码的目的是从通用类型而不是已知类型的实例调用方法。

The following demonstrate the idea: 以下说明了这个想法:

class A {
  def process = {
    (1 to 1000).foreach(x => x + 10)
  }
}

def getTypeTag[T: ru.TypeTag](obj: T) = ru.typeTag[T]

def perf[T: ru.TypeTag](t: T, sMethodName: String): Any = {
  val m = ru.runtimeMirror(t.getClass.getClassLoader)

  val myType = ru.typeTag[T].tpe

  val mn = myType.declaration(ru.newTermName(sMethodName)).asMethod

  val im = m.reflect(getTypeTag(t))

  val toCall = im.reflectMethod(mn)

  toCall()

}


val a = new A
perf(a, "process")

The code compile perfectly (on a worksheet) but give the following stack at execution: 该代码可以完美地编译(在工作表上),但是在执行时提供以下堆栈:

scala.ScalaReflectionException: expected a member of class TypeTagImpl, you provided method A$A11.A$A11.A.process
    at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$ErrorNotMember(test-log4j.sc:126)
    at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf$1.apply(test-log4j.sc:221)
    at scala.reflect.runtime.JavaMirrors$JavaMirror.ensuringNotFree(test-log4j.sc:210)
    at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf(test-log4j.sc:220)
    at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(test-log4j.sc:257)
    at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(test-log4j.sc:239)
    at #worksheet#.perf(test-log4j.sc:20)
    at #worksheet#.get$$instance$$res0(test-log4j.sc:28)
    at #worksheet#.#worksheet#(test-log4j.sc:138)

Any idea about how to correct this ? 关于如何纠正这个想法?

Many thanks to all 非常感谢所有人

In order to reflect a particular object, you have to pass it to Mirror.reflect(obj: T) , and you're passing its typeTag for some reason. 为了反映特定对象,您必须将其传递给Mirror.reflect(obj: T) ,并且出于某种原因要传递其typeTag。 To fix, you have to modify perf signature to generate a ClassTag along with a TypeTag , and pass t directly to reflect , like so: 要解决此问题,您必须修改perf签名以生成ClassTagTypeTag ,然后将t直接传递给reflect ,如下所示:

class A {
  def process = {
    (1 to 1000).foreach(x => x + 10)
    println("ok!")
  }
}

def perf[T : ClassTag : ru.TypeTag](t: T, sMethodName: String): Any = {
//         ^ modified here
  val m = ru.runtimeMirror(t.getClass.getClassLoader)
  val myType = ru.typeTag[T].tpe
  val mn = myType.decl(ru.TermName(sMethodName)).asMethod
  val im = m.reflect(t)
//                   ^ and here
  val toCall = im.reflectMethod(mn)
  toCall()
}


val a = new A
perf(a, "process")
// ok!
// res0: Any = ()

(Note: I also replaced deprecated declaration and newTermName with recommended alternatives) (注意:我还用推荐的替代品替换了已弃用的declarationnewTermName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM