如何在Scala用户定义注释中使用命名参数?

问题描述 投票:1回答:1

在下面的代码中,我能够获得第一个注释对象“Publishable”,但不是第二个。第二个使用命名参数,它转换为x $ 2,x $ 3,x $ 1作为AST中的参数。我该怎么做呢?

class Publishable(
    val path: String = "",
    val format: String = "",
    val saveMode: String = "overwrite"
) extends scala.annotation.StaticAnnotation {
    def show: Unit = {
        println(s"path=$path, format=$format, saveMode=$saveMode")
    }
}

class TestObject {
    @Publishable("a", "b")
    def method1 = 100

    @Publishable(saveMode = "c")
    def method2 = 200
}

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

val mirror = runtimeMirror(getClass.getClassLoader)
typeOf[TestObject].decls.foreach(field => {
    println(s"==== $field ====")
    field.annotations.foreach(anno => {
        println(s"tree = ${show(anno.tree)}")
        import scala.tools.reflect.ToolBox
        val tb = mirror.mkToolBox()
        val pub = tb.eval(tb.untypecheck(anno.tree)).asInstanceOf[Publishable]
        pub.show
    })
})

输出:

==== constructor TestObject ====
==== method method1 ====
tree = new Publishable("a", "b", $line41.$read.$iw.$iw.Publishable.<init>$default$3)
path=a, format=b, saveMode=overwrite
==== method method2 ====
tree = new Publishable(x$2, x$3, x$1)
java.lang.IllegalArgumentException: Could not find proxy for val x$2: String in List(value x$2, value <local TestObject>, class TestObject, object $iw, object $iw, object $read, package $line42, package <root>) (currentOwner= method wrapper )
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:326)
at scala.tools.nsc.transform.LambdaLift$LambdaLifter.scala$tools$nsc$transform$LambdaLift$LambdaLifter$$searchIn$1(LambdaLift.scala:331)
....
scala reflection annotations abstract-syntax-tree
1个回答
2
投票

编译代码会产生警告:

Warning: Usage of named or default arguments transformed this annotation
constructor call into a block. The corresponding AnnotationInfo
will contain references to local values and default getters instead
of the actual argument trees
  @Publishable(saveMode = "c")

您可以使用默认参数,例如

tree = new Publishable("a", "b", $line41.$read.$iw.$iw.Publishable.<init>$default$3)

使用这种技术:How do I access default parameter values via Scala reflection?用于命名为$lessinit$greater$default$3的方法。

但是关于命名参数,比如in

tree = new Publishable(x$2, x$3, x$1)

它们的值存储在局部变量中(错误表示为Could not find proxy for val x$2),局部变量不能通过反射获得(既不是Scala反射也不是Java反射)https://www.thecodingforums.com/threads/java-reflection-with-local-variables.599017/

您可以在编译时使用宏注释的默认和命名参数:Getting Parameters from Scala Macro Annotation

© www.soinside.com 2019 - 2024. All rights reserved.