Groovy for Domain-specific Languages - Second Edition
eBook - ePub

Groovy for Domain-specific Languages - Second Edition

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

Groovy for Domain-specific Languages - Second Edition

Book details
Book preview
Table of contents
Citations

About This Book

Extend and enhance your Java applications with domain-specific scripting in GroovyAbout This Book• Build domain-specific mini languages in Groovy that integrate seamlessly with your Java apps with this hands-on guide• Increase stakeholder participation in the development process with domain-specific scripting in Groovy• Get up to speed with the newest features in Groovy using this second edition and integrate Groovy-based DSLs into your existing Java applications.Who This Book Is ForThis book is for Java software developers who have an interest in building domain scripting into their Java applications. No knowledge of Groovy is required, although it will be helpful. This book does not teach Groovy, but quickly introduces the basic ideas of Groovy. An experienced Java developer should have no problems with these and move quickly on to the more involved aspects of creating DSLs with Groovy. No experience of creating a DSL is required.What You Will Learn• Familiarize yourself with Groovy scripting and work with Groovy closures• Use the meta-programming features in Groovy to build mini languages• Employ Groovy mark-up and builders to simplify application development• Familiarize yourself with Groovy mark-up and build your own Groovy builders• Build effective DSLs with operator overloading, command chains, builders, and a host of other Groovy language features• Integrate Groovy with your Java and JVM based applicationsIn DetailThe times when developing on the JVM meant you were a Java programmer have long passed. The JVM is now firmly established as a polyglot development environment with many projects opting for alternative development languages to Java such as Groovy, Scala, Clojure, and JRuby. In this pantheon of development languages, Groovy stands out for its excellent DSL enabling features which allows it to be manipulated to produce mini languages that are tailored to a project's needs.A comprehensive tutorial on designing and developing mini Groovy based Domain Specific Languages, this book will guide you through the development of several mini DSLs that will help you gain all the skills needed to develop your own Groovy based DSLs with confidence and ease.Starting with the bare basics, this book will focus on how Groovy can be used to construct domain specific mini languages, and will go through the more complex meta-programming features of Groovy, including using the Abstract Syntax Tree (AST). Practical examples are used throughout this book to de-mystify these seemingly complex language features and to show how they can be used to create simple and elegant DSLs. Packed with examples, including several fully worked DSLs, this book will serve as a springboard for developing your own DSLs.Style and approachThis book is a hands-on guide that will walk you through examples for building DSLs with Groovy rather than just talking about "metaprogramming with Groovy". The examples in this book have been designed to help you gain a good working knowledge of the techniques involved and apply these to producing your own Groovy based DSLs.

Frequently asked questions

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.
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.
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.
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.
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.
Yes, you can access Groovy for Domain-specific Languages - Second Edition by Fergal Dearle in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781849695411
Edition
2

Groovy for Domain-specific Languages Second Edition


Table of Contents

Groovy for Domain-specific Languages Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introduction to DSLs and Groovy
DSL – a new name for an old idea
The evolution of programming languages
General-purpose languages
Spreadsheets and 4GLs
Language-oriented programming
Who are DSLs for?
A DSL for process engineers
Stakeholder participation
DSL design and implementation
External versus internal DSLs
Operator overloading
Groovy
A natural fit with the JVM
Groovy language features
Static and optional typing
Native support for lists and maps
Closures
Groovy operator overloading
Regular expression support
Optional syntax
Groovy markup
Summary
2. Groovy Quick Start
Installing Groovy with GVM
Installing GVM
Installing GVM on Windows
How to find and install Groovy from binaries
Running Groovy
The Groovy script engine – groovy
Shebang scripts
The Groovy shell – groovysh
The Groovy console – groovyConsole
The Groovy compiler – groovyc
The Groovy IDE and editor integration
NetBeans
Eclipse
Spring STS
IntelliJ IDEA
Other IDEs and editors
Summary
3. Essential Groovy DSLs
Installing Gradle
Gradle basics
Gradle build scripts
Gradle tasks
Adding actions to tasks
Default tasks
Creating task dependencies
Built-in tasks and plugins
Plugins
The Gradle Groovy plugin
The IDEA plugin
The Eclipse plugin
Repositories
Dependencies
Spock tests
Given, when, then
Spock specification structure
Feature methods
Blocks
Fields
Fixture methods
Helper methods
Where blocks
Fixture blocks
Testing Gradle using Spock
Further reading
Summary
4. The Groovy Language
Introducing the Groovy language
The module structure
Groovy shorthand
Implicit imports
Default visibility, optional semicolon
Optional parentheses
The optional dot in method chains
Dynamic types
The optional return keyword
Properties and GroovyBeans
Assertions
Autoboxing
Strings
Regular expressions
Methods and closures
Control structures
Groovy Truth
Ternary and Elvis operators
Spaceship and Elvis operators
The switch statement
Loops
Collections
Ranges
Lists
Maps
Operators
Spread and spread-dot
Null safe dereference
Operator overloading
Summary
5. Groovy Closures
What is a closure?
Closures and collection methods
Closures as method parameters
Method parameters as DSL
Forwarding parameters
Calling closures
The implicit doCall method
Finding a named closure field
Closure parameters
Enforcing zero parameters
Parameters and the doCall method
Passing multiple parameters
Default parameter values
Implementing closures in Java
Curried parameters
Closure return values
The closure scope
The this, owner, and delegate variables
Closure composition
Closure trampoline
Closure memoization
Summary
6. Example DSL – GeeTwitter
Twitter
Working with the Twitter APIs
Using Twitter4J Java APIs
Tweeting
Direct messages
Searching
Following
Groovy improvements
A Groovier way to find friends
Groovy searching
Removing the boilerplate
Refactoring
Fleshing out GeeTwitter
Improving search
Adding a command-line interface
Adding built-in methods
Summary
7. Power Groovy DSL Features
Named parameters
Named parameters in DSLs
Command chains
Builders
The builder design pattern
Using Groovy builders
MarkupBuilder
Namespaced XML
The GroovyMarkup syntax
GroovyMarkup and HTML
Using program logic with builders
Builders for every occasion
NodeBuilder
Using GPath to navigate a node tree
SwingBuilder
Method pointers
Metaprogramming and the Groovy MOP
Reflection
Groovy reflection shortcuts
Expandos
Categories
Traits
MetaClass
Pretended methods – MetaClass.invokeMethod
Understanding this, delegate, and owner
How builders work
ExpandoMetaClasses
Replacing methods
Adding or overriding static methods
Dynamic method naming
Adding overloaded methods
Adding constructors
Summary
8. AST Transformations
What is an AST
Compiler phases
Local AST transformations
Using ASTBuilder
Build from code
Build from Spec
Traits to the rescue
Global AST transformations
A finite state machine DSL
The state machine pattern
A state machine AST transformation
Handling errors – compile errors
Building the new AST nodes
Testing the state machine DSL
Compiling and packaging an AST transformation
Summary
9. Existing Groovy DSLs
Grails object relational mapping – GORM
Grails quick start
The grails-app directory
Building a GORM model
Using domain classes
Modeling relationships
Associations
One-to-one
Constraints
One-to-many
Many-to-many
Composition
Inheritance
Mapping
Querying
Dynamic finders
GORM as a DSL
Spock as a DSL
Spock
JUnit
Summary
10. Building a Builder
The builder code structure
Closure method calls
The resolve strategy – OWNER_FIRST
Pretended methods
invokeMethod
methodMissing
The closure delegate
BuilderSupport
BuilderSupport hook methods
A database builder
FactoryBuilderSupport
Summary
11. Implementing a Rules DSL
Groovy bindings
Exploiting bindings in DSLs
Closures as built-in methods
Closures as repeatable blocks
Using a specification parameter
Closures as singleton blocks
Using binding properties to form context
Storing and communicating results
Building a rewards DSL
Designing the DSL
BroadbandPlus
Reward types
The reward DSL
Handling events – deferred execution
Convenience methods and shorthand
The offers
The RewardService class
The BroadbandPlus application classes
Testing with Spock
Summary
12. Integrating It All
Groovy as a teaching language
Hiding complexity
A game DSL for kids
A game DSL – goals
Architecture and technology choices
TicTacToe in a DSL
Implementing the AST transform
Building the game engine pattern
Testing the DSL
Class loading issues
Gradle subprojects
Debugging
The game server
Integrating with Spring Boot server
Controller annotations
JSONP
Loading the DSL
Spring Data mapping for MongoDB
The Game UI
Summary
Index

Groovy for Domain-specific Languages Second Edition

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2010
Second edition: September 2015
Production reference: 1230915
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-540-4
www.packtpub.com

Credits

Author
Fergal Dearle
Reviewers
David W Millar
Pietro Martinelli
Jason Winnebeck
Commissioning Editor
Erol Staveley
Acquisition E...

Table of contents

  1. Groovy for Domain-specific Languages Second Edition