Groovy in Action

Free Download

Authors:

Edition: 1

ISBN: 9781932394849, 1-932394-84-2

Size: 13 MB (13810445 bytes)

Pages: 694/694

File format:

Language:

Publishing Year:

Dierk Koenig, Andrew Glover, Paul King, Guillaume Laforge, Jon Skeet, James Gosling9781932394849, 1-932394-84-2

“Groovy in Action” is a comprehensive guide to the brand-new Groovy programming language, its libraries, and everyday use. Groovy brings to Java many of the features that have made Ruby popular. Groovy in Action introduces Java developers to the new dynamic features that Groovy provides for Java developers. Because Groovy is so new, most readers will be learning it from scratch. Groovy In Action quickly moves through the basics of Groovy. After getting readers up and running, Groovy in Action presents rich and detailed examples illustrating Groovy’s enhancements to Java along with Groovy’s special support for XML, regular expressions and database programming. Readers get their hands dirty exploring tips and tricks for Groovy programming along with core tasks such as unit testing and build support. Readers will even learn to script Windows via Groovy. An additional bonus track is dedicated to Grails, the Groovy Web Application Framework. “Groovy in Action” introduces Groovy by example, presenting lots of reusable code while explaining the underlying concepts. Java developers new to Groovy find a smooth transition into the dynamic programming world. Groovy experts gain a solid reference that challenges them to explore Groovy deeply and creatively.

Table of contents :
Groovy in Action……Page 1
foreword……Page 20
preface……Page 21
acknowledgments……Page 24
Your way to Groovy……Page 36
1.1 The Groovy story……Page 38
1.1.2 Playing nicely with Java: seamless integration……Page 39
1.1.3 Power in your code: a feature-rich language……Page 41
1.1.4 Community-driven but corporate-backed……Page 44
1.2.1 Groovy for Java professionals……Page 45
1.2.2 Groovy for script programmers……Page 46
1.2.3 Groovy for pragmatic programmers, extremos, and agilists……Page 47
1.3 Running Groovy……Page 48
1.3.1 Using groovysh for “Hello World”……Page 49
1.3.2 Using groovyConsole……Page 52
1.3.3 Using groovy……Page 53
1.4.1 Compiling Groovy with groovyc……Page 54
1.4.2 Running a compiled Groovy script with Java……Page 55
1.4.3 Compiling and running with Ant……Page 56
1.5 Groovy IDE and editor support……Page 57
1.5.1 IntelliJ IDEA plug-in……Page 58
1.5.3 Groovy support in other editors……Page 59
1.6 Summary……Page 60
Part 1 – The Groovy language……Page 62
Overture: The Groovy basics……Page 64
2.1.1 Commenting Groovy code……Page 65
2.1.2 Comparing Groovy and Java syntax……Page 66
2.1.3 Beauty through brevity……Page 67
2.2 Probing the language with assertions……Page 68
2.3.1 Declaring classes……Page 71
2.3.2 Using scripts……Page 72
2.3.3 GroovyBeans……Page 73
2.3.4 Handling text……Page 74
2.3.5 Numbers are objects……Page 75
2.3.6 Using lists, maps, and ranges……Page 76
2.3.7 Code as objects: closures……Page 78
2.3.8 Groovy control structures……Page 81
2.4.1 My class is your class……Page 82
2.4.2 GDK: the Groovy library……Page 84
2.4.3 The Groovy lifecycle……Page 85
2.5 Summary……Page 88
The simple Groovy datatypes……Page 90
3.1.1 Java’s type system-primitives and references……Page 91
3.1.2 Groovy’s answer-everything’s an object……Page 92
3.1.3 Interoperating with Java-automatic boxing and unboxing……Page 94
3.1.4 No intermediate unboxing……Page 95
3.2.1 Assigning types……Page 96
3.2.2 Static versus dynamic typing……Page 97
3.3.1 Overview of overridable operators……Page 98
3.3.2 Overridden operators in action……Page 100
3.3.3 Making coercion work for you……Page 102
3.4.1 Varieties of string literals……Page 104
3.4.2 Working with GStrings……Page 107
3.4.3 From Java to Groovy……Page 109
3.5 Working with regular expressions……Page 111
3.5.1 Specifying patterns in string literals……Page 113
3.5.2 Applying patterns……Page 116
3.5.3 Patterns in action……Page 117
3.5.4 Patterns and performance……Page 120
3.5.5 Patterns for classification……Page 121
3.6.1 Coercion with numeric operators……Page 122
3.6.2 GDK methods for numbers……Page 125
3.7 Summary……Page 126
The collective Groovy datatypes……Page 128
4.1 Working with ranges……Page 129
4.1.1 Specifying ranges……Page 130
4.1.2 Ranges are objects……Page 132
4.1.3 Ranges in action……Page 133
4.2.1 Specifying lists……Page 135
4.2.2 Using list operators……Page 136
4.2.3 Using list methods……Page 139
4.2.4 Lists in action……Page 144
4.3.1 Specifying maps……Page 146
4.3.2 Using map operators……Page 148
4.3.3 Maps in action……Page 152
4.4.1 Understanding concurrent modification……Page 154
4.4.2 Distinguishing between copy and modify semantics……Page 155
4.5 Summary……Page 156
Working with closures……Page 157
5.1 A gentle introduction to closures……Page 158
5.2.1 Using iterators……Page 160
5.2.2 Handling resources……Page 162
5.3.1 The simple declaration……Page 165
5.3.3 Referring to methods as closures……Page 166
5.3.4 Comparing the available options……Page 168
5.4.1 Calling a closure……Page 170
5.4.2 More closure methods……Page 172
5.5 Understanding scoping……Page 176
5.5.1 The simple variable scope……Page 177
5.5.2 The general closure scope……Page 178
5.5.3 Scoping at work: the classic accumulator test……Page 181
5.6 Returning from closures……Page 183
5.7.1 Relationship to the Visitor pattern……Page 184
5.7.2 Relationship to the Builder pattern……Page 185
5.8 Summary……Page 186
Groovy control structures……Page 188
6.1.1 Evaluating Boolean tests……Page 189
6.1.2 Assignments within Boolean tests……Page 191
6.2.1 The humble if statement……Page 193
6.2.2 The conditional ?: operator……Page 194
6.2.3 The switch statement……Page 195
6.2.4 Sanity checking with assertions……Page 198
6.3.1 Looping with while……Page 202
6.3.2 Looping with for……Page 203
6.4.1 Normal termination: return/break/continue……Page 205
6.4.2 Exceptions: throw/try-catch-finally……Page 206
6.5 Summary……Page 207
Dynamic object orientation, Groovy style……Page 209
7.1 Defining classes and scripts……Page 210
7.1.1 Defining fields and local variables……Page 211
7.1.2 Methods and parameters……Page 215
7.1.3 Safe dereferencing with the ?. operator……Page 219
7.1.4 Constructors……Page 220
7.2.1 File to class relationship……Page 223
7.2.2 Organizing classes in packages……Page 225
7.2.3 Further classpath considerations……Page 229
7.3.1 Using inheritance……Page 230
7.3.2 Using interfaces……Page 231
7.3.3 Multimethods……Page 232
7.4 Working with GroovyBeans……Page 234
7.4.1 Declaring beans……Page 235
7.4.2 Working with beans……Page 236
7.4.3 Using bean methods for any object……Page 240
7.4.4 Fields, accessors, maps, and Expando……Page 241
7.5 Using power features……Page 242
7.5.1 Querying objects with GPaths……Page 243
7.5.2 Injecting the spread operator……Page 247
7.5.3 Mix-in categories with the use keyword……Page 248
7.6.1 Understanding the MetaClass concept……Page 251
7.6.2 Method invocation and interception……Page 253
7.6.3 Method interception in action……Page 255
7.7 Summary……Page 259
Part 2 – Around the Groovy library……Page 262
Working with builders……Page 264
8.1 Learning by example-using a builder……Page 266
8.2 Building object trees with NodeBuilder……Page 269
8.2.1 NodeBuilder in action-a closer look at builder code……Page 270
8.2.3 Smart building with logic……Page 272
8.3 Working with MarkupBuilder……Page 274
8.3.1 Building XML……Page 275
8.3.2 Building HTML……Page 276
8.4.1 From Ant scripts to Groovy scripts……Page 278
8.4.2 How AntBuilder works……Page 280
8.4.3 Smart automation scripts with logic……Page 281
8.5 Easy GUIs with SwingBuilder……Page 282
8.5.1 Reading a password with SwingBuilder……Page 283
8.5.2 Creating Swing widgets……Page 285
8.5.3 Arranging your widgets……Page 289
8.5.4 Referring to widgets……Page 292
8.5.5 Using Swing actions……Page 295
8.5.6 Using models……Page 297
8.5.7 Putting it all together……Page 299
8.6 Creating your own builder……Page 306
8.6.1 Subclassing BuilderSupport……Page 307
8.6.2 The DebugBuilder example……Page 309
8.7 Summary……Page 311
Working with the GDK……Page 312
9.1 Working with Objects……Page 313
9.1.1 Interactive objects……Page 314
9.1.2 Convenient Object methods……Page 320
9.1.3 Iterative Object methods……Page 323
9.2 Working with files and I/O……Page 326
9.2.1 Traversing the filesystem……Page 329
9.2.2 Reading from input sources……Page 330
9.2.3 Writing to output destinations……Page 332
9.2.4 Filters and conversions……Page 333
9.2.5 Streaming serialized objects……Page 335
9.3 Working with threads and processes……Page 336
9.3.1 Groovy multithreading……Page 337
9.3.2 Integrating external processes……Page 339
9.4.1 Understanding the template format……Page 344
9.4.2 Templates in action……Page 345
9.4.3 Advanced template issues……Page 347
9.5.1 Starting with “hello world”……Page 349
9.5.2 The Groovlet binding……Page 351
9.5.3 Templating Groovlets……Page 354
9.6 Summary……Page 356
Database programming with Groovy……Page 358
10.1.1 Setting up for database access……Page 360
10.1.2 Executing SQL……Page 364
10.1.3 Fetching data……Page 369
10.1.4 Putting it all together……Page 373
10.2 DataSets for SQL without SQL……Page 375
10.2.1 Using DataSet operations……Page 376
10.2.2 DataSets on database views……Page 379
10.3.1 Architectural overview……Page 382
10.3.2 Specifying the application behavior……Page 384
10.3.3 Implementing the infrastructure……Page 385
10.3.5 Implementing the application layer……Page 390
10.4 Groovy and ORM……Page 392
10.5 Summary……Page 393
Integrating Groovy……Page 395
11.1 Getting ready to integrate……Page 396
11.1.1 Integrating appropriately……Page 397
11.1.2 Setting up dependencies……Page 398
11.2.1 Starting simply……Page 400
11.2.2 Passing parameters within a binding……Page 402
11.2.3 Generating dynamic classes at runtime……Page 404
11.2.4 Parsing scripts……Page 405
11.2.5 Running scripts or classes……Page 406
11.2.6 Further parameterization of GroovyShell……Page 407
11.3.1 Setting up the engine……Page 411
11.3.3 Defining a different resource connector……Page 412
11.4.1 Parsing and loading Groovy classes……Page 413
11.4.2 The chicken and egg dependency problem……Page 415
11.4.3 Providing a custom resource loader……Page 419
11.4.4 Playing it safe in a secured sandbox……Page 420
11.5 Spring integration……Page 424
11.5.1 Wiring GroovyBeans……Page 425
11.5.3 Inline scripts……Page 427
11.6.1 Introducing JSR-223……Page 428
11.6.2 The script engine manager and its script engines……Page 430
11.6.3 Compilable and invocable script engines……Page 431
11.7 Choosing an integration mechanism……Page 433
11.8 Summary……Page 434
Working with XML……Page 436
12.1 Reading XML documents……Page 437
12.1.1 Working with a DOM parser……Page 438
12.1.2 Reading with a Groovy parser……Page 443
12.1.3 Reading with a SAX parser……Page 449
12.1.4 Reading with a StAX parser……Page 451
12.2 Processing XML……Page 452
12.2.1 In-place processing……Page 453
12.2.2 Streaming processing……Page 456
12.2.3 Combining with XPath……Page 461
12.3 Distributed processing with XML……Page 469
12.3.2 Reading RSS and ATOM……Page 470
12.3.3 Using a REST-based API……Page 472
12.3.4 Using XML-RPC……Page 476
12.3.5 Applying SOAP……Page 479
12.4 Summary……Page 484
Part 3 – Everyday Groovy……Page 486
Tips and tricks……Page 488
13.1.1 Equality versus identity……Page 489
13.1.2 Using parentheses wisely……Page 490
13.1.3 Returning from methods and closures……Page 491
13.1.4 Calling methods in builder code……Page 492
13.1.5 Qualifying access to “this”……Page 494
13.1.6 Considering number types……Page 495
13.1.7 Leveraging Ant……Page 496
13.1.8 Scripts are classes but different……Page 499
13.2.1 Shuffling a collection……Page 502
13.2.3 Console progress bar……Page 503
13.2.4 Self-commenting single-steps……Page 505
13.2.5 Advanced GString usage……Page 506
13.3 Using groovy on the command line……Page 507
13.3.1 Evaluating a command-line script……Page 508
13.3.2 Using print and line options……Page 509
13.3.3 Using the listen mode……Page 510
13.4 Writing automation scripts……Page 511
13.4.1 Supporting command-line options consistently……Page 512
13.4.2 Expanding the classpath with RootLoader……Page 516
13.4.3 Scheduling scripts for execution……Page 518
13.5.1 Scraping HTML pages……Page 520
13.5.2 Automating web actions……Page 522
13.5.3 Inspecting version control……Page 524
13.5.4 Pragmatic code analysis……Page 526
13.5.5 More points of interest……Page 527
13.6 Laying out the workspace……Page 528
13.6.1 IDE setup……Page 529
13.6.2 Debugging……Page 530
13.6.3 Profiling……Page 535
13.7 Summary……Page 536
Unit testing with Groovy……Page 538
14.1.1 Writing tests is easy……Page 540
14.1.2 GroovyTestCase: an introduction……Page 541
14.2 Unit-testing Groovy code……Page 543
14.3 Unit-testing Java code……Page 547
14.4 Organizing your tests……Page 551
14.5 Advanced testing techniques……Page 552
14.5.1 Testing made groovy……Page 553
14.5.2 Stubbing and mocking……Page 555
14.5.3 Using GroovyLogTestCase……Page 560
14.6.1 Using GroovyTestSuite……Page 562
14.6.2 Using AllTestSuite……Page 564
14.6.3 Advanced IDE integration……Page 566
14.7.1 Code coverage with Groovy……Page 568
14.7.2 JUnit extensions……Page 572
14.8.1 Build integration with Ant……Page 574
14.8.2 Build integration with Maven……Page 576
14.9 Summary……Page 579
Groovy on Windows……Page 581
15.1 Downloading and installing Scriptom……Page 582
15.2.1 Introducing Jacob……Page 583
15.2.2 Instantiating an ActiveX component……Page 585
15.2.3 Invoking methods……Page 588
15.2.5 Event support……Page 590
15.3 Real-world scenario: automating localization……Page 593
15.3.1 Designing our document format……Page 594
15.3.2 Designing the thesaurus spreadsheet……Page 595
15.3.3 Creating a Word document……Page 597
15.3.4 Producing the final document……Page 599
15.4 Further application automation……Page 600
15.4.1 Accessing the Windows registry……Page 601
15.4.2 Rolling out your own automation system……Page 603
15.5 Where to get documentation……Page 604
15.6 Summary……Page 605
Seeing the Grails light……Page 607
16.1 Setting the stage……Page 608
16.1.2 Getting your feet wet……Page 609
16.2.1 Thinking through the use cases……Page 612
16.2.2 Designing relations……Page 613
16.3 Implementing the domain model……Page 614
16.3.1 Scaffolding domain classes……Page 615
16.3.2 Scaffolding views and controllers……Page 616
16.3.3 Testing the web application……Page 617
16.3.4 Completing the domain model……Page 619
16.4 Customizing the views……Page 620
16.4.1 Bootstrapping data……Page 621
16.4.2 Working with Groovy Server Pages……Page 622
16.4.3 Working with tag libraries……Page 625
16.5 Working with controllers and finder methods……Page 627
16.6 Elaborating the model……Page 630
16.7 Working with the session……Page 631
16.8 Finishing up……Page 635
16.8.1 Validating constraints……Page 636
16.8.2 Deploying the application……Page 637
16.8.3 Farewell……Page 639
Installation and documentation……Page 641
A.1 Installation……Page 642
A.2.2 Subscribing to mailing lists……Page 643
A.2.3 Connecting to forum and chat……Page 644
Groovy language info……Page 645
B.1 Keyword list……Page 647
GDK API quick reference……Page 648
C.1 Arrays and primitives……Page 649
C.2 The java.lang package……Page 650
C.4 The java.util and java.sql packages……Page 656
C.5 The java.util.regex package……Page 660
C.6 The java.io package……Page 661
C.7 The java.net package……Page 665
Cheat sheets……Page 666
D.2 Closures……Page 667
D.3 Regular expressions……Page 668
D.5 Mocks and stubs……Page 670
D.6 XML GPath notation……Page 671
A……Page 674
C……Page 675
D……Page 678
E……Page 679
F……Page 680
G……Page 681
I……Page 682
J……Page 683
M……Page 684
N……Page 685
O……Page 686
P……Page 687
R……Page 688
S……Page 689
T……Page 692
W……Page 693
Y……Page 694

Reviews

There are no reviews yet.

Be the first to review “Groovy in Action”
Shopping Cart
Scroll to Top