Book Image

Groovy 2 Cookbook

Book Image

Groovy 2 Cookbook

Overview of this book

Get up to speed with Groovy, a language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. This book will show you the powerful features of Groovy 2 applied to real-world scenarios and how the dynamic nature of the language makes it very simple to tackle problems that would otherwise require hours or days of research and implementation. Groovy 2 Cookbook contains a vast number of recipes covering many facets of today's programming landscape. From language-specific topics such as closures and metaprogramming, to more advanced applications of Groovy flexibility such as DSL and testing techniques, this book gives you quick solutions to everyday problems. The recipes in this book start from the basics of installing Groovy and running your first scripts and continue with progressively more advanced examples that will help you to take advantage of the language's amazing features. Packed with hundreds of tried-and-true Groovy recipes, Groovy 2 Cookbook includes code segments covering many specialized APIs to work with files and collections, manipulate XML, work with REST services and JSON, create asynchronous tasks, and more. But Groovy does more than just ease traditional Java development: it brings modern programming features to the Java platform like closures, duck-typing, and metaprogramming. In this new book, you'll find code examples that you can use in your projects right away along with a discussion about how and why the solution works. Focusing on what's useful and tricky, Groovy 2 Cookbook offers a wealth of useful code for all Java and Groovy programmers, not just advanced practitioners.
Table of Contents (17 chapters)
Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Starting groovyConsole to execute Groovy snippets


The Groovy distribution has another option for developers who are more familiar with a graphical interface and want to try out code snippets. The tool in question is groovyConsole, which is very similar to groovysh (see the Using groovysh to try out Groovy commands recipe) except that it provides a GUI with syntax highlighting and basic code editor features.

In this recipe, we are going to cover basic GroovyConsole usage scenarios.

How to do it...

Open your shell and let's dive in:

  1. To start GroovyConsole, you just need to fire the groovyConsole command in your command line:

    The GroovyConsole screen is divided into two logical parts. The top area is meant for script editing, and the bottom area displays the script execution output when a script is launched.

  2. By pressing Ctrl + R (CMD + R on a Mac) or going to the Script menu and selecting Run, the script in the edit area will be executed:

  3. Press Ctrl + W (CMD + W on a Mac) to clean the output in the lower pane.

  4. The bottom panel displays an output similar to the groovysh shell; that is, first the executed script is printed after the groovy> marker followed by the printed output and a result value, if any:

  5. In the previous script, we set the value of the internal test property (this property is being stored within an instance of the groovy.lang.Script class that is used to execute all the code in the console). If you change that code to reuse the previous field value (for example, +=), you will actually notice that the value changes every time the code gets executed:

  6. Similar to groovysh, if you define a variable using the def keyword, then you can't reuse the previous value. The def keyword is used to define locally scoped variables and in this case, it's bound only to single execution of the script:

  7. Let's go through the editor features of the GroovyConsole application. The File menu offers standard functions to start over by clicking on the New File menu item, to open a new GroovyConsole window, to open a previously saved Groovy script, to save or print contents of the editor, and to close the application as shown in the following screenshot:

  8. The Edit menu provides functionality available in any modern text editor such as copy, paste, and basic search functionality, as shown in the following screenshot:

  9. The View menu has some interesting features for manipulating what is displayed in the bottom output area, as shown in the following screenshot:

    It is possible to increase or decrease the font size and configure the way the output panel displays information. For instance, you can disable the script output or clear the output screen at each run.

  10. The History menu allows you to revive previous script versions that you have executed during your session with GroovyConsole, as shown in the following screenshot:

  11. Since GroovyConsole is all about executing a script, the Script menu presents the most valuable options, as shown in the following screenshot:

    You can run or abort your script execution, force script compilation, and add additional libraries to the class path. Also, you can Clear Script Context, which will lead to clearing all the script's accumulated properties (for example, test).

  12. Another useful feature is that you can select a part of your script in the editor and execute only that part by clicking on Run Selection in the Script menu, as shown in the following screenshot:

There's more...

The GroovyConsole tool comes with a handy Object Browser that graphically shows the fields and methods available for a given class.

Press Ctrl + I (CMD + I on a Mac) to launch the browser and display the last evaluated expression. The following simple class would appear like in the following screenshot:

class Person {
  String name
  String lastName
}

p = new Person()

By pressing Ctrl + J (CMD + J on a Mac), we can display another view of the Object Browser, this time focused on the variables visible from within the script.