提交 011e058b 编写于 作者: 梦境迷离's avatar 梦境迷离

refactor

上级 07e15e9c
......@@ -96,7 +96,7 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
* @param annottees
* @return Return ClassDef
*/
def checkAndGetClassDef(annottees: Expr[Any]*): ClassDef = {
def checkAndGetClassDef(annottees: Seq[Expr[Any]]): ClassDef = {
annottees.map(_.tree).toList match {
case (classDecl: ClassDef) :: Nil => classDecl
case (classDecl: ClassDef) :: (compDecl: ModuleDef) :: Nil => classDecl
......@@ -110,10 +110,10 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
* @param annottees
* @return
*/
def tryGetCompanionObject(annottees: Expr[Any]*): Option[ModuleDef] = {
def getCompanionObject(annottees: Seq[Expr[Any]]): Option[ModuleDef] = {
annottees.map(_.tree).toList match {
case (classDecl: ClassDef) :: Nil => None
case (classDecl: ClassDef) :: (compDecl: ModuleDef) :: Nil => Some(compDecl)
case (_: ClassDef) :: Nil => None
case (_: ClassDef) :: (compDecl: ModuleDef) :: Nil => Some(compDecl)
case (compDecl: ModuleDef) :: Nil => Some(compDecl)
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}
......@@ -126,8 +126,8 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
* @param annottees
* @return
*/
def treeReturnWithDefaultCompanionObject(resTree: Tree, annottees: Expr[Any]*): Tree = {
val companionOpt = tryGetCompanionObject(annottees: _*)
def returnWithCompanionObject(resTree: Tree, annottees: Seq[Expr[Any]]): Tree = {
val companionOpt = getCompanionObject(annottees)
companionOpt.fold(resTree) { t =>
q"""
$resTree
......@@ -143,13 +143,11 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
* @param modifyAction The actual processing function
* @return Return the result of modifyAction
*/
def collectCustomExpr(annottees: Expr[Any]*)
def collectCustomExpr(annottees: Seq[Expr[Any]])
(modifyAction: (ClassDef, Option[ModuleDef]) => Any): Expr[Nothing] = {
annottees.map(_.tree) match {
case (classDecl: ClassDef) :: Nil => modifyAction(classDecl, None).asInstanceOf[Expr[Nothing]]
case (classDecl: ClassDef) :: (compDecl: ModuleDef) :: Nil => modifyAction(classDecl, Some(compDecl)).asInstanceOf[Expr[Nothing]]
case _ => c.abort(c.enclosingPosition, ErrorMessage.UNEXPECTED_PATTERN)
}
val classDef = checkAndGetClassDef(annottees)
val compDecl = getCompanionObject(annottees)
modifyAction(classDef, compDecl).asInstanceOf[Expr[Nothing]]
}
/**
......@@ -199,9 +197,7 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
* @return {{ i: Int}}
*/
def getConstructorParamsNameWithType(annotteeClassParams: Seq[Tree]): Seq[Tree] = {
annotteeClassParams.map {
case v: ValDef => q"${v.name}: ${v.tpt}"
}
annotteeClassParams.map(_.asInstanceOf[ValDef]).map(v => q"${v.name}: ${v.tpt}")
}
/**
......
......@@ -55,9 +55,11 @@ object applyMacro {
}
override def impl(annottees: Expr[Any]*): Expr[Any] = {
val annotateeClass: ClassDef = checkAndGetClassDef(annottees: _*)
if (isCaseClass(annotateeClass)) c.abort(c.enclosingPosition, ErrorMessage.ONLY_CASE_CLASS)
val resTree = collectCustomExpr(annottees: _*)(createCustomExpr)
val annotateeClass: ClassDef = checkAndGetClassDef(annottees)
if (isCaseClass(annotateeClass)) {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_CASE_CLASS)
}
val resTree = collectCustomExpr(annottees)(createCustomExpr)
printTree(force = extractArgumentsDetail._1, resTree.tree)
resTree
}
......
......@@ -92,7 +92,7 @@ object builderMacro {
}
override def impl(annottees: c.universe.Expr[Any]*): c.universe.Expr[Any] = {
val resTree = collectCustomExpr(annottees: _*)(createCustomExpr)
val resTree = collectCustomExpr(annottees)(createCustomExpr)
printTree(force = true, resTree.tree)
resTree
}
......
......@@ -106,9 +106,11 @@ object constructorMacro {
}
override def impl(annottees: c.universe.Expr[Any]*): c.universe.Expr[Any] = {
val annotateeClass: ClassDef = checkAndGetClassDef(annottees: _*)
if (isCaseClass(annotateeClass)) c.abort(c.enclosingPosition, ErrorMessage.ONLY_CLASS)
val res = treeReturnWithDefaultCompanionObject(collectCustomExpr(annottees: _*)(createCustomExpr).tree, annottees: _*)
val annotateeClass: ClassDef = checkAndGetClassDef(annottees)
if (isCaseClass(annotateeClass)) {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_CLASS)
}
val res = returnWithCompanionObject(collectCustomExpr(annottees)(createCustomExpr).tree, annottees)
printTree(force = extractArgumentsDetail._1, res)
c.Expr(res)
}
......
......@@ -44,12 +44,12 @@ object equalsAndHashCodeMacro {
}
override def impl(annottees: c.universe.Expr[Any]*): c.universe.Expr[Any] = {
val annotateeClass: ClassDef = checkAndGetClassDef(annottees: _*)
val annotateeClass: ClassDef = checkAndGetClassDef(annottees)
if (isCaseClass(annotateeClass)) {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_CLASS)
}
val resTree = collectCustomExpr(annottees: _*)(createCustomExpr)
val res = treeReturnWithDefaultCompanionObject(resTree.tree, annottees: _*)
val resTree = collectCustomExpr(annottees)(createCustomExpr)
val res = returnWithCompanionObject(resTree.tree, annottees)
printTree(force = extractArgumentsDetail._1, res)
c.Expr(res)
}
......
......@@ -81,8 +81,8 @@ object jacksonEnumMacro {
}
override def impl(annottees: c.universe.Expr[Any]*): c.universe.Expr[Any] = {
val newClassExpr = collectCustomExpr(annottees: _*)(createCustomExpr)
val res = treeReturnWithDefaultCompanionObject(newClassExpr.tree, annottees: _*)
val newClassExpr = collectCustomExpr(annottees)(createCustomExpr)
val res = returnWithCompanionObject(newClassExpr.tree, annottees)
printTree(force = extractArgumentsDetail._1, res)
c.Expr(res)
}
......@@ -99,7 +99,7 @@ object jacksonEnumMacro {
q"""
..$typeReferClasses
$newClass // get field after replacing annotation for each field in constructor
$newClass
""")
}
}
......
......@@ -44,11 +44,11 @@ object jsonMacro {
}
override def impl(annottees: c.universe.Expr[Any]*): c.universe.Expr[Any] = {
val annotateeClass: ClassDef = checkAndGetClassDef(annottees: _*)
val annotateeClass: ClassDef = checkAndGetClassDef(annottees)
if (!isCaseClass(annotateeClass)) {
c.abort(c.enclosingPosition, ErrorMessage.ONLY_CASE_CLASS)
}
val resTree = collectCustomExpr(annottees: _*)(createCustomExpr)
val resTree = collectCustomExpr(annottees)(createCustomExpr)
printTree(force = true, resTree.tree)
resTree
}
......
......@@ -89,7 +89,7 @@ object logMacro {
// see https://docs.scala-lang.org/overviews/macros/annotations.html
}
val res = treeReturnWithDefaultCompanionObject(resTree, annottees: _*)
val res = returnWithCompanionObject(resTree, annottees)
printTree(force = extractArgumentsDetail._1, res)
c.Expr[Any](resTree)
}
......
......@@ -72,9 +72,9 @@ object toStringMacro {
val argument = Argument(extractArgumentsDetail._1, extractArgumentsDetail._2,
extractArgumentsDetail._3, extractArgumentsDetail._4)
// Check the type of the class, which can only be defined on the ordinary class
val annotateeClass: ClassDef = checkAndGetClassDef(annottees: _*)
val annotateeClass: ClassDef = checkAndGetClassDef(annottees)
val resTree = appendClassBody(annotateeClass, _ => Seq(toStringTemplateImpl(argument, annotateeClass)))
val res = treeReturnWithDefaultCompanionObject(resTree, annottees: _*)
val res = returnWithCompanionObject(resTree, annottees)
printTree(argument.verbose, res)
c.Expr[Any](res)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册