Table of Contents
- Chapter 1: Introduction to DSLs and Groovy Preface ix
- 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
- The evolution of programming languages
- 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
- DSL – a new name for an old idea
- Chapter 2: Groovy Quick Start [ ii ]
- Installing Groovy with GVM
- Installing GVM
- Installing GVM on Windows
- Installing GVM
- 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
- Installing Groovy with GVM
- Chapter 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
- 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 [ iii ]
- Summary
- Chapter 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
- 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
- Introducing the Groovy language
- Chapter 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 [ iv ]
- 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
- Chapter 6: Example DSL – GeeTwitter
- Working with the Twitter APIs
- Using Twitter4J Java APIs
- Tweeting
- Direct messages
- Searching
- Following
- Using Twitter4J Java APIs
- Groovy improvements
- A Groovier way to find friends
- Groovy searching
- A Groovier way to find friends
- Removing the boilerplate
- Refactoring
- Fleshing out GeeTwitter
- Improving search
- Refactoring
- Adding a command-line interface
- Adding built-in methods
- Summary
- Chapter 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 [ v ]
- Using program logic with builders
- Builders for every occasion
- NodeBuilder
- 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
- Reflection
- How builders work
- ExpandoMetaClasses
- Replacing methods
- Adding or overriding static methods
- Dynamic method naming
- Adding overloaded methods
- Adding constructors
- Summary
- Named parameters
- Chapter 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
- Chapter 9: Existing Groovy DSLs [ vi ]
- Grails object relational mapping – GORM
- Grails quick start
- The grails-app directory
- Building a GORM model
- Using domain classes
- Modeling relationships
- Associations
- Composition
- Inheritance
- Querying
- Dynamic finders
- GORM as a DSL
- Spock as a DSL
- Spock
- JUnit
- Spock
- Summary
- Grails object relational mapping – GORM
- Chapter 10: Building a Builder
- The builder code structure
- Closure method calls
- The resolve strategy – OWNER_FIRST
- Pretended methods
- invokeMethod
- methodMissing
- The closure delegate
- Closure method calls
- BuilderSupport
- BuilderSupport hook methods
- A database builder
- FactoryBuilderSupport
- Summary
- The builder code structure
- Chapter 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
- Exploiting bindings in DSLs
- Building a rewards DSL [ vii ]
- 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
- Designing the DSL
- Summary
- Groovy bindings
- Chapter 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
- Groovy as a teaching language
- Index