Archive for May, 2011
Groovy Goodness: Transforming Reader Input to Writer Output
May 31st
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.
Groovy Goodness: Apply Read and Write Locking
May 28th
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 WithReadLock and 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 Goodness: Command Chain Expressions for Fluid DSLs
May 27th
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.
Groovy Goodness: Change XML Structure
May 26th
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 appendNode() and replaceNode() to work with nodes using a builder syntax.
Groovy Goodness: Add Java-style Listener Support with ListenerList Annotation
May 25th
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.
Groovy Goodness: Canonical Annotation to Create Mutable Class
May 24th
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 @Canonical.
Gr8Conf 2011 Conference Report: Day 2
May 20th
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.
Gr8Conf 2011 Conference Report: Day 1
May 20th
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.