applyMacro.scala 3.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2022 bitlap
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
package org.bitlap.tools.macros
梦境迷离's avatar
梦境迷离 已提交
23 24 25 26 27 28 29 30

import scala.reflect.macros.whitebox

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

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

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

梦境迷离's avatar
梦境迷离 已提交
37 38 39 40 41 42 43 44
    /**
     * We generate apply method with currying, and we have to deal with the first layer of currying alone.
     *
     * @param typeName
     * @param fieldss
     * @return A apply method with currying.
     * @example Return a tree, such as `def apply(int: Int)(j: Int)(k: Option[String])(t: Option[Long]): B3 = new B3(int)(j)(k)(t)`
     */
梦境迷离's avatar
梦境迷离 已提交
45 46 47 48 49
    private def getApplyMethodWithCurrying(
      typeName: TypeName,
      fieldss: List[List[Tree]],
      classTypeParams: List[Tree]
    ): Tree = {
梦境迷离's avatar
梦境迷离 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
      val allFieldsTermName = fieldss.map(f => getConstructorParamsNameWithType(f))
      val returnTypeParams = extractClassTypeParamsTypeName(classTypeParams)
      // not currying
      val applyMethod = if (fieldss.isEmpty || fieldss.size == 1) {
        q"def apply[..$classTypeParams](..${allFieldsTermName.flatten}): $typeName[..$returnTypeParams] = ${getConstructorWithCurrying(typeName, fieldss, isCase = false)}"
      } else {
        // currying
        val first = allFieldsTermName.head
        q"def apply[..$classTypeParams](..$first)(...${allFieldsTermName.tail}): $typeName[..$returnTypeParams] = ${getConstructorWithCurrying(typeName, fieldss, isCase = false)}"
      }
      applyMethod
    }

63 64
    override def createCustomExpr(classDecl: ClassDef, compDeclOpt: Option[ModuleDef] = None): Any = {
      val classDefinition = mapToClassDeclInfo(classDecl)
梦境迷离's avatar
梦境迷离 已提交
65 66 67 68 69
      val apply = getApplyMethodWithCurrying(
        classDefinition.className,
        classDefinition.classParamss,
        classDefinition.classTypeParams
      )
70
      val compDecl = appendModuleBody(compDeclOpt, List(apply), classDefinition.className)
梦境迷离's avatar
梦境迷离 已提交
71
      c.Expr(q"""
梦境迷离's avatar
梦境迷离 已提交
72 73 74
            $classDecl
            $compDecl
          """)
梦境迷离's avatar
梦境迷离 已提交
75
    }
梦境迷离's avatar
梦境迷离 已提交
76

梦境迷离's avatar
梦境迷离 已提交
77 78
    override def checkAnnottees(annottees: Seq[c.universe.Expr[Any]]): Unit = {
      super.checkAnnottees(annottees)
79 80 81 82
      val annotateeClass: ClassDef = checkGetClassDef(annottees)
      if (isCaseClass(annotateeClass)) {
        c.abort(c.enclosingPosition, ErrorMessage.ONLY_CASE_CLASS)
      }
梦境迷离's avatar
梦境迷离 已提交
83
    }
梦境迷离's avatar
梦境迷离 已提交
84
  }
梦境迷离's avatar
梦境迷离 已提交
85

梦境迷离's avatar
梦境迷离 已提交
86
}