BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Just the Cure, More Groovy

Just the Cure, More Groovy

Leia em Português

This item in japanese

Bookmarks

Actor Christopher Walken once performed a skit on Saturday Night Live in which he proclaimed he needed more cowbell to get rid of his "fever".  In the same light, the Groovy team has certainly heard the calls for more Groovy and have recently released 1.6 of their award winning language.  There are a slew of new features mentioned in the release including,

  • great runtime performance improvements
  • multiple assignments - optional return in if/else and try/catch blocks
  • AST transformations and all the provided transformation annotations like @Singleton, @Lazy, @Immutable, @Delegate and friends
  • the Grape module and dependency system and its @Grab transformation
  • various Swing builder improvements, thanks to the Swing / Griffon (http://griffon.codehaus.org) team
  • as well as several Swing console improvements
  • the integration of JMX builder
  • JSR-223 scripting engine built-in
  • various metaprogramming improvements, like the EMC DSL, per-instance metaclasses even for POJOs, and runtime mixins

One of the primary focuses in this release was on performance and the Groovy team claims to have made significant improvements ranging from 150% to 460%.  Another feature included in this release was the official integration of the JMX builder, which is just another example of community efforts helping to improve Groovy.

Below are some examples of several of the new features including multiple assignments and AST transformations.

// this class' properties are immutable once the object is constructed
@Immutable final class ServerConfig {
  String url
  int port
}

def getServerInfo() {
  ['http://home.net', 8080]
}

// attempts to set a property on an Immutable object
def setUrl(config, newUrl) {
  try {
    config.url = newUrl
  }
  catch (ReadOnlyPropertyException ex) {
    ex
  }
}

// multiple assignment
def (url, port) = getServerInfo()

assert url == 'http://home.net'
assert port == 8080

def config = new ServerConfig(url, port)

assert config.url == url
assert config.port == port

// try to change the property on the Immutable object
def result = setUrl(config, 'www.google.com')

// verify the property change failed
assert result instanceof ReadOnlyPropertyException


The example above shows how the @Immutable AST transformation provides a very simple way of creating a read-only object.  The example also demonstrates using the new "multiple assignments" feature.  For more information about the AST transformations you can visit the Groovy user guide, currently the section only covers the @Immutable transformation.

Now that you've seen a quick overview of what Groovy 1.6 has to offer, check out What's New in Groovy 1.6, an in-depth article about Groovy 1.6 written for InfoQ by the Groovy Project Manager, Guillaume LaForge.  Guillaume covers each of the new features and provides plenty of code examples which help clarify all of the new capabilities.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Grape and grab are pretty nice

    by andrej koelewijn,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    The build in dependency management makes life a lot easier, especially for scripting. Nice example on my blog with Apache Camel: Groovy and Grape - easiest way to send gtalk message with Apache Camel?.

  • 1.6 is really more effective than 1.5.7

    by qiu james,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    But groovy need to improve startup speed.
    gant is too slow so we have to rewrite it in python, or beanshell.

  • congrats

    by Roger Pack,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    congrats to the groovy guys.

  • JRuby or Groovy?

    by Joshua Partogi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    That is the question now.

  • Re: JRuby or Groovy?

    by Guillaume Laforge,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    That is the question now.


    Groovy, of course ;-)

  • Re: JRuby or Groovy?

    by Marcio Garcia,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Why not both of them? :)

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT