Java Reference
In-Depth Information
input variables are called x , y ,and z ,which might be asking too much. Still, it'sinteresting
that this mechanism exists at all.
In addition to illustrating the mechanics of calling Groovy scripts from Java, the tests also
demonstrate operator overloading in the String class. The minus operator in Groovy cor-
responds to the minus method in String . Its implementation to remove the first instance
of its argument from the given string is used with strings to remove instances of substrings.
In Groovy, strings can be contained within either single or double quotes. Single-quoted
strings are regular Java strings, and double-quoted strings are parameterized strings, diplo-
matically called Groovy strings, but formally called, unfortunately, GString s. [ 6 ]
6 To make matters worse, simple parameters are injected into GString s using a dollar sign. This has led to far
too many “insert a $ into a GString” jokes. To me, this is a clear demonstration that we don't have enough women
in computer science. Don't you think that if there had been one woman on the team at the time, she could have
said, “Hey, that's a funny joke, but let's not build it into the standard library that's going to be used by everybody
forever?” After all, it's hard enough to get a language named Groovy taken seriously by the Fortune 500 without
going there, too. For my part, I call them Groovy strings, which is what the class should have been called all along.
It is a funny joke, though—for about 10 minutes.
The process of using Eval from Java is shown in figure 3.4 .
Figure 3.4. Java calls the me, x, xy , or xyz method in the Groovy Eval class to execute a script.
The Eval class is convenient and simple, but often it's too simple. It rests on a more
powerful foundation, the GroovyShell class, which I'll discuss next.
3.2.3. Working with the GroovyShell class
The GroovyShell class is used for scripts that aren't restricted to the special cases de-
scribed in the previous section on Eval . The class groovy.lang.GroovyShell can
be used to execute scripts, particularly when combined with a Binding .
 
 
Search WWH ::




Custom Search