Archive for May, 2011
With Groovy we can immediately transform the input from a
Reader object(like a file, URL or other input type) to a
Writer object (like a file, URL or other output type). Groovy adds the
transformLine(Writer, Closure) and
transformChar(Writer, Closure) methods to the
Reader class. We need to pass the
Writer object that will contain the transformed output as the first argument. The second argument is a closure with the rules for the transformation that needs to be applied.
When we write applications with concurrency needs we can use the
ReentrantReadWriteLock class to specify a lock on specific methods. Normally we have to add a field to our class of the type
ReentrantReadWriteLock and then write code to get a read lock or write lock, do our stuff and finally release the lock again. Since Groovy 1.8 we can use the AST transformation annotations duo
WithWriteLock. Groovy will add a lock field and adds the code to acquire and release the locks again in our compiled class file. We don’t have to write this code ourselves anymore.
Groovy 1.8 introduces command chain expression to further the support for DSLs. We already could leave out parenthesis when invoking top-level methods. But now we can also leave out punctuation when we chain methods calls, so we don’t have to type the dots anymore. This results in a DSL that looks and reads like a natural language.
We know how easy it is to read XML and to write XML. Recently someone asked during a workshop if it is possible to change the XML structure as easy. I found the answer on the Groovy website. It turns out we can simple use
XmlSlurper and we can traverse the structure to change existing node values and attributes. We use
replaceNode() to work with nodes using a builder syntax.
To implement a bean event pattern in Java a class must be able to register or remove event listeners. Then the list of listeners can be used to fire methods defined by the listeners. Setting up a list of listeners, providing the correct add and remove listener methods, add fire event methods is all boilerplate code we have to write each time. With Groovy we don’t have to, we simply apply the
@ListenerList annotation to a typed collection field in our class.
With Groovy 1.8 we get a lot of AST transformations. We can combine @ToString, @EqualsAndHashCode and @TupleConstructor with the
@Canonical annotation. This annotation will do all the transformations at once. If we want to customize one of the AST transformations, we add the annotation with configuration parameters extra after
Groovy has great SQL support. We can easily access databases and execute queries and statements. Since Groovy 1.8 we can even use paging when we query for data. The
rows() methods have two new parameters to define the offset and maximum results we want to be returned.
The final day of the conference, the last day I could go to informative and inspiring sessions. The whole day there will be parallel sessions, so here is a report of the sessions I have choosen to attend. We can also read the conference reports of day 1 and day 2.
After already a first great university day it was now time for the official start of the conference days. After Søren’s welcome speech it was for time for Guillaume Laforge to present his brand new presentation about Groovy 1.8. He touched on all the interesting new features we can find in Groovy in his presentation.
The new command chain expressions even allows us to leave out dots when we invoke methods so we can write even nicier and more natural language like DSLs. A lot AST transformations have been added in Groovy 1.8, closure methods, JSON supports, compilation customizers and much more. The slides are available on SlideShare.
For the first time this year Gr8Conf organized a university day. On this day 3 hour sessions where given about Groovy (in the morning) and Grails (in the afternoon). In the morning I did the session about Getting Groovy together with Søren Berg Glasius (one of the organizers of Gr8Conf). We had a lot of topics to cover and didn’t make it to cover it all during the session. Luckily we created a big document with all the topics and a lot of sample code (also from the Groovy Goodness blog series) for the attendees. So hopefully people will look into the document to learn more about the topics we couldn’t cover.