Groovy Tutorial

1 Introduction

1.1 Why Dynamic Languages?
1.2 What’s Groovy?
1.3 Why Groovy?
1.4 What’s in This Book?
1.5 Who Is This Book For?
1.6 Acknowledgments

2 Getting Started

2.1 Getting Groovy
2.2 Installing Groovy
2.3 Test-Drive Using groovysh
2.4 Using groovyConsole
2.5 Running Groovy on the Command Line
2.6 Using an IDE
2.7 Creating first program Hello World
2.8 Tokens in Groovy

3 Basic Syntax

3.1 Comments in Groovy
3.2 Semicolons
3.3 Identifiers
3.4 Keywords
3.5 Whitespaces
3.6 Literals

4 Data Types

4.1 Built-in Data Types
4.2 Bound values
4.3 Class Numeric

5 Variables

5.1 Variable Declarations
5.2 Naming Variables
5.3 Printing Variables

6 Operators

6.1 Arithmetic Operators
6.2 Relational operators
6.3 Logical Operators
6.4 Bitwise Operators
6.5 Assignment operators
6.6 Range Operators
6.7 Operator Precedence

Loops and Control Statements

7.1.1 While Statement
7.1.2 for Statement
7.1.3 for-in Statement
7.2 Loop Control Statements
7.2.1 Break Statement
7.2.2 Continue Statement


3 Groovy for the Java Eyes 37
3.1 From Java to Groovy . . . . . . . . . . . . . . . . . . . . 37
3.2 JavaBeans . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.3 Optional Parameters . . . . . . . . . . . . . . . . . . . . 50
3.4 Implementing Interfaces . . . . . . . . . . . . . . . . . . 51
3.5 Groovy boolean Evaluation . . . . . . . . . . . . . . . . 55
3.6 Operator Overloading . . . . . . . . . . . . . . . . . . . . 56
3.7 Support of Java 5 Language Features . . . . . . . . . . 59
3.8 Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
CONTENTS 10
4 Dynamic Typing 75
4.1 Typing in Java . . . . . . . . . . . . . . . . . . . . . . . . 75
4.2 Dynamic Typing . . . . . . . . . . . . . . . . . . . . . . . 78
4.3 Dynamic Typing != Weak Typing . . . . . . . . . . . . . 79
4.4 Design by Capability . . . . . . . . . . . . . . . . . . . . 80
4.5 Optional Typing . . . . . . . . . . . . . . . . . . . . . . . 86
4.6 Types in Groovy . . . . . . . . . . . . . . . . . . . . . . . 86
4.7 Multimethods . . . . . . . . . . . . . . . . . . . . . . . . 87
4.8 Dynamic: To Be or Not to Be? . . . . . . . . . . . . . . . 91
5 Using Closures 92
5.1 Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
5.2 Use of Closures . . . . . . . . . . . . . . . . . . . . . . . 96
5.3 Working with Closures . . . . . . . . . . . . . . . . . . . 98
5.4 Closure and Resource Cleanup . . . . . . . . . . . . . . 98
5.5 Closures and Coroutines . . . . . . . . . . . . . . . . . . 101
5.6 Curried Closure . . . . . . . . . . . . . . . . . . . . . . . 102
5.7 Dynamic Closures . . . . . . . . . . . . . . . . . . . . . . 105
5.8 Closure Delegation . . . . . . . . . . . . . . . . . . . . . 107
5.9 Using Closures . . . . . . . . . . . . . . . . . . . . . . . 110
6 Working with Strings 111
6.1 Literals and Expressions . . . . . . . . . . . . . . . . . . 111
6.2 GString Lazy Evaluation Problem . . . . . . . . . . . . . 114
6.3 Multiline String . . . . . . . . . . . . . . . . . . . . . . . 118
6.4 String Convenience Methods . . . . . . . . . . . . . . . 120
6.5 Regular Expressions . . . . . . . . . . . . . . . . . . . . 121
7 Working with Collections 124
7.1 Using List . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
7.2 Iterating Over an ArrayList . . . . . . . . . . . . . . . . . 126
7.3 Finder Methods . . . . . . . . . . . . . . . . . . . . . . . 129
7.4 Collections’ Convenience Methods . . . . . . . . . . . . 130
7.5 Using Map . . . . . . . . . . . . . . . . . . . . . . . . . . 133
7.6 Iterating Over Map . . . . . . . . . . . . . . . . . . . . . 135
7.7 Map Convenience Methods . . . . . . . . . . . . . . . . . 137
CONTENTS 11
II Using Groovy 140
8 Exploring the GDK 141
8.1 Object Extensions . . . . . . . . . . . . . . . . . . . . . . 141
8.2 Other Extensions . . . . . . . . . . . . . . . . . . . . . . 147
9 Working with XML 155
9.1 Parsing XML . . . . . . . . . . . . . . . . . . . . . . . . . 155
9.2 Creating XML . . . . . . . . . . . . . . . . . . . . . . . . 160
10 Working with Databases 164
10.1 Connecting to a Database . . . . . . . . . . . . . . . . . 165
10.2 Database Select . . . . . . . . . . . . . . . . . . . . . . . 166
10.3 Transforming Data to XML . . . . . . . . . . . . . . . . . 167
10.4 Using DataSet . . . . . . . . . . . . . . . . . . . . . . . . 168
10.5 Inserting and Updating . . . . . . . . . . . . . . . . . . . 169
10.6 Accessing Microsoft Excel . . . . . . . . . . . . . . . . . 169
11 Working with Scripts and Classes 172
11.1 The Melting Pot of Java and Groovy . . . . . . . . . . . 172
11.2 Running Groovy . . . . . . . . . . . . . . . . . . . . . . . 173
11.3 Using Groovy Classes from Groovy . . . . . . . . . . . . 174
11.4 Using Groovy Classes from Java . . . . . . . . . . . . . 175
11.5 Using Java Classes from Groovy . . . . . . . . . . . . . 176
11.6 Using Groovy Scripts from Groovy . . . . . . . . . . . . 178
11.7 Using Groovy Scripts from Java . . . . . . . . . . . . . . 180
11.8 Ease of Integration . . . . . . . . . . . . . . . . . . . . . 182
III MOPping Groovy 183
12 Exploring Meta-Object Protocol (MOP) 184
12.1 Groovy Object . . . . . . . . . . . . . . . . . . . . . . . . 185
12.2 Querying Methods and Properties . . . . . . . . . . . . 190
12.3 Dynamically Accessing Objects . . . . . . . . . . . . . . 192
13 Intercepting Methods Using MOP 194
13.1 Intercepting Methods Using GroovyInterceptable . . . . 194
13.2 Intercepting Methods Using MetaClass . . . . . . . . . 197
CONTENTS 12
14 MOP Method Injection and Synthesis 202
14.1 Injecting Methods Using Categories . . . . . . . . . . . 203
14.2 Injecting Methods Using ExpandoMetaClass . . . . . . 208
14.3 Injecting Methods into Specific Instances . . . . . . . . 212
14.4 Method Synthesis Using methodMissing . . . . . . . . . 214
14.5 Method Synthesis Using ExpandoMetaClass . . . . . . 219
14.6 Synthesizing Methods for Specific Instances . . . . . . 222
15 MOPping Up 224
15.1 Creating Dynamic Classes with Expando . . . . . . . . 224
15.2 Method Delegation: Putting It All Together . . . . . . . 227
15.3 Review of MOP Techniques . . . . . . . . . . . . . . . . . 231
16 Unit Testing and Mocking 234
16.1 Code in This Book and Automated Unit Tests . . . . . . 234
16.2 Unit Testing Java and Groovy Code . . . . . . . . . . . 236
16.3 Testing for Exceptions . . . . . . . . . . . . . . . . . . . 240
16.4 Mocking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
16.5 Mocking by Overriding . . . . . . . . . . . . . . . . . . . 244
16.6 Mocking Using Categories . . . . . . . . . . . . . . . . . 248
16.7 Mocking Using ExpandoMetaClass . . . . . . . . . . . . 249
16.8 Mocking Using Expando . . . . . . . . . . . . . . . . . . 251
16.9 Mocking Using Map . . . . . . . . . . . . . . . . . . . . . 253
16.10 Mocking Using the Groovy Mock Library . . . . . . . . . 254
17 Groovy Builders 260
17.1 Building XML . . . . . . . . . . . . . . . . . . . . . . . . 260
17.2 Building Swing . . . . . . . . . . . . . . . . . . . . . . . . 264
17.3 Custom Builder Using Metaprogramming . . . . . . . . 265
17.4 Using BuilderSupport . . . . . . . . . . . . . . . . . . . . 268
17.5 Using FactoryBuilderSupport . . . . . . . . . . . . . . . 272
18 Creating DSLs in Groovy 277
18.1 Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
18.2 Fluency . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
18.3 Types of DSLs . . . . . . . . . . . . . . . . . . . . . . . . 280
18.4 Designing Internal DSLs . . . . . . . . . . . . . . . . . . 281
18.5 Groovy and DSLs . . . . . . . . . . . . . . . . . . . . . . 281
18.6 Closures and DSLs . . . . . . . . . . . . . . . . . . . . . 282
18.7 Method Interception and DSLs . . . . . . . . . . . . . . 283
18.8 The Parentheses Limitation and a Workaround . . . . . 285
18.9 Categories and DSLs . . . . . . . . . . . . . . . . . . . . 286
18.10 ExpandoMetaClass and DSLs . . . . . . . . . . . . . . . 289
CONTENTS 13
A Web Resources 291
B Bibliography 296
Index 298