applyMacro.scala 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2021 jxnu-liguobin && contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

梦境迷离's avatar
梦境迷离 已提交
22 23 24 25 26 27 28 29 30 31
package io.github.dreamylost.macros

import scala.reflect.macros.whitebox

/**
 *
 * @author 梦境迷离
 * @since 2021/7/7
 * @version 1.0
 */
梦境迷离's avatar
梦境迷离 已提交
32
object applyMacro {
梦境迷离's avatar
梦境迷离 已提交
33

梦境迷离's avatar
梦境迷离 已提交
34
  class applyProcessor(override val c: whitebox.Context) extends AbstractMacroProcessor(c) {
梦境迷离's avatar
梦境迷离 已提交
35

梦境迷离's avatar
梦境迷离 已提交
36
    import c.universe._
梦境迷离's avatar
梦境迷离 已提交
37

梦境迷离's avatar
梦境迷离 已提交
38 39 40 41 42
    override def impl(annottees: Expr[Any]*): Expr[Any] = {
      val args: Tuple1[Boolean] = extractArgumentsTuple1 {
        case q"new apply(verbose=$verbose)" => Tuple1(evalTree(verbose.asInstanceOf[Tree]))
        case q"new apply()"                 => Tuple1(false)
        case _                              => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
梦境迷离's avatar
梦境迷离 已提交
43
      }
梦境迷离's avatar
梦境迷离 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      val annotateeClass: ClassDef = checkAndGetClassDef(annottees: _*)
      val isCase: Boolean = isCaseClass(annotateeClass)
      c.info(c.enclosingPosition, s"impl argument: $args, isCase: $isCase", force = args._1)

      if (isCase) c.abort(c.enclosingPosition, s"Annotation is only supported on 'case class'")

      def modifiedDeclaration(classDecl: ClassDef, compDeclOpt: Option[ModuleDef] = None): Any = {
        val (className, classParams, classTypeParams) = classDecl match {
          case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends ..$bases { ..$body }" =>
            c.info(c.enclosingPosition, s"modifiedDeclaration className: $tpname, paramss: $paramss", force = args._1)
            (tpname, paramss.asInstanceOf[List[List[Tree]]], tparams.asInstanceOf[List[Tree]])
          case _ => c.abort(c.enclosingPosition, s"${ErrorMessage.ONLY_CLASS} classDef: $classDecl")
        }
        c.info(c.enclosingPosition, s"modifiedDeclaration compDeclOpt: $compDeclOpt, annotteeClassParams: $classParams", force = args._1)
        val tpName = className.asInstanceOf[TypeName]
        val apply = getApplyMethodWithCurrying(tpName, classParams, classTypeParams)
        val compDecl = modifiedCompanion(compDeclOpt, apply, tpName)
        c.Expr(
          q"""
梦境迷离's avatar
梦境迷离 已提交
63 64 65
            $classDecl
            $compDecl
          """)
梦境迷离's avatar
梦境迷离 已提交
66
      }
梦境迷离's avatar
梦境迷离 已提交
67

梦境迷离's avatar
梦境迷离 已提交
68 69
      val resTree = handleWithImplType(annottees: _*)(modifiedDeclaration)
      printTree(force = args._1, resTree.tree)
梦境迷离's avatar
梦境迷离 已提交
70

梦境迷离's avatar
梦境迷离 已提交
71 72
      resTree
    }
梦境迷离's avatar
梦境迷离 已提交
73 74
  }
}