About Me

My photo
Author of Groovy modules: GBench, GProf, Co-author of a Java book パーフェクトJava, Game Developer at GREE

Wednesday, May 9, 2012

Restart Groovy life with Invokedynamic

Groovy supported invokedynamic (indy) in version 2.0 beta 3 that was released on May 7. This is a short post just to show you how to restart your Groovy life with indy. Indy is a new bytecode instruction of Java SE 7 for dynamic method invocation. If you don't know about indy, read its JSR.

Okay, let's start. First of all, install an indy-supported version of Groovy. Because indy is not supported by default for now (2.0 beta 3). You can choose which of the following ways:

* Download an archive and use a groovy-indy.jar or groovy-all-indy.jar in indy/ instead of groovy.jar in lib/ or groovy-all.jar in embeddable/.

* Build from source with an option to enable indy.

ant install -DuseIndy=true -DskipTests=true

Second, compile with an option to enable indy in a way that meets your needs as follows:

* groovy

groovy --indy YourScript.groovy

* groovyc

groovyc --indy YourScript.groovy

* GroovyShell class

import org.codehaus.groovy.control.CompilerConfiguration

def conf = new CompilerConfiguration()
conf.optimizationOptions.indy = true
def shell = new GroovyShell(conf)
shell.evaluate(/* your script */)

That's all! Have a nice Groovy life!

4 comments:

  1. Don't forget to mention that you need JDK 7 for the invoke dynamic to work!

    ReplyDelete
    Replies
    1. I added a description about invokedynamic depends Java SE 7. Thank you for your comment :)

      Delete
  2. Great post. Is it possible to explain what are the benefits of the indy optimisation on Groovy ?

    ReplyDelete
    Replies
    1. I'm thinking of writing about it with some data in a next post. Thank you for reading :)

      Delete