Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Scala Macro Tools
提交
011e058b
S
Scala Macro Tools
项目概览
梦境迷离
/
Scala Macro Tools
上一次同步 1 年多
通知
8
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Scala Macro Tools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
011e058b
编写于
8月 04, 2021
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor
上级
07e15e9c
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
33 addition
and
33 deletion
+33
-33
src/main/scala/io/github/dreamylost/macros/AbstractMacroProcessor.scala
.../io/github/dreamylost/macros/AbstractMacroProcessor.scala
+11
-15
src/main/scala/io/github/dreamylost/macros/applyMacro.scala
src/main/scala/io/github/dreamylost/macros/applyMacro.scala
+5
-3
src/main/scala/io/github/dreamylost/macros/builderMacro.scala
...main/scala/io/github/dreamylost/macros/builderMacro.scala
+1
-1
src/main/scala/io/github/dreamylost/macros/constructorMacro.scala
.../scala/io/github/dreamylost/macros/constructorMacro.scala
+5
-3
src/main/scala/io/github/dreamylost/macros/equalsAndHashCodeMacro.scala
.../io/github/dreamylost/macros/equalsAndHashCodeMacro.scala
+3
-3
src/main/scala/io/github/dreamylost/macros/jacksonEnumMacro.scala
.../scala/io/github/dreamylost/macros/jacksonEnumMacro.scala
+3
-3
src/main/scala/io/github/dreamylost/macros/jsonMacro.scala
src/main/scala/io/github/dreamylost/macros/jsonMacro.scala
+2
-2
src/main/scala/io/github/dreamylost/macros/logMacro.scala
src/main/scala/io/github/dreamylost/macros/logMacro.scala
+1
-1
src/main/scala/io/github/dreamylost/macros/toStringMacro.scala
...ain/scala/io/github/dreamylost/macros/toStringMacro.scala
+2
-2
未找到文件。
src/main/scala/io/github/dreamylost/macros/AbstractMacroProcessor.scala
浏览文件 @
011e058b
...
...
@@ -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}"
)
}
/**
...
...
src/main/scala/io/github/dreamylost/macros/applyMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
}
...
...
src/main/scala/io/github/dreamylost/macros/builderMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
}
...
...
src/main/scala/io/github/dreamylost/macros/constructorMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
)
}
...
...
src/main/scala/io/github/dreamylost/macros/equalsAndHashCodeMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
)
}
...
...
src/main/scala/io/github/dreamylost/macros/jacksonEnumMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
"""
)
}
}
...
...
src/main/scala/io/github/dreamylost/macros/jsonMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
}
...
...
src/main/scala/io/github/dreamylost/macros/logMacro.scala
浏览文件 @
011e058b
...
...
@@ -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
)
}
...
...
src/main/scala/io/github/dreamylost/macros/toStringMacro.scala
浏览文件 @
011e058b
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录