SlideShare a Scribd company logo
Adam L. Davis
The Solution Design Group, Inc.
@adamldavis.com
thesdg.com
Writing
Groovy DSLs
Agenda
●
Command Chains and @NamedParams
●
Operator Overloading
●
Categories, Extension modules
●
Closures, with, delegate, owner
●
propertyMissing, methodMissing
●
Groovy Scripts, GroovyShell
●
Groovy 3.0
●
Common DSLs
Command Chains
●
Command Chains allows parenthesis-free
fluent method calls
●
EX: method(param).then(foo) can be written
method param then foo
@NamedParams
● Mainly to give IDE hints for mapped parameters
● @NamedParams([array of @NamedParam])
● Can optionally tell what type is expected
Task task(@NamedParams([
@NamedParam(value = "name", type = String.class),
@NamedParam(value = "type", type = Class.class),
@NamedParam(value = "doLast", type = Closure.class),
@NamedParam(value = "doFirst", type = Closure.class),
@NamedParam(value = "depends", type = List.class)
]) Map<String, Object> map)
Operator Overloading
Demo 0
Categories, Extension modules
●
Category
– use(YourCategory) { *DSL* }
– YourCategory has static methods, first parameter is
class to decorate
– Unlike using metaclass, allows static compilation
●
Extension Modules!
– Much like Category but everywhere
– Put file in META-INF/groovy/
– “org.codehaus.groovy.runtime.ExtensionModule”
Closures, with, delegate, owner
●
Closures { }
●
object.with { * all methods, properties of object
available here * }
●
Closure.delegate = myThing
●
Closure.owner = myThing
●
method(@DelegatesTo(Foo) Closure param)
– Tells the compiler/IDE the class of the delegate
– Allows for auto-completion (in IntelliJ IDEA)
Demo 1
propertyMissing, methodMissing
●
def propertyMissing(String name): called
whenever something could be a property
reference.
– with/delegate/script-base: allows on the fly reference
of anything.
– propertyMissing(String name, value): assignment.
●
def methodMissing(String name, args):
– Name = name of method
– Args = array of arguments to method (0..n)
Groovy Scripts
●
No class or main(args) needed
●
GroovyShell
●
Ability to customize
– ImportCustomizer
– CompilerConfiguration
– Binding
– Change the script base-class
– Supply a class-loader
Demo 2
Groovy 3.0
●
Lambda expressions and :: supported
●
New operators:
– !in
– !instanceof
– ?=
– ?[x]
Groovy DSLs: JSONBuilder
def json = new groovy.json.JsonBuilder()
def root = json { // call(Closure)
person { // methodMissing( “person”, [Closure] )
firstName 'Adam' //methodMissing( “firstName”, [Adam] )
lastName 'Davis'
address( //methodMissing( “address”, [Map] )
city: 'Orlando',
country: 'USA',
zip: 32765
)
married true
// a list of values
conferences 'JavaOne', 'Gr8conf'
}
}
Groovy DSLs: Gradle
//def task(object)
task myTask //propertyMissing(“myTask”)
task myTask { configure closure }
//methodMissing(“myTask”, [closure])
task myTask(type: SomeType)
//methodMissing(“myTask”, [map])
task myTask(type: SomeType) { configure closure }
//methodMissing(“myTask”, [map,closure])
Groovy DSLs: GrooCSS
‘.myclass’.$ {} // Extension module: String
width 10.em // Extension module: Number
{ width 400.px height 200.px color black }
//Command chains
keyframes ‘flash’ {
10 % { color white }
50 % { color black }
100% { color white }
} // operator overloading
Thanks! Any ?’s
Adam L. Davis
The Solution Design Group, Inc.
@adamldavis.com
github.com/adamldavis/2019-gr8conf
or http://bit.ly/Gr8-2019BG
thesdg.com
This work is licensed under a Creative Commons
Attribution-ShareAlike 3.0 Unported License.
It makes use of the works of Mateus Machado Luna.
Thanks!

More Related Content

PDF
Learning groovy -EU workshop
ODP
Learning groovy 1: half day workshop
PDF
Objective c(lang)
PDF
Mongophilly shell-2011-04-26
PPT
Groovy presentation
PDF
Viliam Ganz - Domain Specific Languages
PDF
Mastering the MongoDB Shell
PDF
JavaScript, TypeScipt and React Native
Learning groovy -EU workshop
Learning groovy 1: half day workshop
Objective c(lang)
Mongophilly shell-2011-04-26
Groovy presentation
Viliam Ganz - Domain Specific Languages
Mastering the MongoDB Shell
JavaScript, TypeScipt and React Native

What's hot (20)

PPTX
Making Java Groovy (JavaOne 2013)
PPTX
PDF
Head First Java Chapter 8
PPTX
Next Generation Language Go
PDF
Better DSL Support for Groovy-Eclipse
PDF
(Greach 2015) Dsl'ing your Groovy
PDF
Head First Java Chapter 7
PDF
DConf 2016: Keynote by Walter Bright
PPTX
Briefly Rust
PDF
360|iDev
PPT
Groovy for Java Developers
PDF
Clojure Small Intro
PDF
Head First Java Chapter 4
PDF
Groovy for java developers
PPTX
MongoDB Aug2010 SF Meetup
PDF
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
PDF
GR8Conf 2009: Practical Groovy DSL by Guillaume Laforge
PPT
Prototype Js
PDF
Practical REPL-driven Development with Clojure
PDF
Fantastic DSL in Python
Making Java Groovy (JavaOne 2013)
Head First Java Chapter 8
Next Generation Language Go
Better DSL Support for Groovy-Eclipse
(Greach 2015) Dsl'ing your Groovy
Head First Java Chapter 7
DConf 2016: Keynote by Walter Bright
Briefly Rust
360|iDev
Groovy for Java Developers
Clojure Small Intro
Head First Java Chapter 4
Groovy for java developers
MongoDB Aug2010 SF Meetup
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
GR8Conf 2009: Practical Groovy DSL by Guillaume Laforge
Prototype Js
Practical REPL-driven Development with Clojure
Fantastic DSL in Python
Ad

Similar to Writing Groovy DSLs (20)

PDF
Gradle in 45min
PDF
An Introduction to Gradle for Java Developers
PDF
Groovy.pptx
PDF
Gradle in a Polyglot World
KEY
Groovy DSLs (JavaOne Presentation)
PDF
Gradle in 45min - JBCN2-16 version
PDF
Introductionto fp with groovy
PPTX
Groovy to gradle
PPT
Groovy Basics
PPTX
Groovy!
PDF
DSL's with Groovy
PDF
Using the Groovy Ecosystem for Rapid JVM Development
PDF
.gradle 파일 정독해보기
ODP
PDF
Oscon Java Testing on the Fast Lane
PDF
Groovy Fly Through
PPT
Groovy unleashed
PDF
Introduction to Groovy (Serbian Developer Conference 2013)
PPTX
Groovy closures
PDF
GR8Conf 2011: STS DSL Support
Gradle in 45min
An Introduction to Gradle for Java Developers
Groovy.pptx
Gradle in a Polyglot World
Groovy DSLs (JavaOne Presentation)
Gradle in 45min - JBCN2-16 version
Introductionto fp with groovy
Groovy to gradle
Groovy Basics
Groovy!
DSL's with Groovy
Using the Groovy Ecosystem for Rapid JVM Development
.gradle 파일 정독해보기
Oscon Java Testing on the Fast Lane
Groovy Fly Through
Groovy unleashed
Introduction to Groovy (Serbian Developer Conference 2013)
Groovy closures
GR8Conf 2011: STS DSL Support
Ad

Recently uploaded (20)

PPTX
1. Introduction to Computer Programming.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
Hybrid model detection and classification of lung cancer
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
Tartificialntelligence_presentation.pptx
PDF
August Patch Tuesday
PDF
Encapsulation theory and applications.pdf
PDF
Mushroom cultivation and it's methods.pdf
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Approach and Philosophy of On baking technology
PDF
project resource management chapter-09.pdf
1. Introduction to Computer Programming.pptx
Getting Started with Data Integration: FME Form 101
Hybrid model detection and classification of lung cancer
Unlocking AI with Model Context Protocol (MCP)
A novel scalable deep ensemble learning framework for big data classification...
Tartificialntelligence_presentation.pptx
August Patch Tuesday
Encapsulation theory and applications.pdf
Mushroom cultivation and it's methods.pdf
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
DP Operators-handbook-extract for the Mautical Institute
WOOl fibre morphology and structure.pdf for textiles
Building Integrated photovoltaic BIPV_UPV.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Group 1 Presentation -Planning and Decision Making .pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Approach and Philosophy of On baking technology
project resource management chapter-09.pdf

Writing Groovy DSLs

  • 1. Adam L. Davis The Solution Design Group, Inc. @adamldavis.com thesdg.com Writing Groovy DSLs
  • 2. Agenda ● Command Chains and @NamedParams ● Operator Overloading ● Categories, Extension modules ● Closures, with, delegate, owner ● propertyMissing, methodMissing ● Groovy Scripts, GroovyShell ● Groovy 3.0 ● Common DSLs
  • 3. Command Chains ● Command Chains allows parenthesis-free fluent method calls ● EX: method(param).then(foo) can be written method param then foo
  • 4. @NamedParams ● Mainly to give IDE hints for mapped parameters ● @NamedParams([array of @NamedParam]) ● Can optionally tell what type is expected Task task(@NamedParams([ @NamedParam(value = "name", type = String.class), @NamedParam(value = "type", type = Class.class), @NamedParam(value = "doLast", type = Closure.class), @NamedParam(value = "doFirst", type = Closure.class), @NamedParam(value = "depends", type = List.class) ]) Map<String, Object> map)
  • 7. Categories, Extension modules ● Category – use(YourCategory) { *DSL* } – YourCategory has static methods, first parameter is class to decorate – Unlike using metaclass, allows static compilation ● Extension Modules! – Much like Category but everywhere – Put file in META-INF/groovy/ – “org.codehaus.groovy.runtime.ExtensionModule”
  • 8. Closures, with, delegate, owner ● Closures { } ● object.with { * all methods, properties of object available here * } ● Closure.delegate = myThing ● Closure.owner = myThing ● method(@DelegatesTo(Foo) Closure param) – Tells the compiler/IDE the class of the delegate – Allows for auto-completion (in IntelliJ IDEA)
  • 10. propertyMissing, methodMissing ● def propertyMissing(String name): called whenever something could be a property reference. – with/delegate/script-base: allows on the fly reference of anything. – propertyMissing(String name, value): assignment. ● def methodMissing(String name, args): – Name = name of method – Args = array of arguments to method (0..n)
  • 11. Groovy Scripts ● No class or main(args) needed ● GroovyShell ● Ability to customize – ImportCustomizer – CompilerConfiguration – Binding – Change the script base-class – Supply a class-loader
  • 13. Groovy 3.0 ● Lambda expressions and :: supported ● New operators: – !in – !instanceof – ?= – ?[x]
  • 14. Groovy DSLs: JSONBuilder def json = new groovy.json.JsonBuilder() def root = json { // call(Closure) person { // methodMissing( “person”, [Closure] ) firstName 'Adam' //methodMissing( “firstName”, [Adam] ) lastName 'Davis' address( //methodMissing( “address”, [Map] ) city: 'Orlando', country: 'USA', zip: 32765 ) married true // a list of values conferences 'JavaOne', 'Gr8conf' } }
  • 15. Groovy DSLs: Gradle //def task(object) task myTask //propertyMissing(“myTask”) task myTask { configure closure } //methodMissing(“myTask”, [closure]) task myTask(type: SomeType) //methodMissing(“myTask”, [map]) task myTask(type: SomeType) { configure closure } //methodMissing(“myTask”, [map,closure])
  • 16. Groovy DSLs: GrooCSS ‘.myclass’.$ {} // Extension module: String width 10.em // Extension module: Number { width 400.px height 200.px color black } //Command chains keyframes ‘flash’ { 10 % { color white } 50 % { color black } 100% { color white } } // operator overloading
  • 17. Thanks! Any ?’s Adam L. Davis The Solution Design Group, Inc. @adamldavis.com github.com/adamldavis/2019-gr8conf or http://bit.ly/Gr8-2019BG thesdg.com
  • 18. This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. It makes use of the works of Mateus Machado Luna. Thanks!