Database Reference
In-Depth Information
Introducing groovy by building this script right of the bat was perhaps a bit of a
drink from the fire hose, but hopefully no one's head is spinning too much. We used a
lot of groovy's features to very quickly create a prototype outline extractor using very
little code. We built tight, nested loops to step through the outlines, created JAPI objects
along the way, and extracted the information we need from them. An exciting thing to
see is how much the code is able to concentrate on the work that needs to be done and
not the “scaffolding” that needs to be in place in order for the work to be done. We spent
little energy creating temporary variables to loop on or in setting up those loops. We did
not define any classes. We leveraged methods and properties that groovy adds to Java
objects. The result is a program that is remarkably dense, yet readable.
Developing with groovy in this way can be a rather enjoyable activity because the
language takes care of so many of the less interesting operations for us. It is accessible
to newcomers and immediately useable by Java programmers. This example illustrates
groovy's power and ease of use, and paves the way for the next section, where we will
look more closely at this language and its features.
9.4 groovy things aBout groovy:
a Deeper look at the language
groovy (http://groovy.codehaus.org/) is a standard (JSr 241) language created by seasoned
Java developers who saw features in other languages like Python, ruby, and Smalltalk, and
wanted to bring those features into the Java world. At the same time, they wanted to main-
tain a high level of friendliness to Java itself. to accomplish these goals, they adopted a
syntax that aligns very closely with Java. In fact, most of the time, Java code can be cut and
pasted into a groovy program and executed without modification. objects from the Java
runtime Environment (JrE) or any third-party classes can be created and used in groovy
with absolute ease. For that matter, integration in the other direction is just as simple.
Classes written in groovy can be precompiled and used in Java as easily as any Java class.
In addition to this high level of language compatibility, groovy also offers many extra
features that are not available in pure Java, such as:
•  Ability to use for scripting or for fully structured application programming
•  Choice to execute code directly from source or to compile
•  Dynamic typing (optional type declarations)
•  Anonymous code blocks/code as data (closures)
•  relaxed syntax requirements (optional returns, semicolons, and parentheses)
•  Ease of use for collection types beyond simple arrays (lists, maps, ranges)
•  treatment of everything as an object (no division of types into primitives and
references)
•  Automatic accessor and mutator methods (getters and setters) on class instance
fields
•  Enhanced value substitution within strings (gStrings)
•  Integration of regular expressions
These features add up to make groovy a fun and productive language to work with,
yet it is up to the individual developer how much to use these features. Experienced
Java programmers may start off writing groovy code with a familiar Java-like style and
migrate over time to more idiomatic groovy. others may jump right in and start writ-
ing groovy-style right away.
Search WWH ::




Custom Search