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!

Friday, May 4, 2012

Exploring JavaFX 2 - Accessing application parameters

JavaFX provides Application.getParameters() as a way to access application parameters from an Application class object. For example, if a user sets a width and a height as the options and a message as an argument in GNU format:
java AccessApplicationParametersDemo --width=200 --height=100 hello
the application can receive the parameters as follows:
public class AccessApplicationParametersDemo {

    public static void main(String[] args) {
        Application.launch(UsingGetParametersApplication.class, args);

public class UsingGetParametersApplication extends Application {

    public void start(Stage stage) throws Exception {
        Map opts = getParameters().getNamed();
        System.out.println("width=" + opts.get("width"));
        System.out.println("height=" + opts.get("height"));
        List args = getParameters().getUnnamed(); 
        System.out.println("message=" + args.get(0));

But getParameters() is not functional for now (v2.2-b06) because the option parser is poor. It does not support short form options and POSIX format and it does not allow any of the following formats:
-w=200 -h=100
--width 200 --height 100
-w 200 -h 100
Should I customize getParemters()? What do you do if you hear accessing the implementation class is hard coded in it and the method is final? I mean, it is not good choice to get options or arguments directly from getParameters() for now. I recommend that you let getParameters() to act a transporter and ask Apache Commons CLI to take the main work:
public class UsingCommonsCliApplication extends Application {

    public void start(Stage stage) throws Exception {
        Options options = new Options();
        options.addOption("w", "width", true, "");
        options.addOption("h", "height", true, "");
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(
                new String[getParameters().getRaw().size()]
        System.out.println("width=" + cmd.getOptionValue("width")); 
        System.out.println("height=" + cmd.getOptionValue("height")); 
        System.out.println("message=" + cmd.getArgList().get(0));

The problem might will be solved sooner or later because JavaFX team's answer to the problem is "We could consider this for a future version". In the first place, such a feature must be needed only by Applet or JWS that receives parameters in the fixed format, thus it seems better that JavaFX has application classes for those environments like AppletApplication or JWSApplication and make only them to have the feature.