Programming Groovy: dynamic productivity for the Java developer

7 downloads 104 Views 171KB Size Report
1.1. Why Dynamic Languages? 1. 1.2. What's Groovy? 4. 1.3. Why Groovy? 5. 1.4 . What's in This Book? 8. 1.5. Who Is This Book For? 11. 1.6. Acknowledgments.
Dynamic Productivity for the Java Developer Venkat Subramaniam

The Pragmatic Bookshelf Raleigh, North

Carolina

Dallas, Texas

ifflffl§SJisW:^W^8iS®M^^W^®giSSSl

Foreword 1

Introduction 1.1 1.2 1.3

1

2

1

Why Dynamic Languages? What's Groovy? Why Groovy?

1 4

5

1.4

What's in This Book?

1.5

Who Is This Book For?

11

1.6

Acknowledgments

11

8

Beginning Groovy

15

Getting Started

17

2.1 2.2

2.3

Getting Groovy Installing Groovy

17 18

Test-Drive

2.5

Using groovysh Using groovyConsole Running Groovy on the Command

2.6

Using

2.4

3

xvii

Groovy

19

20 Line

an IDE

21 22

for the Java

Eyes

25

3.1

From Java to

3.2

JavaBeans

3.3

Optional Parameters

38

3.4

Implementing

39

3.5

Groovy boolean Evaluation

3.6

Operator Overloading Support of Java 5 Language

3.7 3.8

Gotchas

Groovy

25 33

Interfaces

43 44 Features

47 55

xii

CONTENTS

4

4.1

5

6

Typing

63

in Java

66

4.2

Dynamic Typing

4.3

Dynamic Typing

4.4

Design by Capability

68

4.5

Optional Typing Types in Groovy

74

4.6 4.7

Multimethods

!= Weak

Typing

67

74 75

4.8

Dynamic:

Using

Closures

81

5.1

Closures

81

5.2

Use of Closures

85

5.3

Working with Closures

87

5.4

Closure and Resource

5.5

Closures and Coroutines

90

5.6

Curried Closure

91

5.7

Dynamic Closures

94

5.8

Closure

96

5.9

Using Closures

To Be

or

Not to Be?

Cleanup

Delegation

GString Lazy

6.3

Multiline

87

101

Literals and Expressions

6.2

79

99

Working with Strings 6.1

7

63

Dynamic Typing

Evaluation Problem

101 104

108

6.4

String String Convenience

6.5

Regular Expressions

Ill

with Collections

115

Working

Methods

List

110

7.1

Using

7.2

Iterating

7.3

Finder Methods

120

7.4

Collections'Convenience Methods

121

7.5

Using Map

124

7.6

Iterating Over Map

126

7.7

Map Convenience Methods

128

Over

115 an

ArrayList

117

^ xill

II 8

Using Groovy

9

Exploring the Object

Other Extensions

Parsing

133 139

147

XML

Creating

Working with

147

XML

152

Databases

10.1

Connecting

10.2

Database Select

10.3

Transforming Data

10.4

Using

10.5

Inserting

10.6

to

a

157

Database

158

159 to XML

160

DataSet and

with

161

Updating

162

Microsoft Excel

162

Scripts and Classes

165

Accessing

Working 11.1

The

11.2

Running Groovy

11.3

Using Groovy

Classes from

11.4

Using Groovy

Classes from Java

11.5

Melting Pot of Java

and

Groovy

165 166

Groovy

167 168

Java Classes from Groovy

11.6

Using Using Groovy Scripts from Groovy

171

11.7

Using Groovy Scripts from Java

173

11.8

Ease of Integration

175

169

MOPping Groovy Exploring Meta-Object 12.1

13

Extensions

Working with XML

10

12

133

8.2

9.2

III

GDK

8.1

9.1

11

131

177 Protocol

(MOP)

179

Groovy Object

180

12.2

Querying

12.3

Dynamically Accessing Objects

Methods and

Intercepting Methods Using

Properties

185 187

MOP

13.1

Intercepting

Methods Using

13.2

Intercepting

Methods

Groovylnterceptable Using MetaClass

189 .

...

189

192

xiv

CONTENTS

14 MOP Method

14.2

Injecting Methods Using ExpandoMetaClass

Using Categories

Methods into

Injecting

14.4

Method Synthesis Using

14.5

Method

Instances

methodMissing

Synthesis Using ExpandoMetaClass

Synthesizing

Methods for

Specific

Instances

198

203 207 209 214 217

219

MOPping Up 15.1

Creating Dynamic

15.2

Method Delegation: Putting It All Together

222

15.3

Review of MOP

226

Testing

and

Classes with

Expando

Techniques

219

229

Mocking

16.1

Code in This Book and Automated Unit Tests

16.2

Unit

16.3

Testing for Exceptions

235

16.4

Mocking

236

16.5

Mocking by Overriding

239

16.6

Mocking Using Categories

243

16.7

Mocking Using ExpandoMetaClass

244

16.8

Mocking Using Expando

246

16.9

Mocking Using Map

Testing

Java and

16.10 Mocking Using the

18

Specific

14.3

16 Unit

17

197

Injecting Methods

14.6

15

Injection and Synthesis

14.1

Groovy Code

229 231

248

Groovy

Mock

Library

249

Groovy Builders

255

17.1

Building

255

17.2

Building Swing

259

17.3

Custom Builder

260

17.4

Using Metaprogramming Using BuilderSupport

17.5

Using FactoryBuilderSupport

267

Creating

XML

DSLs in

Groovy

263

273

18.1

Context

273

18.2

Fluency

275

18.3

Types

18.4

Designing Internal DSLs

277

18.5

Groovy

and DSLs

277

18.6

Closures and DSLs

18.7

Method

18.8

The Parentheses Limitation and a Workaround

281

18.9

Categories and

282

18.10

of DSLs

276

278

Interception

and DSLs

DSLs

ExpandoMetaClass

and DSLs

279

285

Web Resources

287

Bibliography

293

Index

295