-Groovyのプレパース

前は以下のようにやってました。

final String source = "out.println('done');"
Binding binding = new Binding();
binding.setVariable("out", System.out);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(script);

しかし、調べるとあらかじめスクリプトのパースをさせとくことできるのですね。

final String source = "out.println('done');"
GroovyShell shell = new GroovyShell();
Script script = shell.parse(source);
Binding binding = script.getBinding();
binding.setVariable("out", System.out);
script.run();

パース済みScriptをプールしておけば、java.sql.PreparedStatementみたいに後からバインド変数を設定できるから、実行時にパース処理をさせなくても良くて、これはいいですね。どうもGroovy、パースは重いが、実行は軽いようなのでステキです。