Groovy 中的 AST 转换:创建宏以提供基本构造函数
我想制作一个宏,本质上将其扩展
@WithBasicConstructor
class Person {
private String name
private String address
}
为:
class Person {
private String name
private String address
Person(String name, String address) {
this.name = name
this.address = address
}
}
我一直在阅读 @Immutable
的代码,以了解它是如何完成的。有人用过新的 AstBuilder 吗?
有什么想法如何实施吗? AstBuilder 对于 string/code/spec 是否有首选选项?
I would like to make a macro that would essentially expand this:
@WithBasicConstructor
class Person {
private String name
private String address
}
into this:
class Person {
private String name
private String address
Person(String name, String address) {
this.name = name
this.address = address
}
}
I've been reading through the code for @Immutable
to get a feel for how it is done. Has anyone used the new AstBuilder?
Any ideas how to implement this? Is there a preferred option between AstBuilder for string/code/spec ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用(或复制)groovy 1.8 中的
@groovy.transform.TupleConstructor
。关于偏好...我喜欢
buildFromSpec
,它会减少意外情况。但我建议您尝试一下
buildFromCode
,测试它的局限性和怪癖,并尝试一下所有这些。You could use (or copy)
@groovy.transform.TupleConstructor
from groovy 1.8.Regarding preferences... I like the
buildFromSpec
, it leads to fewer surprises.But I'd suggest you try the
buildFromCode
, test its limitations and quirks, play a little with all of them.