Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

296

APPENDIX B Groovy by feature

B.1 Scripts and the traditional example

Assuming you already have Groovy installed,2 I’ll start with the traditional “Hello, World!” program, as shown here:

println 'Hello, Groovy!'

That’s the whole program. In Java, you need a main method inside a class, and inside the main method you call System.out.println to write to the console. Java developers are used to it, but there are roughly 8 to 10 different object-oriented concepts involved, depending on how you count them.3 In Groovy, the whole program is a single line.

To demonstrate, consider one of the two execution environments that come with Groovy, the groovysh command, which starts the Groovy shell. The Groovy shell is a REPL4 that allows you to execute Groovy code a line at a time. All of the lines in the following listing produce the same result.

Listing B.1 Running “Hello, World!” in the Groovy shell

$ groovysh

 

 

 

 

 

 

 

 

Groovy Shell (2.1.5, JVM: 1.7.0_11)

 

 

 

 

 

 

 

 

Type 'help' or '\h' for help.

 

 

 

 

 

 

 

Java syntax

----------------------------------------------

 

 

 

 

 

 

 

groovy:000> System.out.println("Hello, World!");

 

 

 

 

 

 

 

 

 

 

Hello, World!

 

 

 

 

 

 

Semicolons

===> null

 

 

 

 

 

 

groovy:000> System.out.println("Hello, World!")

 

 

 

are optional

 

 

 

 

 

Hello, World!

 

 

 

Default Groovy

===> null

 

 

 

groovy:000> println("Hello, World!")

 

 

 

method

 

 

 

 

 

 

 

 

Hello, World!

Optional

===> null

 

 

groovy:000> println "Hello, World!"

 

 

 

 

parentheses

 

 

 

 

 

 

 

 

 

 

 

 

Hello, World!

 

 

 

 

 

 

 

 

===> null

 

 

 

 

 

 

 

 

groovy:000> println 'Hello, World!'

 

 

Single-quoted

Hello, World!

 

 

string

 

 

 

 

 

===> null

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

groovy:000>

 

 

 

 

 

 

 

 

In each case the println method prints to the console and returns null. When there’s no ambiguity, the parentheses can be omitted. Semicolons work as in Java, but they’re optional.

2See appendix A for details.

3A rough count includes classes, methods, strings, arrays, public access, static methods and attributes, void return types, overloaded methods like println, and more. It’s no accident that Bruce Eckel’s Thinking in Java (Prentice-Hall, 2002) takes over 100 pages just to get to his first “Hello, World” program.

4Read-Eval-Print Loop; see http://en.wikipedia.org/wiki/REPL for details.

www.it-ebooks.info

Scripts and the traditional example

297

This is an example of a Groovy script. A script is a code listing that doesn’t explicitly include a class definition. In Java, everything has to be inside a class. Groovy is able to work with both scripts and classes.

A Groovy script is a form of syntactic sugar.5 A class is, in fact, involved. If I compile this script and then run the javap command on it, I get the following response:

>groovyc hello_world.groovy

>javap hello_world

Compiled from "hello_world.groovy"

public class hello_world extends groovy.lang.Script{ public static transient boolean __$stMC;

public static long __timeStamp;

public static long __timeStamp__239_neverHappen1309544582162; public hello_world();

public hello_world(groovy.lang.Binding); public static void main(java.lang.String[]); public java.lang.Object run();

...

There are about 30 more lines of output from the javap command, mostly involving superclass methods. The interesting part is that the groovy command generates a class called hello_world, along with a pair of constructors and a main method. The class is generated at compile time and extends a class from the Groovy library called groovy.lang.Script. In effect, scripts in Groovy become classes in Java, where the code in the script ultimately (after a few layers of indirection) is executed by the main method. I don’t want to give the impression that Groovy is generating Java, however. Groovy code is compiled directly into bytecodes for the JVM.

COMPILED GROOVY Groovy is compiled, not interpreted. It’s not a code generator; the compiler generates Java bytecodes directly.

Because the bytecodes run on the JVM, you can execute Groovy scripts using the java command as long as you include the necessary JAR file in your classpath:

> java –cp .;%GROOVY_HOME%\embeddable\groovy-all-2.1.5.jar hello_world Hello, World!

EXECUTING GROOVY At runtime, Groovy is just another JAR file. As long as the groovy-all JAR file is in the classpath, Java is perfectly happy to execute compiled Groovy code.

The groovy command is used to execute Groovy programs. It can be used with either the compiled code (similar to the java command) or Groovy source. If you use the source, the groovy command first compiles the code and then executes it.

5Syntactic sugar is syntax that simplifies writing code but doesn’t change anything under the hood. There may be some evidence that an overuse of syntactic sugar leads to syntactic diabetes.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]