Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Groovy Quick Start


[ 30 ]

This launches the following Groovy console:


In the preceding GroovyConsole, we are trying out a handy feature of Groovy,
which is the ability to find class methods on the fly. This simple line of code exploits
a few of the Groovy language features. Object.methods is analogous to calling
getMethods() for the Object class. Instead of a method array, a Groovy list is
returned containing the method objects. We use the built-in iteration method to pass
each element in the list to the closure that prints the method details. We will go over
all of these language features in depth later.


On the other hand, GroovyShell, the class on which groovysh is built, is an
extremely useful class. Later in this book, we will use GroovyShell to evaluate DSL
scripts on the fly, but other uses can be made of it including building an on-the-fly
Groovy interpreter into your application, if necessary.


The Groovy console has several other features, such as the ability to select a part
of the buffer and run it, and also has a useful object browser. This allows you to
inspect the last result object from the console. Combined with the ability to select
part of the display buffer and run it independently, the console is the ideal sandbox
for playing with code snippets—whether debugging existing code or learning the
Groovy language.


http://www.ebook3000.com
Free download pdf