如何从AnyRef对象获取反映的运行时方法?

tex*_*uce 5 reflection scala

我试图在一个实例中获得反射的运行时方法,但它没有显示在decls结果中:

val foo: AnyRef = new Object {
  def bar = 1
}
typeOf[foo.type].decls //Does not contain bar method
Run Code Online (Sandbox Code Playgroud)

我尝试使用Java反射类,它可以工作:

foo.getClass.getDeclaredMethods //It contains bar method
Run Code Online (Sandbox Code Playgroud)

但我更喜欢使用MethodSymbols和Scala Type而不是Java Class和Method反射.如何获得反映的MethodSymbol?


我想要一个方法来查找作为方法栏的AnyRef传递的对象并调用它.如下所示:

def getBarMethodFromObj(obj: AnyRef): MethodSymbol = {
  //typeOf(obj).decl(TermName("bar")) this doesn't work
}
Run Code Online (Sandbox Code Playgroud)

我不能使用trait因为bar可以有不同的参数并返回类型和数字.由于Scala不支持varadic泛型参数,我计划使用反射来查找方法和调用,但这也不能在Scala中完成.我目前正在使用Java解决方案:

val bar = foo.getClass.getDeclaredMethods.find(_.getName == "bar")
bar.invoke(foo, params: _*)
Run Code Online (Sandbox Code Playgroud)

但是,Java反射不会保留泛型类型,因为它会为List和Map等创建问题.所以我想知道我是否可以在Scala中实现它,或者是否有任何新的解决方案

Bri*_*hon 6

我不知道你要做什么,但删除AnyRef注释使你的代码工作:

val foo = new { def bar = 1 }
typeOf[foo.type].decls // Contains bar method
Run Code Online (Sandbox Code Playgroud)

如果需要类型注释(例如,在方法签名中),则可以使用编译器推断的相同结构类型:

val foo: { def bar: Int } = new { def bar = 1 }
Run Code Online (Sandbox Code Playgroud)

如果你想从另一种方法获得完整的方法列表而不知道除了通用泛型之外的确切类型,你可能会对以下内容感兴趣TypeTag:

import scala.reflect.runtime.universe.{ TypeTag, typeTag }
val foo = new { def bar = 1 }
def getMethods[T: TypeTag](t: T) = typeTag[T].tpe.decls
getMethods(foo) // Contains bar
Run Code Online (Sandbox Code Playgroud)

如果您无法使用TypeTag(可能因为您无法进行API更改),那么您最好使用Java反射API.Scala反射API通常设计为使用类型信息,因此如果您只知道类型,它可能不适合您AnyRef.


为了回应您的编辑:

我不能使用trait因为bar可以有不同的参数并返回类型和数字.

你当然可以:

trait Foo[A, B] {
  def bar(a: A): B
}
Run Code Online (Sandbox Code Playgroud)

如果你需要多个参数,只需要bar取一个元组.如果你需要做一些元组不支持的列表操作,你可能会考虑学习HLists和无形.

但是,Java反射不会保留泛型类型......

好吧,没有运行时反射API会帮助你.根本没有办法找出AnyRef运行时的通用参数,因为JVM上不存在该信息.使用TypeTags,或trait以上述方式使用a .