Programming Groovy 2

Book description

Groovy brings you the best of both worlds: a flexible, highly productive, agile, dynamic language that runs on the rich framework of the Java Platform. Groovy preserves the Java semantics and extends the JDK to give you true dynamic language capabilities. Programming Groovy 2 will help you, the experienced Java developer, learn and take advantage of the latest version of this rich dynamic language. You'll go from the basics of Groovy to the latest advances in the language, including options for type checking, tail-call and memoization optimizations, compile time metaprogramming, and fluent interfaces to create DSLs.

You don't have to leave the rich Java Platform to take advantage of Groovy. Groovy preserves Java's semantics and extends the JDK, so programming in Groovy feels like the Java language has been augmented; it's like working with a lighter, more elegant Java. If you're an experienced Java developer who wants to learn how Groovy works, you'll find exactly what you need in this book.

You'll start with the fundamentals of programming in Groovy and how it works with Java, and then you'll explore advanced concepts such as unit testing with mock objects, using Builders, working with databases and XML, and creating DSLs. You'll master Groovy's powerful yet complex run-time and compile-time metaprogramming features.

Much has evolved in the Groovy language since the publication of the first edition of Programming Groovy. Programming Groovy 2 will help you learn and apply Groovy's new features. Creating DSLs is easier now, and Groovy's already-powerful metaprogramming facilities have improved even more. You'll see how to work with closures, including tail call optimization and memoization. The book also covers Groovy's new static compilation feature.

Whether you're learning the basics of the language or interested in getting proficient with the new features, Programming Groovy 2 has you covered.

What You Need

To work on the examples in the book you need Groovy 2.0.5 and Java JDK 5 or higher.

Table of contents

  1. Programming Groovy 2
  2. Table of Contents
  3. What readers are saying about Programming Groovy 2
  4. Foreword to the Second Edition
  5. Introduction
    1. What’s Groovy?
    2. Why Dynamic Languages?
    3. Why Groovy?
    4. What’s in This Book?
    5. Changes Since This Book’s First Edition
    6. Who Is This Book For?
    7. Online Resources
    8. Acknowledgments
  6. Part 1: Beginning Groovy
  7. Chapter 1: Getting Started
    1. 1.1 Installing Groovy
    2. 1.2 Installing and Managing Groovy Versions
    3. 1.3 Test-Drive Using groovysh
    4. 1.4 Using groovyConsole
    5. 1.5 Running Groovy on the Command Line
    6. 1.6 Using an IDE
  8. Chapter 2: Groovy for Java Eyes
    1. 2.1 From Java to Groovy
    2. 2.2 JavaBeans
    3. 2.3 Flexible Initialization and Named Arguments
    4. 2.4 Optional Parameters
    5. 2.5 Using Multiple Assignments
    6. 2.6 Implementing Interfaces
    7. 2.7 Groovy Boolean Evaluation
    8. 2.8 Operator Overloading
    9. 2.9 Support of Java 5 Language Features
    10. 2.10 Using Groovy Code-Generation Transformations
    11. 2.11 Gotchas
  9. Chapter 3: Dynamic Typing
    1. 3.1 Typing in Java
    2. 3.2 Dynamic Typing
    3. 3.3 Dynamic Typing != Weak Typing
    4. 3.4 Design by Capability
    5. 3.5 Optional Typing
    6. 3.6 Multimethods
    7. 3.7 Dynamic: To Be or Not to Be?
    8. 3.8 Switching Off Dynamic Typing
  10. Chapter 4: Using Closures
    1. 4.1 The Convenience of Closures
    2. 4.2 Programming with Closures
    3. 4.3 Ways to Use Closures
    4. 4.4 Passing Parameters to Closures
    5. 4.5 Using Closures for Resource Cleanup
    6. 4.6 Closures and Coroutines
    7. 4.7 Curried Closure
    8. 4.8 Dynamic Closures
    9. 4.9 Closure Delegation
    10. 4.10 Programming with Tail Recursion
    11. 4.11 Improving Performance Using Memoization
  11. Chapter 5: Working with Strings
    1. 5.1 Literals and Expressions
    2. 5.2 GString Lazy-Evaluation Problem
    3. 5.3 Multiline Strings
    4. 5.4 String Convenience Methods
    5. 5.5 Regular Expressions
  12. Chapter 6: Working with Collections
    1. 6.1 Using List
    2. 6.2 Iterating Over an ArrayList
    3. 6.3 Using Finder Methods
    4. 6.4 Other Convenience Methods on ​ List ​s
    5. 6.5 Using the ​ Map ​ Class
    6. 6.6 Iterating Over Map
    7. 6.7 Other Convenience Methods on ​ Map ​s
  13. Part 2: Using Groovy
  14. Chapter 7: Exploring the GDK
    1. 7.1 Using Object Extensions
    2. 7.2 Other Extensions
    3. 7.3 Custom Methods Using the Extension Modules
  15. Chapter 8: Working with XML
    1. 8.1 Parsing XML
    2. 8.2 Creating XML
  16. Chapter 9: Working with Databases
    1. 9.1 Setting Up the Database
    2. 9.2 Connecting to a Database
    3. 9.3 Database Select
    4. 9.4 Transforming Data to XML
    5. 9.5 Using DataSet
    6. 9.6 Inserting and Updating
    7. 9.7 Accessing Microsoft Excel
  17. Chapter 10: Working with Scripts and Classes
    1. 10.1 The Melting Pot of Java and Groovy
    2. 10.2 Running Groovy
    3. 10.3 Using Groovy Classes from Groovy
    4. 10.4 Intermixing Groovy and Java with Joint Compilation
    5. 10.5 Creating and Passing Groovy Closures from Java
    6. 10.6 Calling Groovy Dynamic Methods from Java
    7. 10.7 Using Java Classes from Groovy
    8. 10.8 Using Groovy Scripts from Groovy
    9. 10.9 Using Groovy Scripts from Java
  18. Part 3: MOPping Groovy
  19. Chapter 11: Exploring Metaobject Protocol (MOP)
    1. 11.1 Groovy Object
    2. 11.2 Querying Methods and Properties
    3. 11.3 Dynamically Accessing Objects
  20. Chapter 12: Intercepting Methods Using MOP
    1. 12.1 Intercepting Methods Using GroovyInterceptable
    2. 12.2 Intercepting Methods Using MetaClass
  21. Chapter 13: MOP Method Injection
    1. 13.1 Injecting Methods Using Categories
    2. 13.2 Injecting Methods Using ExpandoMetaClass
    3. 13.3 Injecting Methods into Specific Instances
    4. 13.4 Injecting Methods Using Mixins
    5. 13.5 Decorating Classes with Multiple Mixins
  22. Chapter 14: MOP Method Synthesis
    1. 14.1 Method Synthesis Using methodMissing
    2. 14.2 Method Synthesis Using ExpandoMetaClass
    3. 14.3 Synthesizing Methods for Specific Instances
  23. Chapter 15: MOPping Up
    1. 15.1 Creating Dynamic Classes with Expando
    2. 15.2 Method Delegation: Putting It All Together
    3. 15.3 Review of MOP Techniques
  24. Chapter 16: Applying Compile-Time Metaprogramming
    1. 16.1 Analyzing Code at Compile Time
    2. 16.2 Intercepting Method Calls Using AST Transformations
    3. 16.3 Injecting Methods Using AST Transformations
  25. Part 4: Using Metaprogramming
  26. Chapter 17: Groovy Builders
    1. 17.1 Building XML
    2. 17.2 Building JSON
    3. 17.3 Building Swing
    4. 17.4 Custom Builder Using Metaprogramming
    5. 17.5 Using BuilderSupport
    6. 17.6 Using FactoryBuilderSupport
  27. Chapter 18: Unit Testing and Mocking
    1. 18.1 Code in This Book and Automated Unit Tests
    2. 18.2 Unit Testing Java and Groovy Code
    3. 18.3 Testing for Exceptions
    4. 18.4 Mocking
    5. 18.5 Mocking by Overriding
    6. 18.6 Mocking Using Categories
    7. 18.7 Mocking Using ExpandoMetaClass
    8. 18.8 Mocking Using Expando
    9. 18.9 Mocking Using Map
    10. 18.10 Mocking Using the Groovy Mock Library
  28. Chapter 19: Creating DSLs in Groovy
    1. 19.1 Context
    2. 19.2 Fluency
    3. 19.3 Types of DSLs
    4. 19.4 Designing Internal DSLs
    5. 19.5 Groovy and DSLs
    6. 19.6 Using Command-Chain Fluency
    7. 19.7 Closures and DSLs
    8. 19.8 Method Interception and DSLs
    9. 19.9 The Parentheses Limitation and a Workaround
    10. 19.10 Categories and DSLs
    11. 19.11 ExpandoMetaClass and DSLs
  29. Appendix 1: Web Resources
  30. Appendix 2: Bibliography
    1. You May Be Interested In…

Product information

  • Title: Programming Groovy 2
  • Author(s): Venkat Subramaniam
  • Release date: July 2013
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680504347