Fluent AST Transformation
I've been tinkering with my own Groovy AST Transformations of late, largely as a masochis.. academic exercise. They're incredibly powerful, but sisyphean to learn and implement. Nevertheless, by some amalgam of code and black magic, I've managed to construct a few working examples. For that, I have to thank Hamlet D'Arcy, whose efforts on the AstBuilder undoubtedly reduced the learning-curve to something approaching my pain threshold.
Most of my projects have been rather trivial; automatic method timers and the like (Nice for ProjectEuler problems, at least). However, I have recently made something (while still trivial) that may actually be of use to someone other than myself..
Although neither as idiomatic, nor as robust as it surely should/could be, I've created a simple AST transformation that automatically implements a Fluent Interface on the target class(es). It's a very basic idea, with an equally basic implementation: By automatically creating setters that return the instance of the property's declaring class, one can chain methods together into a more "natural flow."
For example:
The transformation also plays nice with Groovy's built-in @Delegate, which can produce some interesting results:
Of course, Groovy already possesses all manner of convenience when it comes to properties, so the true utility of this endeavor is arguable. Using "with" for instance, would also cut down on the usual property noise, though by a an entirely different mechanism.
Nevertheless, I've created a repository from which the code may be obtained by anyone so inclined to toy with it. Use it as you will. I welcome any comments or criticism - I'm still very much a novice when it comes to AST, so any suggestions or corrections would be appreciated.
I hope someone finds it useful (or at least interesting).
Most of my projects have been rather trivial; automatic method timers and the like (Nice for ProjectEuler problems, at least). However, I have recently made something (while still trivial) that may actually be of use to someone other than myself..
Although neither as idiomatic, nor as robust as it surely should/could be, I've created a simple AST transformation that automatically implements a Fluent Interface on the target class(es). It's a very basic idea, with an equally basic implementation: By automatically creating setters that return the instance of the property's declaring class, one can chain methods together into a more "natural flow."
For example:
import northover.* @Fluent class Person{ def first def last int telephone } def person = new Person().setFirst("Joe").setLast("Smith").setTelephone(123456789)
The transformation also plays nice with Groovy's built-in @Delegate, which can produce some interesting results:
import northover.* @Fluent class Example{ @Delegate String first boolean modified = false } assert new Example().setFirst(" A String ").setModified(true).trim() == "A String"
Of course, Groovy already possesses all manner of convenience when it comes to properties, so the true utility of this endeavor is arguable. Using "with" for instance, would also cut down on the usual property noise, though by a an entirely different mechanism.
Nevertheless, I've created a repository from which the code may be obtained by anyone so inclined to toy with it. Use it as you will. I welcome any comments or criticism - I'm still very much a novice when it comes to AST, so any suggestions or corrections would be appreciated.
I hope someone finds it useful (or at least interesting).