Java Reference
In-Depth Information
The integration is already here through the use of the library class and various Java meth-
ods. I needed to pass the address to Google in URL-encoded form. To do that I ran each
element of the address (street, city, and state) through the
java.net.URL-Encoder
,
using its
encode
method. In other words, the Groovy script used a Java library class and
called one of its methods.
Lessons learned (integration)
1
. Groovy scripts can be called with Java alone using the JSR 223 script engine.
2
. The Groovy
Eval
class makes calling scripts involving zero, one, two, or three
arguments simple.
3
. The
GroovyShell
and
Binding
classes are used to programmatically set input
variables, invoke a script, and retrieve its result.
4
. The easiest way to call Groovy from Java is to make a Groovy class, compile it,
instantiate it in Java, and call the methods as usual.
The combination of Java and Groovy is also emphasized in
Figure 3.3
, shown with the ori-
ginal listing. In that figure each Java method and each Groovy method is indicated with
arrows.
The fact that the script mixes both Java and Groovy is true of practically any Groovy
script. Groovy rests on the foundation of the Java libraries. It enhances those libraries, as
you'll see in
section 4.3
on the Groovy JDK, but there's no need to re-invent the flat tire.
[
8
]
Groovy is perfectly happy to use any Java classes you supply, and it makes many of them
better.