在Scala宏中获取标准方法名称 [英] Get fully qualified method name in scala macros

查看:126
本文介绍了在Scala宏中获取标准方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Scala宏并匹配Apply,我想获得被调用函数的完全限定名称.

I use Scala macros and match Apply and I would like to get fully qualified name of the function which is called.

示例:

println("") -> scala.Predef.println
scala.Predef.println("") -> scala.Predef.println
class Abc {
  def met(): Unit = ???
}
case class X { 
  def met(): Unit = ???
  def abc(): Abc = ???
}
val a = new Abc()
val x = new Abc()
a.met() -> Abc.met
new Abc().met() -> Abc.met
X() -> X.apply
X().met() -> X.met
x.met() -> X.met
x.abc.met() -> Abc.met

左侧是我想要的代码,右侧是我想要得到的箭头.是否有可能?以及如何?

On the left side is what I have in code and on the right side after arrow is what I want to get. Is it possible? And how?

推荐答案

以下是宏:

import scala.language.experimental.macros
import scala.reflect.macros.blackbox

object ExampleMacro {

  final val useFullyQualifiedName = false

  def methodName(param: Any): String = macro debugParameters_Impl

  def debugParameters_Impl(c: blackbox.Context)(param: c.Expr[Any]): c.Expr[String] = {
    import c.universe._

    param.tree match {
      case Apply(Select(t, TermName(methodName)), _) =>
        val baseClass = t.tpe.resultType.baseClasses.head // there may be a better way than this line
        val className = if (useFullyQualifiedName) baseClass.fullName else baseClass.name
        c.Expr[String](Literal(Constant(className + "." + methodName)))
      case _ => sys.error("Not a method call: " + show(param.tree))
    }
  }
}

宏的用法:

object Main {

  def main(args: Array[String]): Unit = {

    class Abc {
      def met(): Unit = ???
    }
    case class X() {
      def met(): Unit = ???

      def abc(): Abc = ???
    }
    val a = new Abc()
    val x = X()

    import sk.ygor.stackoverflow.q53326545.macros.ExampleMacro.methodName

    println(methodName(Main.main(Array("foo", "bar"))))
    println(methodName(a.met()))
    println(methodName(new Abc().met()))
    println(methodName(X()))
    println(methodName(X().met()))
    println(methodName(x.met()))
    println(methodName(x.abc().met()))
    println(methodName("a".getClass))
  }

}

源代码此示例包含以下内容:

Source code for this example contains following:

  • 这是一个多模块SBT项目,因为宏必须与使用宏的类位于不同的编译单元中
  • 宏模块明确依赖于libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value,

这篇关于在Scala宏中获取标准方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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