Groovy in Action (Paperback)
暫譯: 《Groovy 實戰》

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

  • 出版商: Manning
  • 出版日期: 2007-01-30
  • 售價: $1,900
  • 貴賓價: 9.5$1,805
  • 語言: 英文
  • 頁數: 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

商品描述(中文翻譯)

描述

“... 清晰且詳細地闡述了 Groovy 的優點。我很高興將它放在我的書架上。”
— 來自 James Gosling 的前言

“優秀的程式碼範例 ... 非常易讀。”
— Scott Shaw, ThoughtWorks

“我心目中的首選。”
— Samuel Pullara, Yahoo, Inc. 技術策略副總裁

“將語言及其庫的細節集中於一處,是一個寶貴的資源。”
— John Wilson, The Wilson Partnership

“對語言特性的出色、邏輯性聚焦。”
— Norman Richards, JBoss 開發者,《XDoclet in Action》作者

“注定成為權威指南。一流!”
— Glen Smith, Bytecode Pty Ltd

“想學習 Groovy 嗎?這本書擁有你所需的一切。”
— Stuart Caborn, ThoughtWorks

Groovy 是一種全新的 Java 平台語言,為 Java 帶來了許多使 Ruby 受歡迎的特性。《Groovy in Action》是一本全面的 Groovy 程式設計指南,向 Java 開發者介紹 Groovy 提供的新動態特性。為了呈現《Groovy in Action》,Manning 再次與一組專家作者合作,包括 Groovy 專案團隊的成員和經理。最終結果是這本全新的 Groovy 語言的真正權威指南。

《Groovy in Action》通過範例介紹 Groovy,提供大量可重用的程式碼,同時解釋其背後的概念。對於新接觸 Groovy 的 Java 開發者來說,這是一個平滑過渡到動態程式設計世界的過程。Groovy 專家則獲得了一個堅實的參考資料,挑戰他們深入且創造性地探索 Groovy。

由於 Groovy 是如此新穎,大多數讀者將從零開始學習。 《Groovy in Action》迅速涵蓋了 Groovy 的基本知識,包括:
- 簡單和集合的 Groovy 數據類型
- 使用閉包和 Groovy 控制結構
- 動態物件導向,Groovy 風格

讀者將看到豐富且詳細的範例,展示 Groovy 對 Java 的增強,包括:
- 如何使用建構器和 GDK
- 使用 Groovy 進行數據庫編程

《Groovy in Action》接著展示如何將 Groovy 與 XML 整合,並提供:
- 提示和技巧
- 單元測試和構建支持
- 在 Windows 上使用 Groovy

額外的獎勵是專門介紹 Grails 的章節,這是 Groovy 的網頁應用框架。

目錄

前言 xix
序言 xx
致謝 xxiii
關於本書 xxv
關於作者 xxix
關於書名 xxxii
關於封面插圖 xxxiii

1 你的 Groovy 之路 1
1.1 Groovy 的故事 3
- Groovy 是什麼? 4
- 與 Java 和諧共處:無縫整合 4
- 代碼中的力量:功能豐富的語言 6
- 社群驅動但企業支持 9
1.2 Groovy 能為你做什麼 10
- Groovy 對 Java 專業人士的幫助 10
- Groovy 對腳本程序員的幫助 11
- Groovy 對務實程序員、極端程序員和敏捷開發者的幫助 12
1.3 運行 Groovy 13
- 使用 groovysh 進行 “Hello World” 14
- 使用 groovyConsole 17
- 使用 groovy 18
1.4 編譯和運行 Groovy 19
- 使用 groovyc 編譯 Groovy 19
- 使用 Java 運行編譯的 Groovy 腳本 20
- 使用 Ant 進行編譯和運行 21
1.5 Groovy IDE 和編輯器支持 22
- IntelliJ IDEA 插件 23
- Eclipse 插件 24
- 其他編輯器中的 Groovy 支持 24
1.6 總結 25

第一部分 Groovy 語言 27

2 序曲:Groovy 基礎 29
2.1 一般代碼外觀 30
- 注釋 Groovy 代碼 30
- 比較 Groovy 和 Java 語法 31
- 簡潔之美 32
2.2 使用斷言探究語言 33
2.3 一瞥 Groovy 36
- 宣告類別 36
- 使用腳本 37
- GroovyBeans 38
- 處理文本 39
- 數字是物件 40
- 使用列表、映射和範圍 41
- 代碼作為物件:閉包 43
- Groovy 控制結構 46
2.4 Groovy 在 Java 環境中的位置 47
- 我的類別是你的類別 47
- GDK:Groovy 庫 49
- Groovy 生命週期 50
2.5 總結 53

3 簡單的 Groovy 數據類型 55
3.1 到處都是物件 56
- Java 的類型系統—原始類型和引用 56
- Groovy 的答案—一切都是物件 57
- 與 Java 的互操作—自動裝箱和拆箱 59
- 無中介拆箱 60
3.2 可選類型的概念 61
- 指定類型 61
- 靜態與動態類型 62
3.3 重載運算符 63
- 可重載運算符概述 63
- 重載運算符的實際應用 65
- 使強制轉換為你所用 67
3.4 處理字串 69
- 字串字面量的多樣性 69
- 使用 GStrings 72
- 從 Java 到 Groovy 74
3.5 處理正則表達式 76
- 在字串字面量中指定模式 78
- 應用模式 81
- 模式的實際應用 82
- 模式與性能 85
- 用於分類的模式 86
3.6 處理數字 87
- 使用數字運算符的強制轉換 87
- GDK 數字方法 90
3.7 總結 91

4 集合的 Groovy 數據類型 93
4.1 處理範圍 94
- 指定範圍 95
- 範圍是物件 97
- 範圍的實際應用 98
4.2 處理列表 100
- 指定列表 100
- 使用列表運算符 101
- 使用列表方法 104
- 列表的實際應用 109
4.3 處理映射 111