Monday, October 3, 2011

JavaOne 2011: Extending Java's Reach with Groovy: Seven Scenarios

Dierk Koenig's presentation "Extending Java's Reach with Groovy: Seven Scenarios" was held in Market Street conference room in Parc 55 Hotel on late Monday morning and was a well-attended session. Koenig used IntelliJ IDEA for several of his demonstrations and showed off its ability to provide method completion for Groovy objects. Although Koenig's presentation was not intended as an introduction to Groovy, he did provide some introductory details in the course of his presentation. Between one quarter and one half of the audience raised their hand when asked if they used Groovy regularly.

Koenig started by stating something that I happen to agree with: he said that Groovy allows Java developers to take advantage of their Java knowledge in some situations in which Java might not normally be well suited. He also started by saying a recording of this presentation would be made available. He then dove into the seven scenarios covered in this presentation:

  1. Super Glue
  2. Liquid Heart
  3. Lipstick
  4. Keyhole Surgery
  5. Unlimited Openness
  6. Ghost Writer
  7. House Elf
Super Glue

The "Super Glue" category refers to using Groovy as "glue" between "existing [Java] building blocks." Groovy makes use of the countless Java libraries and frameworks. Koenig provided an example of this that uses "Java awesome XML parsing" with not-so-great API. For this example, Koenig referenced groovy.blogs() and showed a single-line of Groovy that scraped the HTML from that source (see #6 of my Groovy one-liners for an example of this and Minimalistic HTTP Clients with Groovy for a more detailed example). Koenig expanded the single line into a simple 12-line script using SwingBuilder to build up a Swing-based table with the content from the HTML-based blog. This showed off Groovy's AST Transformations functionality.

Liquid Heart

This category describes Groovy usefulness when proven business logic is implemented in Java. Koenig's example showed implementing calculation of employee bonuses in Groovy code and then invoking that Groovy code from within a Java application using Binding and GroovyShell.

Lipstick

This category involves putting a thin veneer of Groovy on top of existing Java applications. It increases code fluency and converts traditional APIs to DSL ease of use. Koenig showed an example that cannot be performed directly in Java even with bytecode manipulation. The example showed off several more features of Groovy including TupleConstructor.

Keyhole Surgery

This category involves using Groovy to be minimally invasive. Koenig's example showed using the GDK-provided findAll(Closure) to filter out negative prices in a shopping cart.

Unlimited Openness

Every line of code might be changed. This category incorporates "lessons of Perl, PHP, and Python" and perhaps the best summary is the bullet "Allow for easily changing the code without tedious setup for compilation and deployment."

Ghost Writer

Ghost Writer describes functionality available since Groovy 1.8 for "code generation without source code generation." The feature allows production of byte code for which there is no corresponding source code. I have blogged on some of these in previous posts such as Groovy 1.8 Transformations: @ToString, @EqualsAndHashCode, and @TupleConstructor and Groovy 1.8's @Canonical Transformation: Great Functionality, Less Code. Koenig's example focused on how much easier it is to use @Immutable without fear of doing it wrong and avoiding the verbosity required in Java. Koenig also referenced gcontracts in relation to this topic.

House Elf

This category is about "delegating the housework." Koenig described examples as senv, groovy install, and gradle and stated it might be used with Ant, Maven, AntBuilder, Gant, Gradle, Grails scaffolding, etc.

Other Observations

Koenig observed that the IDE provides details that the compiler does not provide when using Groovy. He stated, "You're not so much concerned if the compiler catches the error; you're concerned if the IDE catches the error."

Conclusion

Koenig's presentation demonstrated seven usage patterns that allow Java developers to supplement their Java work with Groovy. I find that my most common use of Groovy is as a scripting language in my Java development environment and so was not surprised to see many of the ways I use Groovy discussed in this presentation. However, Koenig presented a couple of ideas that are new to me and that I look forward to adding to my Java development toolbox. You can find Koenig's initial impressions of JavaOne 2011 in his post JavaOne 2011 Arrival on his blog.