提交 07e15e9c 编写于 作者: 梦境迷离's avatar 梦境迷离

refactor

上级 7bef18a3
......@@ -373,7 +373,7 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
rhs: Tree
) {
// def typeName: TypeName = symbol.name.toTypeName // unused
def typeName: TypeName = symbol.name.toTypeName
def symbol: c.universe.Symbol = paramType.typeSymbol
}
......@@ -384,7 +384,7 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
* @param params The list of params retrieved from the class
* @return An Sequence of tuples where each tuple encodes the string name and string type of a field
*/
def accessors(params: Seq[ValDef]): Seq[Accessor] = {
def accessors(params: Seq[Tree]): Seq[Accessor] = {
params.map {
case ValDef(mods, name: TermName, tpt: Tree, rhs) => {
Accessor(mods, name, c.typecheck(tq"$tpt", c.TYPEmode).tpe, rhs)
......@@ -405,19 +405,31 @@ abstract class AbstractMacroProcessor(val c: whitebox.Context) {
/**
* Generate the specified syntax tree and assign it to the tree definition itself.
* Used only when you modify the definition of the class itself. Such as add method/add field
* Used only when you modify the definition of the class itself. Such as add method/add field.
*
* @param classDecl
* @param classInfoAction
* @param classInfoAction Content body added in class definition
* @return
*/
def appendedBody(classDecl: ClassDef, classInfoAction: ClassDefinition => Seq[Tree]): c.universe.ClassDef = {
def appendClassBody(classDecl: ClassDef, classInfoAction: ClassDefinition => Seq[Tree]): c.universe.ClassDef = {
val classInfo = mapToClassDeclInfo(classDecl)
val ClassDef(mods, name, tparams, impl) = classDecl
val Template(parents, self, body) = impl
ClassDef(mods, name, tparams, Template(parents, self, body ++ classInfoAction(classInfo)))
}
/**
* Modify the method body of the method tree.
*
* @param defDef
* @param defBodyAction Method body of final result
* @return
*/
def mapToMethodDef(defDef: DefDef, defBodyAction: => Tree): c.universe.DefDef = {
val DefDef(mods, name, tparams, vparamss, tpt, rhs) = defDef
DefDef(mods, name, tparams, vparamss, tpt, defBodyAction)
}
private[macros] case class ClassDefinition(
className: TypeName,
classParamss: List[List[Tree]],
......
......@@ -99,7 +99,7 @@ object constructorMacro {
}
override def createCustomExpr(classDecl: ClassDef, compDeclOpt: Option[ModuleDef] = None): Any = {
val resTree = appendedBody(
val resTree = appendClassBody(
classDecl,
classInfo => Seq(getThisMethodWithCurrying(classInfo.classParamss, classInfo.body)))
c.Expr(resTree)
......
......@@ -119,7 +119,7 @@ object equalsAndHashCodeMacro {
getInternalFieldsTermNameExcludeLocal(classDefinition.body)
}
val classDefinition = mapToClassDeclInfo(classDecl)
val res = appendedBody(classDecl, classInfo =>
val res = appendClassBody(classDecl, classInfo =>
getEqualsMethod(classDefinition.className, map(classInfo),
classDefinition.superClasses, classDefinition.body) ++
List(getHashcodeMethod(map(classInfo), classDefinition.superClasses))
......
......@@ -49,7 +49,7 @@ object jacksonEnumMacro {
}
private def getTypeTermName(valDefTree: Tree): c.universe.TermName = {
val safeValDef = accessors(Seq(valDefTree.asInstanceOf[ValDef])).head
val safeValDef = accessors(Seq(valDefTree)).head
getTypeTermName(safeValDef)
}
......@@ -63,8 +63,8 @@ object jacksonEnumMacro {
}
private def replaceAnnotation(valDefTree: Tree): Tree = {
val safeValDef = accessors(Seq(valDefTree.asInstanceOf[ValDef])).head
if (safeValDef.symbol.name.toTermName.toString == "Value") {
val safeValDef = accessors(Seq(valDefTree)).head
if (safeValDef.typeName.decodedName.toString == "Value") {
// duplication should be removed
val mods = safeValDef.mods.mapAnnotations(f => {
if (!f.toString().contains("JsonScalaEnumeration") &&
......
......@@ -43,18 +43,18 @@ object synchronizedMacro {
}
override def impl(annottees: c.universe.Expr[Any]*): c.universe.Expr[Any] = {
val resTree = annottees map (_.tree) match {
// Match a method, and expand.
case _@ q"$modrs def $tname[..$tparams](...$paramss): $tpt = $expr" :: _ =>
if (extractArgumentsDetail._2 != null) {
val resTree = annottees.map(_.tree) match {
case (defDef: DefDef) :: Nil =>
val body = if (extractArgumentsDetail._2 != null) {
if (extractArgumentsDetail._2 == "this") {
q"""def $tname[..$tparams](...$paramss): $tpt = ${This(TypeName(""))}.synchronized { $expr }"""
q"${This(TypeName(""))}.synchronized { ${defDef.rhs} }"
} else {
q"""def $tname[..$tparams](...$paramss): $tpt = ${TermName(extractArgumentsDetail._2)}.synchronized { $expr }"""
q"${TermName(extractArgumentsDetail._2)}.synchronized { ${defDef.rhs} }"
}
} else {
c.abort(c.enclosingPosition, "Invalid args, lockName cannot be a null!")
}
mapToMethodDef(defDef, body)
case _ => c.abort(c.enclosingPosition, "Invalid annotation target: not a method")
}
printTree(extractArgumentsDetail._1, resTree)
......
......@@ -73,7 +73,7 @@ object toStringMacro {
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 resTree = appendedBody(annotateeClass, _ => Seq(toStringTemplateImpl(argument, annotateeClass)))
val resTree = appendClassBody(annotateeClass, _ => Seq(toStringTemplateImpl(argument, annotateeClass)))
val res = treeReturnWithDefaultCompanionObject(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.
先完成此消息的编辑!
想要评论请 注册