Scala Macros: Checking for a certain annotation

后端 未结 1 637
别那么骄傲
别那么骄傲 2021-02-20 07:24

Thanks to the answers to my previous question, I was able to create a function macro such that it returns a Map that maps each field name to its value of a class, e

1条回答
  •  不知归路
    2021-02-20 07:30

    The annotation will be on the val itself, not on the accessor. The easiest way to access the val is through the accessed method on MethodSymbol:

    def isTransient(m: MethodSymbol) = m.accessed.annotations.exists(
      _.tpe =:= typeOf[scala.transient]
    )
    

    Now you can just write the following in your collect:

    case m: MethodSymbol if m.isAccessor && !isTransient(m) =>
    

    Note that the version of isTransient I've given here has to be defined in your macro, since it needs the imports from c.universe, but you could factor it out by adding a Universe argument if you're doing this kind of thing in several macros.

    0 讨论(0)
提交回复
热议问题