Making Java Groovy
Simplify Your Java Development
With Groovy
JavaOne 2013
Contact Info
Ken Kousen
President, Kousen IT, Inc.
www.kousenit.com
ken.kousen@kousenit.com
@kenkousen
Making Java Groovy
http://manning.com/kousen
Groovy
JVM language
Groovy
JVM language
compiles to bytecodes
Groovy
JVM language
compiles to bytecodes
optional semicolons
Groovy
JVM language
compiles to bytecodes
optional semicolons
optional parentheses
Groovy
JVM language
compiles to bytecodes
optional semicolons
optional parentheses
everything is an object
Adding Groovy to Java
Adding Groovy to Java
Actually, Boromir, One Does
Adding Groovy to Java
Actually, Boromir, One Does
(and simply)
Adding Groovy to Java
Actually, Boromir, One Does
(and simply)
(and btw, Lord Stark, you might want
to stay out
of King’s Landing…)
Adding Groovy to Java
Demo: Execute compiled Groovy
with java command
Operator Overloading
All operators in Groovy invoke methods
Operator Overloading
All operators in Groovy invoke methods
a + b a.plus(b)
a – b a.minus(b)
a * b a.multiply(b)
a[b] a.getAt(b)
a[b] = c a.putAt(b,c)
a == b a.equals(b) or
a.compareTo(b)
Operator Overloading
All operators in Groovy invoke methods
http://groovy.codehaus.org/Operator+Overloading
a + b a.plus(b)
a – b a.minus(b)
a * b a.multiply(b)
a[b] a.getAt(b)
a[b] = c a.putAt(b,c)
a == b a.equals(b) or
a.compareTo(b)
POGOs
Actually, YES
POGOs
Actually, YES
Plain Old Groovy Objects
POGOs
Generated getters / setters
POGOs
Generated getters / setters
Map-based constructor
POGOs
Generated getters / setters
Map-based constructor
AST transformations
@ToString
@EqualsAndHashCode
@TupleConstructor
@Canonical
Adding Groovy to Java
Demo:
Account class in Groovy
NumberFormat from Java
Test in Java
Collections
Okay, make it so…
Collections
Okay, make it so…
Native collections
Collections
Okay, make it so…
Native collections
Ranges
Collections
Okay, make it so…
Native collections
Ranges
Lists and Sets
Collections
Okay, make it so…
Native collections
Ranges
Lists and Sets
Maps
Ranges
Implement Comparable
next()
previous()
Number, Date, String, …
Lists
[3, 1, 4, 1, 5, 9, …]
['this', 'is', 'a', 'list’]
Sets
[3, 1, 4, 1, 5, 9, …] as Set
Set strings
Maps
def map = [a:1, b:2, c:3]
map.d = 4
map['e'] = 5
map.put('f', 6)
Closures
{ … it … }
{ x, y -> x + y }
Closures
{ … it … }
{ x, y -> x + y }
anonymous functions as first-class objects
Closures
{ … it … }
{ x, y -> x + y }
anonymous functions as first-class objects
(oversimplified, but it’s a start…)
Closures
Methods on collections:
each
collect
findAll
inject
Functional Groovy
Closures  lambdas (if pure)
collect  map
findAll  filter
inject  reduce
Groovy JDK
Java classes enhanced by Groovy
through metaprogramming
Number
String
Date
Collection
URL
…
RESTful Web Service
Google v3 Geocoder
Convert addresses to latitude / longitude
Geocoder
Base URL
URLEncoder.encode(it, 'UTF-8').join(',’)
Geocoder
Base URL
URLEncoder.encode(it, 'UTF-8').join(',’)
params.collect { k,v -> "$k=$v"
}.join('&')
Geocoder
Base URL
URLEncoder.encode(it, 'UTF-8').join(',’)
params.collect { k,v -> "$k=$v"
}.join('&')
XmlSlurper.parse("$base$qs")
JSON
ICNDB  the Internet Chuck Norris Database
http://api.icndb.com/jokes/random?
limitTo=[nerdy]&
firstName=…&
lastName=…
ICNDB
JsonSlurper
Metaprogramming
Spock Testing
Spock
Specification + Mock
Spock
Specification + Mock
Includes JUnit runner
Spock
Specification + Mock
Includes JUnit runner
Data-driven tests
Spock
Specification + Mock
Includes JUnit runner
Data-driven tests
Database-driven tests
Gradle
Build tool
Gradle
Build tool
DSL for builds
Gradle
Build tool
DSL for builds
Build script in Groovy rather than XML
Gradle
Build tool
DSL for builds
Build script in Groovy rather than XML
Used by:
Hibernate, Spring, Android, …
Summary
Groovy compiles to bytecodes
Groovy JDK
Native collections
Closures
Builders
Metaprogramming

Making Java Groovy (JavaOne 2013)