Difference between @Delegate and @Mixin AST transformations in Groovy

无人久伴 提交于 2019-12-03 10:39:58

The behavior is similar, but @Delegate and @Mixin are implemented completely differently.

@Delegate generates accessor methods at compile time. Superhero will have a method called walk() that simply calls person.walk(). The generated methods can be seen by dumping the Superhero class file with javap.

@Mixin, on the other hand just creates a small stub that mixs in the Person methods at runtime. It uses groovy's meta-object protocol to allow Superhero to respond to Person's methods. In this case, you won't see any Person methods in Superhero.class.

@Delegate has the advantage that the methods are callable from Java and it avoids doing a dynamic invocation. In addition, @Mixin can't augment the class with properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!