Java Reference
In-Depth Information
and configurations. Allthough all of that is interesting, it doesn't really add a lot to the in-
tegration story, so I'll refer you to Dierk Koenig's most excellent Groovy in Action for de-
tails.
The Hard Way
Use the ScriptEngine class from Java, or the Eval and GroovyShell classes from
Groovy, along with a Binding if necessary, to call Groovy scripts from Java.
Between the ScriptEngine , Eval ,and GroovyShell classes, hopefullyyou'llagree
that there are a variety of ways to execute Groovy scripts from Java. Collectively I still
refer to this as “the hard way,” though it isn't terribly hard, but it's awfully indirect com-
pared to the easy way. From now on I'll stop trying to maintain the artificial separation of
Java code from Groovy code. In order to make progress all I need to do is put the Groovy
code into a class.
3.2.4. Calling Groovy from Java the easy way
Allthetechniques I'vediscussed sofar—usingtheJSR223 ScriptEngine ,orusingthe
Groovy API classes Eval and GroovyShell —work just fine but feel overly complic-
ated. Groovy is supposed to simplify your life, so although the mechanisms shown in the
previous section all work, for most use cases there's an easier way.
The easiest way to call Groovy from Java is to put the Groovy code in a class and compile
it. Then Java code can instantiate the class and invoke its methods the normal way.
The Easy Way
To call Groovy from Java, put the Groovy code in a class, compile it as usual, and then
instantiate it and invoke methods as though it was Java.
Let's return, once again, to the geocoder. This time, however, I'll refactor it into a class that
can be instantiated, with methods that can be invoked from outside. The process is shown
in figure 3.6 .
 
Search WWH ::




Custom Search