Groovy for Domain-specific Languages - Second Edition

Book description

Extend and enhance your Java applications with domain-specific scripting in Groovy

About 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 For

This 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 applications

In Detail

The 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 approach

This 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.

Table of contents

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

Product information

  • Title: Groovy for Domain-specific Languages - Second Edition
  • Author(s): Fergal Dearle
  • Release date: September 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781849695404