Wednesday, February 7, 2018

Using Groovy Transform Objects With a Java Constructor

If you've used much groovy you probably love Transforms. The ability to declare a simple object and get all the nice groovy features automatically is great.


However if you've ever had to mix Java code and Groovy code in the same project you may have noticed a problem with transforms. They don't generate a constructor that is compatible with Java code. This is especially problematic if you're using the @Immutable transform since there is no now way to effectively instantiate your object.


The best work around for this problem that I've found is to do something like this. You add a static builder method within the object itself (or any other groovy class in your project) so you can just invoke that instead of the constructor. It's not pretty but it works.


Let me know if you have a better solution to this problem.

Using Groovy Transform Objects With a Java Constructor

If you've used much groovy you probably love Transforms. The ability to declare a simple object and get all the nice groovy features aut...