Groovy in Action
eBook - ePub

Groovy in Action

Cédric Champeau, Dierk Koenig, Hamlet D'Arcy, Paul King

Share book
  1. 912 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Groovy in Action

Cédric Champeau, Dierk Koenig, Hamlet D'Arcy, Paul King

Book details
Book preview
Table of contents
Citations

About This Book

Summary Groovy in Action, Second Edition is a thoroughly revised, comprehensive guide to Groovy programming. It introduces Java developers to the dynamic features that Groovy provides, and shows how to apply Groovy to a range of tasks including building new apps, integration with existing code, and DSL development. Covers Groovy 2.4.Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology In the last ten years, Groovy has become an integral part of a Java developer's toolbox. Its comfortable, common-sense design, seamless integration with Java, and rich ecosystem that includes the Grails web framework, the Gradle build system, and Spock testing platform have created a large Groovy community About the Book Groovy in Action, Second Edition is the undisputed definitive reference on the Groovy language. Written by core members of the Groovy language team, this book presents Groovy like no other can—from the inside out. With relevant examples, careful explanations of Groovy's key concepts and features, and insightful coverage of how to use Groovy in-production tasks, including building new applications, integration with existing code, and DSL development, this is the only book you'll need. Updated for Groovy 2.4.Some experience with Java or another programming language is helpful. No Groovy experience is assumed. What's Inside

  • Comprehensive coverage of Groovy 2.4 including language features, libraries, and AST transformations
  • Dynamic, static, and extensible typing
  • Concurrency: actors, data parallelism, and dataflow
  • Applying Groovy: Java integration, XML, SQL, testing, and domain-specific language support
  • Hundreds of reusable examples


About the Authors
Authors Dierk König, Paul King, Guillaume Laforge, Hamlet D'Arcy, Cédric Champeau, Erik Pragt, and Jon Skeet are intimately involved in the creation and ongoing development of the Groovy language and its ecosystem.
Table of Contents
PART 1 THE GROOVY LANGUAGE

  • Your way to Groovy
  • Overture: Groovy basics
  • Simple Groovy datatypes
  • Collective Groovy datatypes
  • Working with closures
  • Groovy control structures
  • Object orientation, Groovy style
  • Dynamic programming with Groovy
  • Compile-time metaprogramming and AST transformations
  • Groovy as a static language
  • PART 2 AROUND THE GROOVY LIBRARY
  • Working with builders
  • Working with the GDK
  • Database programming with Groovy
  • Working with XML and JSON
  • Interacting with Web Services
  • Integrating Groovy
  • PART 3 APPLIED GROOVY
  • Unit testing with Groovy
  • Concurrent Groovy with GPars
  • Domain-specific languages
  • The Groovy ecosystem

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Groovy in Action an online PDF/ePUB?
Yes, you can access Groovy in Action by Cédric Champeau, Dierk Koenig, Hamlet D'Arcy, Paul King in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2015
ISBN
9781638352877

Part 1. The Groovy language

A good notation has subtlety and suggestiveness which at times makes it almost seem like a live teacher.
Bertrand Russell The World of Mathematics (1956)
Learning a new programming language is comparable to learning to speak a foreign language. You have to deal with new vocabulary, grammar, and language idioms. But this initial effort pays off multiple times. With the new language, you find unique ways to express yourself, you’re exposed to new concepts and styles that add to your personal abilities, and you may even explore new perspectives on your world. This is what Groovy did for us, and we hope Groovy will do it for you, too.
The first part of this book introduces you to the language basics: the Groovy syntax, grammar, and typical idioms. We present the language by example as opposed to using an academic style.
You may skim this part on first read and revisit it before going into serious development with Groovy. If you decide to skim, please make sure you visit chapter 2 and its examples. They are cross-linked to the in-depth chapters so you can easily look up details about any topic that interests you.
One of the difficulties of explaining a programming language by example is that you have to start somewhere. No matter where you start, you end up needing to use some concept or feature that you haven’t explained yet for your examples. Section 2.3 serves to resolve this perceived deadlock by providing a collection of self-explanatory warm-up examples.
We explain the main portion of the language using its built-in datatypes and introduce expressions, operators, and keywords as we go along. By starting with some of the most familiar aspects of the language and building up your knowledge in stages, we hope you’ll always feel confident when exploring new territory.
Chapter 3 introduces Groovy’s practical approach to typing, examines the numeric and other primitive types that Groovy supports, and discusses strings and regular expressions.
Chapter 4 continues looking at Groovy’s rich set of built-in types, examining those with a collection-like nature: ranges, lists, and maps.
Chapter 5 builds on the preceding sections and provides an in-depth description of the closure concept.
Chapter 6 touches on logical branching, looping, and shortcutting program execution flow.
Chapter 7 sheds light on the way Groovy builds on Java’s object-oriented features adding support for multimethods and traits.
Chapter 8 looks at Groovy’s dynamic programming capabilities.
Chapter 9 dives into compile-time metaprogramming and AST transformations.
Chapter 10, the final chapter in part 1, discusses Groovy as a static language.
At the end of part 1, you’ll have the whole picture of the Groovy language. This is the basis for getting the most out of part 2, which explores the Groovy library: the classes and methods that Groovy adds to the Java platform. Part 3, “Applied Groovy,” leads you through places where the power of Groovy is put into action.

Chapter 1. Your way to Groovy

This chapter covers
  • What Groovy is all about
  • How it makes your programming life easier
  • How to start
It isn’t the mountains ahead to climb that wear you out; it’s the pebble in your shoe.
Muhammad Ali
You’ve heard of Groovy, maybe even installed the distribution and tried snippets from the online tutorials. Perhaps your project has adopted Groovy as a dynamic extension to Java and you now seek information about what you can do with it. You may have been acquainted with Groovy from using the Grails web application platform, the Griffon desktop application framework, the Gradle build system, or the Spock testing facility, and now look for background information about the language that these tools are built upon. This book delivers to that purpose, but you can expect even more from learning Groovy.
Groovy will give you quick wins, whether by making your Java code simpler to write, by automating recurring tasks, by modeling business logic in domain-specific languages (DSLs), or by supporting ad-hoc scripting for your daily work as a programmer. It’ll give you longer-term wins by making your code simpler to read. Perhaps most important, it’s a pleasure to use.
Learning Groovy is a wise investment. Groovy brings the power of advanced language features such as closures, dynamic methods, and the Meta Object Protocol (MOP) to the Java platform. Your Java knowledge will not become obsolete by walking the Groovy path. Groovy will build on your experience and familiarity with the Java platform, allowing you to pick and choose when you use which tool—and when to combine the two seamlessly.
Groovy follows a pragmatic “no drama”[1] approach: it obeys the Java object model and always keeps the perspective of a Java programmer. It doesn’t force you into any new programming paradigm, but offers those advanced capabilities that you legitimately expect from a “top-of-stack” language.
1 Thanks to Mac Liaw for this wording.
This first chapter provides background information about Groovy and everything you need to know to get started. It starts with the Groovy story: why Groovy was created, what considerations drive its design, and how it positions itself in the landscape of languages and technologies. The next section expands on Groovy’s merits and how they can make life easier for you, whether you’re a Java programmer, a script aficionado, or an agile developer.
We strongly believe that there’s only one way to learn a programming language: by trying it. We present a variety of scripts to demonstrate the compiler, interpreter, and shells, before listing plug-ins available for widely used IDEs and where to find the latest information about Groovy.
By the end of this chapter, you’ll have a basic understanding of what Groovy is and how you can experiment with it.
We—the authors, the reviewers, and the editing team—wish you a great time programming Groovy and using this book for guidance and reference.

1.1. The Groovy story

At Groovy One 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.
He and his wife were waiting for a late plane. While she went shopping, he visited an internet cafe and spontaneously decided to go to the Python website and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes...

Table of contents