Groovy in Action (Paperback)

Dierk Koenig, Andrew Glover, Paul King, Guillaume Laforge, Jon Skeet

  • 出版商: Manning
  • 出版日期: 2007-01-30
  • 售價: $1,960
  • 貴賓價: 9.5$1,862
  • 語言: 英文
  • 頁數: 696
  • 裝訂: Paperback
  • ISBN: 1932394842
  • ISBN-13: 9781932394849
  • 相關分類: JVM 語言
  • 已過版

買這商品的人也買了...

相關主題

商品描述

Description

“... a clear and detailed exposition of what is groovy about Groovy. Im glad to have it on my bookshelf.”
—From the Foreword by James Gosling

“Excellent code samples ... very readable.”
—Scott Shaw, ThoughtWorks

“Top of my list.”
—Samuel Pullara, VP Technology Strategy, Yahoo, Inc.

“Collects in one place details of the language and its librariesa valuable resource.”
—John Wilson, The Wilson Partnership

“Great, logical focus on language features.”
—Norman Richards, JBoss Developer, author of XDoclet in Action

“Destined to be the definitive guide. First rate!”
—Glen Smith, Bytecode Pty Ltd

“You want to learn Groovy? This book has all you need.”
—Stuart Caborn, ThoughtWorks

Groovy, the brand-new language for the Java platform, brings to Java many of the features that have made Ruby popular. Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the source by working with a team of expert authors including both members and the manager of the Groovy Project team. The result is the true definitive guide to the new Groovy language.

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.

Because Groovy is so new, most readers will be learning it from scratch. Groovy in Action quickly moves through the Groovy basics, including:

  • Simple and collective Groovy data types
  • Working with closures and Groovy control structures
  • Dynamic Object Orientation, Groovy style

Readers are presented with rich and detailed examples illustrating Groovy's enhancements to Java, including

  • How to work with builders and the GDK
  • Database programming with Groovy

Groovy in Action then demonstrates how to Integrate Groovy with XML, and provides,

  • Tips and Tricks
  • Unit testing and build support
  • Groovy on Windows

An additional bonus is a chapter dedicated to Grails, the Groovy web application framework.

 

Table of contents

foreword xix
preface xx
acknowledgments xxiii
about this book xxv
about the authors xxix
about the title xxxii
about the cover illustration xxxiii


1  Your way to Groovy   1
1.1 The Groovy story 3
What is Groovy? 4
Playing nicely with Java: seamless integration 4
Power in your code: a feature-rich language 6
Community-driven but corporate-backed 9
1.2 What Groovy can do for you 10
Groovy for Java professionals 10
Groovy for script programmers 11
Groovy for pragmatic programmers, extremos, and agilists 12
1.3 Running Groovy 13
Using groovysh for “Hello World” 14
Using groovyConsole 17
Using groovy 18
1.4 Compiling and running Groovy 19
Compiling Groovy with groovyc 19
Running a compiled Groovy script with Java 20
Compiling and running with Ant 21
1.5 Groovy IDE and editor support 22
IntelliJ IDEA plug-in 23
Eclipse plug-in 24
Groovy support in other editors 24
1.6 Summary 25

Part 1  The Groovy language   27

2  Overture: The Groovy basics   29
2.1 General code appearance 30
Commenting Groovy code 30
Comparing Groovy and Java syntax 31
Beauty through brevity 32
2.2 Probing the language with assertions 33
2.3 Groovy at a glance 36
Declaring classes 36
Using scripts 37
GroovyBeans 38
Handling text 39
Numbers are objects 40
Using lists, maps, and ranges 41
Code as objects: closures 43
Groovy control structures 46
2.4 Groovy’s place in the Java environment 47
My class is your class 47
GDK: the Groovy library 49
The Groovy lifecycle 50
2.5 Summary 53
3  The simple Groovy datatypes   55
3.1 Objects, objects everywhere 56
Java’s type system—primitives and references 56
Groovy’s answer—everything’s an object 57
Interoperating with Java—automatic boxing and unboxing 59
No intermediate unboxing 60
3.2 The concept of optional typing 61
Assigning types 61
Static versus dynamic typing 62
3.3 Overriding operators 63
Overview of overridable operators 63
Overridden operators in action 65
Making coercion work for you 67
3.4 Working with strings 69
Varieties of string literals 69
Working with GStrings 72
From Java to Groovy 74
3.5 Working with regular expressions 76
Specifying patterns in string literals 78
Applying patterns 81
Patterns in action 82
Patterns and performance 85
Patterns for classification 86
3.6 Working with numbers 87
Coercion with numeric operators 87
GDK methods for numbers 90
3.7 Summary 91
4  The collective Groovy datatypes   93
4.1 Working with ranges 94
Specifying ranges 95
Ranges are objects 97
Ranges in action 98
4.2 Working with lists 100
Specifying lists 100
Using list operators 101
Using list methods 104
Lists in action 109
4.3 Working with maps 111
Specifying maps 111
Using map operators 113
Maps in action 117
4.4 Notes on Groovy collections 119
Understanding concurrent modification 119
Distinguishing between copy and modify semantics 120
4.5 Summary 121
5  Working with closures   122
5.1 A gentle introduction to closures 123
5.2 The case for closures 125
Using iterators 125
Handling resources 127
5.3 Declaring closures 130
The simple declaration 130
Using assignments for declaration 131
Referring to methods as closures 131
Comparing the available options 133
5.4 Using closures 135
Calling a closure 135
More closure methods 137
5.5 Understanding scoping 141
The simple variable scope 142
The general closure scope 143
Scoping at work: the classic accumulator test 146
5.6 Returning from closures 148
5.7 Support for design patterns 149
Relationship to the Visitor pattern 149
Relationship to the Builder pattern 150
Relationship to other patterns 151
5.8 Summary 151
6  Groovy control structures   153
6.1 The Groovy truth 154
Evaluating Boolean tests 154
Assignments within Boolean tests 156
6.2 Conditional execution structures 158
The humble if statement 158
The conditional ?: operator 159
The switch statement 160
Sanity checking with assertions 163
6.3 Looping 167
Looping with while 167
Looping with for 168
6.4 Exiting blocks and methods 170
Normal termination: return/break/continue 170
Exceptions: throw/try-catch-finally 171
6.5 Summary 172
7  Dynamic object orientation, Groovy style   174
7.1 Defining classes and scripts 175
Defining fields and local variables 176
Methods and parameters 180
Safe dereferencing with the ?. operator 184
Constructors 185
7.2 Organizing classes and scripts 188
File to class relationship 188
Organizing classes in packages 190
Further classpath considerations 194
7.3 Advanced OO features 195
Using inheritance 195
Using interfaces 196
Multimethods 197
7.4 Working with GroovyBeans 199
Declaring beans 200
Working with beans 201
Using bean methods for any object 205
Fields, accessors, maps, and Expando 206
7.5 Using power features 207
Querying objects with GPaths 208
Injecting the spread operator 212
Mix-in categories with the use keyword 213
7.6 Meta programming in Groovy 216
Understanding the MetaClass concept 216
Method invocation and interception 218
Method interception in action 220
7.7 Summary 224

Part 2  Around the Groovy library   227

8  Working with builders   229
8.1 Learning by example—using a builder 231
8.2 Building object trees with NodeBuilder 234
NodeBuilder in action—a closer look at builder code 235
Understanding the builder concept 237
Smart building with logic 237
8.3 Working with MarkupBuilder 239
Building XML 240
Building HTML 241
8.4 Task automation with AntBuilder 243
From Ant scripts to Groovy scripts 243
How AntBuilder works 245
Smart automation scripts with logic 246
8.5 Easy GUIs with SwingBuilder 247
Reading a password with SwingBuilder 248
Creating Swing widgets 250
Arranging your widgets 254
Referring to widgets 257
Using Swing actions 260
Using models 262
Putting it all together 264
8.6 Creating your own builder 271
Subclassing BuilderSupport 272
The DebugBuilder example 274
8.7 Summary 276
9  Working with the GDK   277
9.1 Working with Objects 278
Interactive objects 279
Convenient Object methods 285
Iterative Object methods 288
9.2 Working with files and I/O 291
Traversing the filesystem 294
Reading from input sources 295
Writing to output destinations 297
Filters and conversions 298
Streaming serialized objects 300
9.3 Working with threads and processes 301
Groovy multithreading 302
Integrating external processes 304
9.4 Working with templates 309
Understanding the template format 309
Templates in action 310
Advanced template issues 312
9.5 Working with Groovlets 314
Starting with “hello world” 314
The Groovlet binding 316
Templating Groovlets 319
9.6 Summary 321
10  Database programming with Groovy   323
10.1 Basic database operations 325
Setting up for database access 325
Executing SQL 329
Fetching data 334
Putting it all together 338
10.2 DataSets for SQL without SQL 340
Using DataSet operations 341
DataSets on database views 344
10.3 Organizing database work 347
Architectural overview 347
Specifying the application behavior 349
Implementing the infrastructure 350
Using a transparent domain model 355
Implementing the application layer 355
10.4 Groovy and ORM 357
10.5 Summary 358
11  Integrating Groovy   360
11.1 Getting ready to integrate 361
Integrating appropriately 362
Setting up dependencies 363
11.2 Evaluating expressions and scripts with GroovyShell 365
Starting simply 365
Passing parameters within a binding 367
Generating dynamic classes at runtime 369
Parsing scripts 370
Running scripts or classes 371
Further parameterization of GroovyShell 372
11.3 Using the Groovy script engine 376
Setting up the engine 376
Running scripts 377
Defining a different resource connector 377
11.4 Working with the GroovyClassLoader 378
Parsing and loading Groovy classes 378
The chicken and egg dependency problem 380
Providing a custom resource loader 384
Playing it safe in a secured sandbox 385
11.5 Spring integration 389
Wiring GroovyBeans 390
Refreshable beans 392
Inline scripts 392
11.6 Riding Mustang and JSR-223 393
Introducing JSR-223 393
The script engine manager and its script engines 395
Compilable and invocable script engines 396
11.7 Choosing an integration mechanism 398
11.8 Summary 399
12  Working with XML   401
12.1 Reading XML documents 402
Working with a DOM parser 403
Reading with a Groovy parser 408
Reading with a SAX parser 414
Reading with a StAX parser 416
12.2 Processing XML 417
In-place processing 418
Streaming processing 421
Combining with XPath 426
12.3 Distributed processing with XML 434
An overview of web services 435
Reading RSS and ATOM 435
Using a REST-based API 437
Using XML-RPC 441
Applying SOAP 444
12.4 Summary 449

Part 3  Everyday Groovy   451

13  Tips and tricks   453
13.1 Things to remember 454
Equality versus identity 454
Using parentheses wisely 455
Returning from methods and closures 456
Calling methods in builder code 457
Qualifying access to “this” 459
Considering number types 460
Leveraging Ant 461
Scripts are classes but different 464
13.2 Useful snippets 467
Shuffling a collection 467
Scrambling text with regular expressions 468
Console progress bar 468
Self-commenting single-steps 470
Advanced GString usage 471
13.3 Using groovy on the command line 472
Evaluating a command-line script 473
Using print and line options 474
Using the listen mode 475
In-place editing from the command line 476
13.4 Writing automation scripts 476
Supporting command-line options consistently 477
Expanding the classpath with RootLoader 481
Scheduling scripts for execution 483
13.5 Example automation tasks 485
Scraping HTML pages 485
Automating web actions 487
Inspecting version control 489
Pragmatic code analysis 491
More points of interest 492
13.6 Laying out the workspace 493
IDE setup 494
Debugging 495
Profiling 500
Refactoring 501
13.7 Summary 501
14  Unit testing with Groovy   503
14.1 Getting started 505
Writing tests is easy 505
GroovyTestCase: an introduction 506
Working with GroovyTestCase 508
14.2 Unit-testing Groovy code 508
14.3 Unit-testing Java code 512
14.4 Organizing your tests 516
14.5 Advanced testing techniques 517
Testing made groovy 518
Stubbing and mocking 520
Using GroovyLogTestCase 525
14.6 IDE integration 527
Using GroovyTestSuite 527
Using AllTestSuite 529
Advanced IDE integration 531
14.7 Tools for Groovy testing 533
Code coverage with Groovy 533
JUnit extensions 537
14.8 Build automation 539
Build integration with Ant 539
Build integration with Maven 541
14.9 Summary 544
15  Groovy on Windows   546
15.1 Downloading and installing Scriptom 547
15.2 Inside Scriptom 548
Introducing Jacob 548
Instantiating an ActiveX component 550
Invoking methods 553
Accessing properties and return values 555
Event support 555
15.3 Real-world scenario: automating localization 558
Designing our document format 559
Designing the thesaurus spreadsheet 560
Creating a Word document 562
Producing the final document 564
15.4 Further application automation 565
Accessing the Windows registry 566
Rolling out your own automation system 568
15.5 Where to get documentation 569
15.6 Summary 570
16  Seeing the Grails light   572
16.1 Setting the stage 573
Installing Grails 574
Getting your feet wet 574
16.2 Laying out the domain model 577
Thinking through the use cases 577
Designing relations 578
16.3 Implementing the domain model 579
Scaffolding domain classes 580
Scaffolding views and controllers 581
Testing the web application 582
Completing the domain model 584
16.4 Customizing the views 585
Bootstrapping data 586
Working with Groovy Server Pages 587
Working with tag libraries 590
16.5 Working with controllers and finder methods 592
16.6 Elaborating the model 595
16.7 Working with the session 596
16.8 Finishing up 600
Validating constraints 601
Deploying the application 602
Farewell 604
appendix A  Installation and documentation   606
appendix B  Groovy language info   610
appendix C  GDK API quick reference   613
appendix D  Cheat sheets   631


index 639 1