1

假设我有一些带有一些方法的类


class Clz ... {

  def someMethod: Map[String, Long] = ... 

  def id: Long = 0L
}

我需要重用 someMethod 并覆盖 id 我不知道为什么但它会抛出 Stackoverflow 而且我需要用 Clz 的参数/方法做一些事情而不返回结果

我试过的:

object Macros {


    def impl(c: whitebox.Context)(annottees: c.Tree*): c.Tree = {
      import c.universe._

      annottees match {

        case (cls @ q"$_ class $tpname[..$_] $_(...$paramss) extends { ..$_ } with ..$_ { $_ => ..$stats }") :: Nil =>

          val someMethodM = stats
            .filter(case q"$_ def $methodName: $_ = $_" => methodName.toString == "someMethod")
            .map {
              case q"$_ def $_: $_ = $res" => res
            }

          q"""
             $cls

             object ClzCompanion {

               val someMethodsRef: Map[String, Int] = Map.apply(..$someMethodM)

               ..${
                  paramss
                    .flatten
                    .foreach {
                      case q"$_ val $nname: $tpt = $valuee" => 
                        // simply do something with valuee and nothing else
                        // "fire and forget"
                     }
                  }

               ..${
                  paramss
                    .flatten
                    .map {
                      case q"$_ val $nname: $tpt = $valuee" => 
                        // here logic, if this nname in someMethodsRef then
                        // someMethodsRef.find({ case (key, _) => key == nname) match {
                        //    case Some(_) => "def $nname: ($tpt, Int) = ... // new method with body
                        //    case None => do nothing...  
                     }
                  }

               }
           """
      }
    }
  }

我如何覆盖 Clz 的 id 方法?

为什么它会抛出 StackOverflow?

4

0 回答 0