You are previewing Groovy for Domain-Specific Languages.
O'Reilly logo
Groovy for Domain-Specific Languages

Book Description

Java developers who need to build Domain-Specific Languages into their applications will find this book the passport to doing it with Groovy. Even the trickiest concepts are explained clearly and methodically.

  • Build your own Domain Specific Languages on top of Groovy

  • Integrate your existing Java applications using Groovy-based Domain Specific Languages (DSLs)

  • Develop a Groovy scripting interface to Twitter

  • A step-by-step guide to building Groovy-based Domain Specific Languages that run seamlessly in the Java environment

  • In Detail

    You may already be aware that the Java virtual machine runs on everything from the largest mainframe to the smallest microchip and supports almost every conceivable application. What you may not realize is that to develop software in some of these scenarios requires a targeted, single purpose language, a Domain Specific Language. The popular scripting language Groovy can be used to create a Domain Specific Language that can run directly on the JVM alongside regular Java code.

    This comprehensive tutorial will take you through the design and development of Groovy-based Domain Specific Languages. It is a complete guide to the development of several mini-DSLs with a lot of easy-to-understand examples. This book will help you to gain all of the skills needed to develop your own Groovy-based DSLs, as it guides you from the basics through to the more complex meta-programming features of Groovy. The focus is on how the Groovy language can be used to construct domain-specific mini-languages

    Practical examples are used throughout to de-mystify the seemingly complex language features and to show how they can be used to create simple and elegant DSLs. The examples include a quick and simple Groovy DSL to interface with Twitter.

    The book concludes with a chapter focusing on integrating Groovy-based DSLs in such a way that the scripts can be readily incorporated into the readers' own Java applications. The overall goal of this book is to take Java developers through the skills and knowledge they need to start building effective Groovy-based DSLs to integrate into their own applications.

    This practical guide will teach you how to build Domain Specific Languages in Groovy that integrate seamlessly with your Java applications

    Table of Contents

    1. Groovy for Domain-Specific Languages
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. Preface
      1. What this book covers
      2. Who this book is for
      3. Conventions
      4. Reader feedback
      5. Customer support
        1. Errata
        2. Piracy
        3. Questions
    6. 1. Introduction to DSL and Groovy
      1. DSL: 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 DSL
        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
          4. Groovy operator overloading
          5. Regular expression support
          6. Optional syntax
          7. Groovy markup
      3. Summary
    7. 2. Groovy Quick Start
      1. How to find and install Groovy
      2. 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
      3. Groovy IDE and editor integration
        1. Netbeans
        2. Eclipse
        3. IntelliJ IDEA
        4. Other IDEs and editors
      4. Introducing the Groovy Language
        1. Module structure
        2. Groovy shorthand
          1. Assumed imports
          2. Default visibility, optional semicolon
          3. Optional parentheses and types
          4. 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. Switch statement
          4. Loops
        9. Collections
          1. Ranges
          2. Lists
          3. Maps
        10. Operators
          1. Spread and spread-dot
          2. Null safe dereference
          3. Operator overloading
      5. Summary
    8. 3. 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
      5. Finding a named closure field
      6. Closure parameters
        1. Parameters and the doCall method
        2. Passing multiple parameters
        3. Enforcing zero parameters
        4. Default parameter values
        5. Curried parameters
      7. Closure return values
      8. Closure scope
        1. this, owner, and delegate
      9. Summary
    9. 4. 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 boilerplate
        1. Refactoring
          1. Fleshing out GeeTwitter
        2. Improving search
      5. Adding a command-line interface
      6. Adding built-in methods
      7. Summary
    10. 5. Power Groovy DSL features
      1. Named parameters
        1. Named parameters in DSLs
      2. Builders
        1. Builder design pattern
        2. Using Groovy Builders
          1. MarkupBuilder
        3. Namespaced XML
          1. GroovyMarkup syntax
        4. GroovyMarkup and the builder design pattern
        5. Using program logic with builders
        6. Builders for every occasion
          1. NodeBuilder
            1. Using GPath to navigate a node tree
      3. SwingBuilder
        1. Griffon: Builders as DSL
      4. Method pointers
      5. Metaprogramming and the Groovy MOP
        1. Reflection
          1. Groovy Reflection shortcuts
        2. Expandos
        3. Categories
        4. Metaclass
          1. Pretended methods (MetaClass.invokeMethod)
        5. Understanding this, delegate, and owner
      6. How Builders work
      7. ExpandoMetaClasses
        1. Replacing methods
        2. Adding or overriding static methods
        3. Dynamic method naming
        4. Adding overloaded methods
        5. Adding constructors
      8. Summary
    11. 6. Existing Groovy DSLs
      1. The Grails Object Relational Mapping (GORM)
        1. Grails quick start
        2. The grails-app directory
          1. DataSource configuration
        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. Gant
        1. Ant
        2. AntBuilder
        3. Gant and AntBuilder
      3. ATDD, BDD with GSpec, EasyB, and Spock
        1. GSpec
        2. EasyB
        3. Spock
          1. Blocks
        4. BDD DSL style
      4. Summary
    12. 7. Building a Builder
      1. Builder code structure
        1. Closure method calls
          1. Resolve Strategy: OWNER_FIRST
        2. Pretended methods
          1. invokeMethod
          2. methodMissing
        3. Closure delegate
      2. BuilderSupport
        1. BuilderSupport hook methods
        2. A database builder
      3. FactoryBuilderSupport
      4. Summary
    13. 8. 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
          7. Bindings and the GORM DataSource DSL
      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 GroovyTestCase
      3. Summary
    14. 9. Integrating it all
      1. Mixing and matching Groovy and Java
        1. Calling Groovy from Java
          1. POJOs and POGOs
        2. Calling Java from Groovy
        3. Privacy concerns
        4. Interfaces in Java and Groovy
        5. Resolving dependencies
        6. Dependency injection with Spring
      2. GroovyClassLoader
        1. Integrating scripts
        2. GroovyShell
          1. CompilerConfiguration
        3. GroovyScriptEngine
      3. Summary