开发者

AST Transformation in Groovy: Create Macro to Give a Basic Constructor

I would like to make a macro that would essentially expand this:

@WithB开发者_运维问答asicConstructor
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 ?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜